From 4edca239784770cf5944b46bbebde7a680cafc15 Mon Sep 17 00:00:00 2001 From: lucas cordeiro da Silva Date: Sat, 30 Apr 2016 21:48:43 -0300 Subject: [PATCH] 0.2 --- example/index.php | 39 +++--- src/treeWalker.php | 302 ++++++++++++++++++++++++--------------------- 2 files changed, 177 insertions(+), 164 deletions(-) diff --git a/example/index.php b/example/index.php index d52a5f5..0310a0f 100644 --- a/example/index.php +++ b/example/index.php @@ -1,27 +1,16 @@ - - - - - treeWalker - - - true, //true => return the time, false => not "returntype"=>"jsonstring") //Returntype = ["obj","jsonstring","array"] - ); - - /*$struct2 = json_decode(utf8_encode(file_get_contents('json/json1.json')), true); - $struct1 = json_decode(utf8_encode(file_get_contents('json/json2.json')), true);*/ - - $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5); - $struct2 = array("casa"=>2, "cafeina"=>array("ss"=>"dddd"), "oi2"=>5); - - //first argument == modified, second argument static - //print_r($treeWalker->getdiff($struct1, $struct2)); - print_r($treeWalker->replaceValues($struct2, "test", "ss", false)); - ?> - - + ); + /*$struct2 = json_decode(utf8_encode(file_get_contents('json/json1.json')), true); + $struct1 = json_decode(utf8_encode(file_get_contents('json/json2.json')), true);*/ + $struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5, "1" => "255"); + //$struct2 = array("casa"=>2, "cafeina"=>array("ss"=>"dddd"), "oi2"=>5); + //first argument == modified, second argument static + //print_r($treeWalker->getdiff($struct1, $struct2)); + //print_r($treeWalker->replaceValues($struct2, "test", "ss", false)); + $treeWalker->createDynamicallyObjects($struct1, array(1,2,5,9,10,11)); + print_r($struct1); +?> diff --git a/src/treeWalker.php b/src/treeWalker.php index aac6d10..9e3c6af 100644 --- a/src/treeWalker.php +++ b/src/treeWalker.php @@ -1,5 +1,5 @@ debug = $config["debug"]; - } + if(isset($config["debug"])) { + $this->debug = $config["debug"]; + } - if($config["returntype"]) { - $this->returntype = strtolower($config["returntype"]); + if($config["returntype"]) { + $this->returntype = strtolower($config["returntype"]); + } } - } - public function replaceValues($assocarray, $newvalue, $field, $onlyseed) { - if(!$this->studytype($assocarray, $problem)) { - return $problem; + /* + accesDynamically((String) : Path, + (Array) : Current array + ) + */ + private function accesDynamically($path_string, &$array) { + $keys = explode('/', substr_replace($path_string, "", -1)); + $ref = &$array; + + while ($key = array_shift($keys)) { + $ref = &$ref[$key]; + } + $ref = array(); } - $time_start = microtime(true); - $replaced_array = $this->replaceWalker($assocarray, $newvalue, $field, $onlyseed); + /* + createDynamicallyObjects((object, Jsonstring, Array) : Structure, + (Array) : Array with the keys that will be created + ) - if($this->debug) { - $time_end = microtime(true); - $time = $time_end - $time_start; - $replaced_array["time"] = $time; + Create nested obj Dynamically if(key exist || key !exist), by $path_string + */ + public function createDynamicallyObjects(&$struct1, $keypath_array) { + if(!$this->studytype($struct1, $problem)) { + return $problem; + } + + $path_string = ""; + + for($i = 0 ; $i < count($keypath_array) ; $i++) { + $key = $keypath_array[$i]; + $path_string .= $key."/"; + $this->accesDynamically($path_string, $struct1); + } + $this->returnTypeConvert($struct1); } - $this->returnTypeConvert($replaced_array); + public function replaceValues($struct1, $newvalue, $field, $onlyseed) { + if(!$this->studytype($struct1, $problem)) { + return $problem; + } + $time_start = microtime(true); - return $replaced_array; + $replaced_array = $this->replaceWalker($struct1, $newvalue, $field, $onlyseed); - } + if($this->debug) { + $time_end = microtime(true); + $time = $time_end - $time_start; + $replaced_array["time"] = $time; + } - public function getdiff($struct1, $struct2) { + $this->returnTypeConvert($replaced_array); - if(!$this->studytype($struct1, $problem) || !$this->studytype($struct2, $problem)) { - return $problem; + return $replaced_array; } - $time_start = microtime(true); + public function getdiff($struct1, $struct2) { - $this->structPathArray($struct1, $structpath1_array, ""); + if(!$this->studytype($struct1, $problem) || !$this->studytype($struct2, $problem)) { + return $problem; + } - $this->structPathArray($struct2, $structpath2_array, ""); + $time_start = microtime(true); + $this->structPathArray($struct1, $structpath1_array, ""); + $this->structPathArray($struct2, $structpath2_array, ""); + $this->structPathArrayDiff($structpath1_array, $structpath2_array, $deltadiff_array); - $this->structPathArrayDiff($structpath1_array, $structpath2_array, $deltadiff_array); + if($this->debug) { + $time_end = microtime(true); + $time = $time_end - $time_start; + $deltadiff_array["time"] = $time; + } - if($this->debug) { - $time_end = microtime(true); - $time = $time_end - $time_start; - $deltadiff_array["time"] = $time; + $this->returnTypeConvert($deltadiff_array); + return $deltadiff_array; } - $this->returnTypeConvert($deltadiff_array); - - return $deltadiff_array; - } - - private function structPathArray($assocarray, &$array, $currentpath) { - - if(is_array($assocarray)) { + private function structPathArray($assocarray, &$array, $currentpath) { + if(is_array($assocarray)) { foreach ($assocarray as $key => $value) { - if(isset($assocarray[$key])) { - if($key != "_id") { - //Lógica 1 - $path = $currentpath ? $currentpath."/".$key : $key; - }else { - $path = $currentpath; - } - - if(gettype($assocarray[$key]) == "array") { - $this->structPathArray($assocarray[$key], $array, $path); - }else { - if($path != "") { + if(isset($assocarray[$key])) { + if($key != "_id") { //Lógica 1 - $array[$path] = $value; - } - } - } - } - } - } + $path = $currentpath ? $currentpath."/".$key : $key; + }else { + $path = $currentpath; + } - private function replaceWalker(&$assocarray, $newvalue, $field, $onlyseed) { + if(gettype($assocarray[$key]) == "array") { + $this->structPathArray($assocarray[$key], $array, $path); + }else { + if($path != "") { + //Lógica 1 + $array[$path] = $value; + } + } + } + } + } + } - if(is_array($assocarray)) { + private function replaceWalker(&$assocarray, $newvalue, $field, $onlyseed) { + if(is_array($assocarray)) { foreach ($assocarray as $key => &$value) { - if(isset($assocarray[$key])) { - if(is_array($assocarray[$key])) { - if(!$onlyseed) { - if($key == $field) { - $value = $newvalue; - } - } - $this->replaceWalker($assocarray[$key], $newvalue, $field, $onlyseed); - }else { - if(isset($newvalue)) { - if(isset($field)){ - if($key == $field) { - $value = $newvalue; + if(isset($assocarray[$key])) { + if(is_array($assocarray[$key])) { + if(!$onlyseed) { + if($key == $field) { + $value = $newvalue; + } + } + $this->replaceWalker($assocarray[$key], $newvalue, $field, $onlyseed); + }else { + if(isset($newvalue)) { + if(isset($field)){ + if($key == $field) { + $value = $newvalue; + } + }else { + $value = $newvalue; + } } - }else { - $value = $newvalue; - } } - } - } + } } - } - return $assocarray; - } + } + return $assocarray; + } - private function structPathArrayDiff($structpath1_array, $structpath2_array, &$deltadiff_array) { + private function structPathArrayDiff($structpath1_array, $structpath2_array, &$deltadiff_array) { - $deltadiff_array = array( + $deltadiff_array = array( "new" => array(), "removed" => array(), "edited" => array() - ); + ); - foreach($structpath1_array as $key1 => $value1) { + foreach($structpath1_array as $key1 => $value1) { if(array_key_exists($key1, $structpath2_array)) { - if($value1 != $structpath2_array[$key1]) { + if($value1 != $structpath2_array[$key1]) { - $edited = array( + $edited = array( "oldvalue" => $structpath2_array[$key1], "newvalue" => $value1 - ); - - $deltadiff_array["edited"][$key1] = $edited; - } + ); + $deltadiff_array["edited"][$key1] = $edited; + } }else { - $deltadiff_array["new"][$key1] = $value1; + $deltadiff_array["new"][$key1] = $value1; } - } + } - $removido = array_diff_key($structpath2_array, $structpath1_array); + $removido = array_diff_key($structpath2_array, $structpath1_array); /*print_r($structpath2_array); echo "----------------------"; print_r($structpath1_array);*/ - if(!empty($removido)) { + if(!empty($removido)) { foreach ($removido as $key => $value) { - $deltadiff_array["removed"][$key] = $value; + $deltadiff_array["removed"][$key] = $value; } - } - - //print_r($deltadiff_array); - } + } - private function returnTypeConvert(&$struct1) { + //print_r($deltadiff_array); + } + private function returnTypeConvert(&$struct1) { switch ($this->returntype) { - case 'jsonstring': - $struct1 = json_encode($struct1); - break; - - case 'obj': - $struct1 = json_decode(json_encode($struct1),false); - break; - - case 'array': - break; - - default: - return "returntype não é valido!"; - break; + case 'jsonstring': + $struct1 = json_encode($struct1); + break; + case 'obj': + $struct1 = json_decode(json_encode($struct1),false); + break; + case 'array': + break; + default: + return "returntype não é valido!"; + break; } - } - - private function studytype(&$struct1, &$problem) { + } - if($this->isJson_string($struct1)) { + private function studytype(&$struct1, &$problem) { + if($this->isJson_string($struct1)) { $struct1 = json_decode($struct1, true); return true; - }else { + }else { if(is_array($struct1)) { - return true; - }else { - if(is_object($struct1)) { return true; - }else{ - $problem = "comptype não é válido"; - return false; - } + }else { + if(is_object($struct1)) { + return true; + }else{ + $problem = "comptype não é válido"; + return false; + } } - } - } + } + } - private function isJson_string($string) { + private function isJson_string($string) { if(!is_string($string)) { - return false; + return false; }else { - return (json_last_error() == JSON_ERROR_NONE); + return (json_last_error() == JSON_ERROR_NONE); } - } - } + } +} ?>