Skip to content

Commit b997a29

Browse files
authored
Merge pull request #22 from swaggest/modified-diff-flag
Change modified diff collection behavior
2 parents 2f51b1a + d9308e3 commit b997a29

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ Returns modifications as partial value of original.
102102
Returns modifications as partial value of new.
103103

104104
#### `getModifiedDiff`
105-
Returns list of paths with original and new values.
105+
Returns list of `ModifiedPathDiff` containing paths with original and new values.
106+
107+
Not collected by default, requires `JsonDiff::COLLECT_MODIFIED_DIFF` option.
106108

107109
#### `getModifiedPaths`
108110
Returns list of `JSON` paths that were modified from original to new.

src/JsonDiff.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ class JsonDiff
4040
*/
4141
const TOLERATE_ASSOCIATIVE_ARRAYS = 32;
4242

43+
/**
44+
* COLLECT_MODIFIED_DIFF is an option to enable getModifiedDiff.
45+
*/
46+
const COLLECT_MODIFIED_DIFF = 64;
47+
48+
4349
private $options = 0;
4450
private $original;
4551
private $new;
@@ -61,6 +67,9 @@ class JsonDiff
6167
private $modifiedNew;
6268
private $modifiedCnt = 0;
6369
private $modifiedPaths = array();
70+
/**
71+
* @var ModifiedPathDiff[]
72+
*/
6473
private $modifiedDiff = array();
6574

6675
private $path = '';
@@ -198,7 +207,7 @@ public function getModifiedPaths()
198207

199208
/**
200209
* Returns list of paths with original and new values.
201-
* @return array
210+
* @return ModifiedPathDiff[]
202211
*/
203212
public function getModifiedDiff()
204213
{
@@ -284,11 +293,9 @@ private function process($original, $new)
284293
JsonPointer::add($this->merge, $this->pathItems, $new, JsonPointer::RECURSIVE_KEY_CREATION);
285294
}
286295

287-
$this->modifiedDiff[] = [
288-
'path' => $this->path,
289-
'original' => $original,
290-
'new' => $new,
291-
];
296+
if ($this->options & self::COLLECT_MODIFIED_DIFF) {
297+
$this->modifiedDiff[] = new ModifiedPathDiff($this->path, $original, $new);
298+
}
292299
}
293300
return $new;
294301
}
@@ -310,7 +317,7 @@ private function process($original, $new)
310317
if ($merge && is_array($new) && !is_array($original)) {
311318
$merge = false;
312319
JsonPointer::add($this->merge, $this->pathItems, $new);
313-
} elseif ($merge && $new instanceof \stdClass && !$original instanceof \stdClass) {
320+
} elseif ($merge && $new instanceof \stdClass && !$original instanceof \stdClass) {
314321
$merge = false;
315322
JsonPointer::add($this->merge, $this->pathItems, $new);
316323
}

src/ModifiedPathDiff.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
4+
namespace Swaggest\JsonDiff;
5+
6+
7+
class ModifiedPathDiff
8+
{
9+
public function __construct($path, $original, $new)
10+
{
11+
$this->path = $path;
12+
$this->original = $original;
13+
$this->new = $new;
14+
}
15+
16+
/**
17+
* @var string
18+
*/
19+
public $path;
20+
21+
/**
22+
* @var mixed
23+
*/
24+
public $original;
25+
26+
/**
27+
* @var mixed
28+
*/
29+
public $new;
30+
}

tests/src/RearrangeTest.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Swaggest\JsonDiff\JsonDiff;
77
use Swaggest\JsonDiff\JsonPatch;
8+
use Swaggest\JsonDiff\ModifiedPathDiff;
89

910
class RearrangeTest extends \PHPUnit_Framework_TestCase
1011
{
@@ -58,7 +59,7 @@ public function testKeepOrder()
5859
}
5960
JSON;
6061

61-
$r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS);
62+
$r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS + JsonDiff::COLLECT_MODIFIED_DIFF);
6263
$this->assertSame(
6364
json_encode(json_decode($expected), JSON_PRETTY_PRINT),
6465
json_encode($r->getRearranged(), JSON_PRETTY_PRINT)
@@ -88,22 +89,10 @@ public function testKeepOrder()
8889
$this->assertSame('{"key1":[4],"key3":{"sub1":"a","sub2":"b"}}', json_encode($r->getModifiedOriginal()));
8990
$this->assertSame('{"key1":[5],"key3":{"sub1":"c","sub2":false}}', json_encode($r->getModifiedNew()));
9091

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-
],
92+
$this->assertEquals([
93+
new ModifiedPathDiff('/key1/0', 4, 5),
94+
new ModifiedPathDiff('/key3/sub1', 'a', 'c'),
95+
new ModifiedPathDiff('/key3/sub2', 'b', false),
10796
], $r->getModifiedDiff());
10897
}
10998

0 commit comments

Comments
 (0)