Skip to content

Commit 2f51b1a

Browse files
authored
Merge pull request #19 from atomatis/feature/add_full_modified_property
Add modifier full diff property
2 parents de18f54 + c952788 commit 2f51b1a

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ Returns modifications as partial value of original.
101101
#### `getModifiedNew`
102102
Returns modifications as partial value of new.
103103

104+
#### `getModifiedDiff`
105+
Returns list of paths with original and new values.
106+
104107
#### `getModifiedPaths`
105108
Returns list of `JSON` paths that were modified from original to new.
106109

src/JsonDiff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class JsonDiff
6161
private $modifiedNew;
6262
private $modifiedCnt = 0;
6363
private $modifiedPaths = array();
64+
private $modifiedDiff = array();
6465

6566
private $path = '';
6667
private $pathItems = array();
@@ -195,6 +196,15 @@ public function getModifiedPaths()
195196
return $this->modifiedPaths;
196197
}
197198

199+
/**
200+
* Returns list of paths with original and new values.
201+
* @return array
202+
*/
203+
public function getModifiedDiff()
204+
{
205+
return $this->modifiedDiff;
206+
}
207+
198208
/**
199209
* Returns new value, rearranged with original order.
200210
* @return array|object
@@ -273,6 +283,12 @@ private function process($original, $new)
273283
if ($merge) {
274284
JsonPointer::add($this->merge, $this->pathItems, $new, JsonPointer::RECURSIVE_KEY_CREATION);
275285
}
286+
287+
$this->modifiedDiff[] = [
288+
'path' => $this->path,
289+
'original' => $original,
290+
'new' => $new,
291+
];
276292
}
277293
return $new;
278294
}

tests/src/RearrangeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ public function testKeepOrder()
8787

8888
$this->assertSame('{"key1":[4],"key3":{"sub1":"a","sub2":"b"}}', json_encode($r->getModifiedOriginal()));
8989
$this->assertSame('{"key1":[5],"key3":{"sub1":"c","sub2":false}}', json_encode($r->getModifiedNew()));
90+
91+
$this->assertSame([
92+
[
93+
'path' => '/key1/0',
94+
'original' => 4,
95+
'new' => 5,
96+
],
97+
[
98+
'path' => '/key3/sub1',
99+
'original' => 'a',
100+
'new' => 'c',
101+
],
102+
[
103+
'path' => '/key3/sub2',
104+
'original' => 'b',
105+
'new' => false,
106+
],
107+
], $r->getModifiedDiff());
90108
}
91109

92110

0 commit comments

Comments
 (0)