diff --git a/README.md b/README.md index 201eab6..74e3739 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,9 @@ Returns modifications as partial value of original. #### `getModifiedNew` Returns modifications as partial value of new. +#### `getModifiedDiff` +Returns list of paths with original and new values. + #### `getModifiedPaths` Returns list of `JSON` paths that were modified from original to new. diff --git a/src/JsonDiff.php b/src/JsonDiff.php index 9b7e0b0..33ac99e 100644 --- a/src/JsonDiff.php +++ b/src/JsonDiff.php @@ -61,6 +61,7 @@ class JsonDiff private $modifiedNew; private $modifiedCnt = 0; private $modifiedPaths = array(); + private $modifiedDiff = array(); private $path = ''; private $pathItems = array(); @@ -195,6 +196,15 @@ public function getModifiedPaths() return $this->modifiedPaths; } + /** + * Returns list of paths with original and new values. + * @return array + */ + public function getModifiedDiff() + { + return $this->modifiedDiff; + } + /** * Returns new value, rearranged with original order. * @return array|object @@ -273,6 +283,12 @@ private function process($original, $new) if ($merge) { JsonPointer::add($this->merge, $this->pathItems, $new, JsonPointer::RECURSIVE_KEY_CREATION); } + + $this->modifiedDiff[] = [ + 'path' => $this->path, + 'original' => $original, + 'new' => $new, + ]; } return $new; } diff --git a/tests/src/RearrangeTest.php b/tests/src/RearrangeTest.php index 660982f..d5d36ff 100644 --- a/tests/src/RearrangeTest.php +++ b/tests/src/RearrangeTest.php @@ -87,6 +87,24 @@ public function testKeepOrder() $this->assertSame('{"key1":[4],"key3":{"sub1":"a","sub2":"b"}}', json_encode($r->getModifiedOriginal())); $this->assertSame('{"key1":[5],"key3":{"sub1":"c","sub2":false}}', json_encode($r->getModifiedNew())); + + $this->assertSame([ + [ + 'path' => '/key1/0', + 'original' => 4, + 'new' => 5, + ], + [ + 'path' => '/key3/sub1', + 'original' => 'a', + 'new' => 'c', + ], + [ + 'path' => '/key3/sub2', + 'original' => 'b', + 'new' => false, + ], + ], $r->getModifiedDiff()); }