diff --git a/README.md b/README.md index 74e3739..7544cbe 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,9 @@ Returns modifications as partial value of original. Returns modifications as partial value of new. #### `getModifiedDiff` -Returns list of paths with original and new values. +Returns list of `ModifiedPathDiff` containing paths with original and new values. + +Not collected by default, requires `JsonDiff::COLLECT_MODIFIED_DIFF` option. #### `getModifiedPaths` Returns list of `JSON` paths that were modified from original to new. diff --git a/src/JsonDiff.php b/src/JsonDiff.php index 33ac99e..327f29b 100644 --- a/src/JsonDiff.php +++ b/src/JsonDiff.php @@ -40,6 +40,12 @@ class JsonDiff */ const TOLERATE_ASSOCIATIVE_ARRAYS = 32; + /** + * COLLECT_MODIFIED_DIFF is an option to enable getModifiedDiff. + */ + const COLLECT_MODIFIED_DIFF = 64; + + private $options = 0; private $original; private $new; @@ -61,6 +67,9 @@ class JsonDiff private $modifiedNew; private $modifiedCnt = 0; private $modifiedPaths = array(); + /** + * @var ModifiedPathDiff[] + */ private $modifiedDiff = array(); private $path = ''; @@ -198,7 +207,7 @@ public function getModifiedPaths() /** * Returns list of paths with original and new values. - * @return array + * @return ModifiedPathDiff[] */ public function getModifiedDiff() { @@ -284,11 +293,9 @@ private function process($original, $new) JsonPointer::add($this->merge, $this->pathItems, $new, JsonPointer::RECURSIVE_KEY_CREATION); } - $this->modifiedDiff[] = [ - 'path' => $this->path, - 'original' => $original, - 'new' => $new, - ]; + if ($this->options & self::COLLECT_MODIFIED_DIFF) { + $this->modifiedDiff[] = new ModifiedPathDiff($this->path, $original, $new); + } } return $new; } @@ -310,7 +317,7 @@ private function process($original, $new) if ($merge && is_array($new) && !is_array($original)) { $merge = false; JsonPointer::add($this->merge, $this->pathItems, $new); - } elseif ($merge && $new instanceof \stdClass && !$original instanceof \stdClass) { + } elseif ($merge && $new instanceof \stdClass && !$original instanceof \stdClass) { $merge = false; JsonPointer::add($this->merge, $this->pathItems, $new); } diff --git a/src/ModifiedPathDiff.php b/src/ModifiedPathDiff.php new file mode 100644 index 0000000..c981e94 --- /dev/null +++ b/src/ModifiedPathDiff.php @@ -0,0 +1,30 @@ +path = $path; + $this->original = $original; + $this->new = $new; + } + + /** + * @var string + */ + public $path; + + /** + * @var mixed + */ + public $original; + + /** + * @var mixed + */ + public $new; +} \ No newline at end of file diff --git a/tests/src/RearrangeTest.php b/tests/src/RearrangeTest.php index d5d36ff..b3983d2 100644 --- a/tests/src/RearrangeTest.php +++ b/tests/src/RearrangeTest.php @@ -5,6 +5,7 @@ use Swaggest\JsonDiff\JsonDiff; use Swaggest\JsonDiff\JsonPatch; +use Swaggest\JsonDiff\ModifiedPathDiff; class RearrangeTest extends \PHPUnit_Framework_TestCase { @@ -58,7 +59,7 @@ public function testKeepOrder() } JSON; - $r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS); + $r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS + JsonDiff::COLLECT_MODIFIED_DIFF); $this->assertSame( json_encode(json_decode($expected), JSON_PRETTY_PRINT), json_encode($r->getRearranged(), JSON_PRETTY_PRINT) @@ -88,22 +89,10 @@ 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, - ], + $this->assertEquals([ + new ModifiedPathDiff('/key1/0', 4, 5), + new ModifiedPathDiff('/key3/sub1', 'a', 'c'), + new ModifiedPathDiff('/key3/sub2', 'b', false), ], $r->getModifiedDiff()); }