All Projects → lukascivil → TreeWalker

lukascivil / TreeWalker

Licence: MIT license
PHP JSON diff

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to TreeWalker

Aehnlich
Show/Merge differences in directories and their content (text files) in Light/Dark designs
Stars: ✭ 73 (+25.86%)
Mutual labels:  diff, merge
jQuery-Merge-for-php-diff
A client side merge tool for JBlonds PHP-Diff @ https://github.com/JBlond/php-diff.
Stars: ✭ 74 (+27.59%)
Mutual labels:  diff, merge
Nbdime
Tools for diffing and merging of Jupyter notebooks.
Stars: ✭ 2,135 (+3581.03%)
Mutual labels:  diff, merge
winmerge2011
Fork of WinMerge which has a different set of features
Stars: ✭ 36 (-37.93%)
Mutual labels:  diff, merge
treediff-rs
Extract differences between arbitrary datastructures
Stars: ✭ 52 (-10.34%)
Mutual labels:  diff, merge
Winmerge
WinMerge is an Open Source differencing and merging tool for Windows. WinMerge can compare both folders and files, presenting differences in a visual text format that is easy to understand and handle.
Stars: ✭ 2,358 (+3965.52%)
Mutual labels:  diff, merge
diffy
Tools for finding and manipulating differences between files
Stars: ✭ 47 (-18.97%)
Mutual labels:  diff, merge
lightline-gitdiff
Show added, deleted and modified lines (`git diff`) in your statusline or lightline
Stars: ✭ 27 (-53.45%)
Mutual labels:  diff
microdiff
A fast, zero dependency object and array comparison library. Significantly faster than most other deep comparison libraries and has full TypeScript support.
Stars: ✭ 3,138 (+5310.34%)
Mutual labels:  diff
ngx-text-diff
A Text Diff component for Angular
Stars: ✭ 49 (-15.52%)
Mutual labels:  diff
git-json-merge
A git merge driver that use xdiff to automatically resolve merge conflicts in json files. This project was inspired by git-po-merge.
Stars: ✭ 86 (+48.28%)
Mutual labels:  merge
duff
Pure OCaml implementation of libXdiff (Rabin's fingerprint)
Stars: ✭ 20 (-65.52%)
Mutual labels:  diff
elixir-utilities-web
Utilties for the Developer. Regex, HTTP echo. Diffing
Stars: ✭ 36 (-37.93%)
Mutual labels:  diff
dif
'dif' is a Linux preprocessing front end to gvimdiff/meld/kompare
Stars: ✭ 18 (-68.97%)
Mutual labels:  diff
defaults-deep
Like `extend` but recursively copies only the missing properties/values to the target object.
Stars: ✭ 26 (-55.17%)
Mutual labels:  merge
textdiff-create
Create lean text diff deltas.
Stars: ✭ 25 (-56.9%)
Mutual labels:  diff
ecomparatio
eComparatio: text diff and support for digital edition
Stars: ✭ 21 (-63.79%)
Mutual labels:  diff
Differentia.js
No longer being supported or maintained. A Graph Theory & Data Structure Library for JavaScript.
Stars: ✭ 13 (-77.59%)
Mutual labels:  diff
git-picked
List merged and cherry-picked branches
Stars: ✭ 30 (-48.28%)
Mutual labels:  merge
spotdiff.vim
A range and area selectable diffthis to compare partially
Stars: ✭ 29 (-50%)
Mutual labels:  diff

TreeWalker

TreeWalker is a simple and small Library that will help you to work faster with manipulation of structures in PHP

Build Status Total Downloads codecov License

  • getdiff() - Get json difference
  • replaceValues() - Edit json value (Recursively)
  • walker() - Edit json (Recursively)
  • structMerge() - Joins two structures
  • createDynamicallyObjects() - Create nested structure by Dynamic keys
  • getDynamicallyValue() - Dynamically get a structure property
  • setDynamicallyValue() - Dynamically access a structure property to set a value

structure = ["jsonstring", "object", "array"]

EXAMPLE - master

Prerequisites

  • PHP >= 5.5

Installation

Using composer

Put the require statement for TreeWalker in your composer.json and install:

{
  "require": {
    "lukascivil/treewalker": "dev-master"
  }
}
composer require lukascivil/treewalker dev-master

Manually

include the TreeWalker.php

<?php
include 'pathto/TreeWalker.php';

Examples

Init:

  $treewalker = new TreeWalker(array(
    "debug"=>true,                      //true => return the execution time, false => not
    "returntype"=>"jsonstring")         //Returntype = ["obj","jsonstring","array"]
  );

Methods:

    //getdiff() - this method will return the diference between struct1 and struct2

    $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5);
    $struct2 = array("casa"=>2, "cafeina"=>array("ss"=>"dddd"), "oi2"=>5);

    $treewalker->getdiff($struct1, $struct2, false) // false -> with slashs

    Output:
    {
        new: {
            b: "5",
            oi: 5
        },
        removed: {
            oi2: 5
        },
        edited: {
            casa: {
              oldvalue: 2,
              newvalue: 1
            },
            cafeina/ss: {
              oldvalue: "dddd",
              newvalue: "ddd"
            }
        },
        time: 0
    }
    //walker() - Walk recursively through the structure

    $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    $treewalker->walker($struct, function(&$struct, $key, &$value) {
        //Removing element
        if ($key == "ff") {
            unset($struct[$key]);
        }

        //changing element
        if ($key == "ff1") {
            $value = array("son" => "tiago");
        }
    })

    Output:
    {"casa":2,"cafeina":{"ss":{"ff1":{"son":"tiago"}}},"oi2":5,"1":"","ss":"dddddf","time":"0 miliseconds"}
    //structMerge() - Merge Structures

    $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss1"=>"1", "ss2"=>"2"), "oi"=>5, "1" => "255");
    $struct2 = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    $treewalker->structMerge($struct2, $struct1, true); //true -> No slashs

    Output:
    {"casa":2,"b":"5","cafeina":{"ss1":"1","ss2":"2","ss":{"ff":21,"ff1":22}},"oi":5,"0":"255","oi2":5,"1":"","ss":"dddddf","time":"0 miliseconds"}
    //createDynamicallyObjects() - this method will create nested objects with with dynamic keys

    $struct = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5, "1" => "255");

    //P.s
    $treewalker->createDynamicallyObjects($struct, array(1,2,5,9,10,11));

    Output:

     {
       "casa": 1,
       "b": "5",
       "cafeina": {
          "ss": "ddd"
       },
       "oi": 5,
       "1": {
          "2": {
            "5": {
              "9": {
                "10": {
                  "11": {}
                }
              }
            }
          }
        }
      }
    //getDynamicallyValue()

    $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    Static access:
    $struct["cafeina"]["ss"];

    Dynamic access:
    $treewalker->getDynamicallyValue($struct, array("cafeina","ss"));

    Output:
    {"ff":21,"ff1":22}
    //setDynamicallyValue()

    $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");

    Static access:
    $struct["cafeina"]["ss"] = "newvalue";

    Dynamic access:
    $treewalker->setDynamicallyValue($struct, array("cafeina","ss"), "newvalue");

    Output:
    {"casa":2,"cafeina":{"ss":"newvalue"},"oi2":5,"1":"","ss":"dddddf"}

Test

composer install
composer test

Additional context

If you need the JS version to also compare objects, you can use this (JsonDifference) lib which will have the same result on the client side.

License

The MIT License (MIT)

Copyright (c) [2016] [LUCAS CORDEIRO DA SILVA]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].