项目作者: lukascivil

项目描述 :
PHP JSON walker
高级语言: PHP
项目地址: git://github.com/lukascivil/TreeWalker.git
创建时间: 2016-02-03T01:35:17Z
项目社区:https://github.com/lukascivil/TreeWalker

开源协议:MIT License

下载


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:

  1. {
  2. "require": {
  3. "lukascivil/treewalker": "dev-master"
  4. }
  5. }
  1. composer require lukascivil/treewalker dev-master

Manually

include the TreeWalker.php

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

Examples

Init:

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

Methods:

  1. //getdiff() - this method will return the diference between struct1 and struct2
  2. $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5);
  3. $struct2 = array("casa"=>2, "cafeina"=>array("ss"=>"dddd"), "oi2"=>5);
  4. $treewalker->getdiff($struct1, $struct2, false) // false -> with slashs
  5. Output:
  6. {
  7. new: {
  8. b: "5",
  9. oi: 5
  10. },
  11. removed: {
  12. oi2: 5
  13. },
  14. edited: {
  15. casa: {
  16. oldvalue: 2,
  17. newvalue: 1
  18. },
  19. cafeina/ss: {
  20. oldvalue: "dddd",
  21. newvalue: "ddd"
  22. }
  23. },
  24. time: 0
  25. }
  1. //walker() - Walk recursively through the structure
  2. $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");
  3. $treewalker->walker($struct, function(&$struct, $key, &$value) {
  4. //Removing element
  5. if ($key == "ff") {
  6. unset($struct[$key]);
  7. }
  8. //changing element
  9. if ($key == "ff1") {
  10. $value = array("son" => "tiago");
  11. }
  12. })
  13. Output:
  14. {"casa":2,"cafeina":{"ss":{"ff1":{"son":"tiago"}}},"oi2":5,"1":"","ss":"dddddf","time":"0 miliseconds"}
  1. //structMerge() - Merge Structures
  2. $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss1"=>"1", "ss2"=>"2"), "oi"=>5, "1" => "255");
  3. $struct2 = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");
  4. $treewalker->structMerge($struct2, $struct1, true); //true -> No slashs
  5. Output:
  6. {"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"}
  1. //createDynamicallyObjects() - this method will create nested objects with with dynamic keys
  2. $struct = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5, "1" => "255");
  3. //P.s
  4. $treewalker->createDynamicallyObjects($struct, array(1,2,5,9,10,11));
  5. Output:
  6. {
  7. "casa": 1,
  8. "b": "5",
  9. "cafeina": {
  10. "ss": "ddd"
  11. },
  12. "oi": 5,
  13. "1": {
  14. "2": {
  15. "5": {
  16. "9": {
  17. "10": {
  18. "11": {}
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  1. //getDynamicallyValue()
  2. $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");
  3. Static access:
  4. $struct["cafeina"]["ss"];
  5. Dynamic access:
  6. $treewalker->getDynamicallyValue($struct, array("cafeina","ss"));
  7. Output:
  8. {"ff":21,"ff1":22}
  1. //setDynamicallyValue()
  2. $struct = array("casa"=>2, "cafeina"=>array("ss"=>array("ff"=>21, "ff1"=>22)), "oi2"=>5, "1"=>"", "ss"=>"dddddf");
  3. Static access:
  4. $struct["cafeina"]["ss"] = "newvalue";
  5. Dynamic access:
  6. $treewalker->setDynamicallyValue($struct, array("cafeina","ss"), "newvalue");
  7. Output:
  8. {"casa":2,"cafeina":{"ss":"newvalue"},"oi2":5,"1":"","ss":"dddddf"}

Test

  1. composer install
  2. 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)

  1. Copyright (c) [2016] [LUCAS CORDEIRO DA SILVA]
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated documentation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all
  9. copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. SOFTWARE.