Skip to content

Commit c952788

Browse files
author
Alexandre Tomatis
committed
Add modifier full property
[JsonDiff] add $modifiedFull + getter
1 parent dadb65f commit c952788

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
@@ -100,6 +100,9 @@ Returns modifications as partial value of original.
100100
#### `getModifiedNew`
101101
Returns modifications as partial value of new.
102102

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

src/JsonDiff.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class JsonDiff
5656
private $modifiedNew;
5757
private $modifiedCnt = 0;
5858
private $modifiedPaths = array();
59+
private $modifiedDiff = array();
5960

6061
private $path = '';
6162
private $pathItems = array();
@@ -190,6 +191,15 @@ public function getModifiedPaths()
190191
return $this->modifiedPaths;
191192
}
192193

194+
/**
195+
* Returns list of paths with original and new values.
196+
* @return array
197+
*/
198+
public function getModifiedDiff()
199+
{
200+
return $this->modifiedDiff;
201+
}
202+
193203
/**
194204
* Returns new value, rearranged with original order.
195205
* @return array|object
@@ -258,6 +268,12 @@ private function process($original, $new)
258268
if ($merge) {
259269
JsonPointer::add($this->merge, $this->pathItems, $new, JsonPointer::RECURSIVE_KEY_CREATION);
260270
}
271+
272+
$this->modifiedDiff[] = [
273+
'path' => $this->path,
274+
'original' => $original,
275+
'new' => $new,
276+
];
261277
}
262278
return $new;
263279
}

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)