Skip to content

Commit

Permalink
Merge pull request #22 from swaggest/modified-diff-flag
Browse files Browse the repository at this point in the history
Change modified diff collection behavior
  • Loading branch information
vearutop authored Apr 25, 2019
2 parents 2f51b1a + d9308e3 commit b997a29
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 14 additions & 7 deletions src/JsonDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -61,6 +67,9 @@ class JsonDiff
private $modifiedNew;
private $modifiedCnt = 0;
private $modifiedPaths = array();
/**
* @var ModifiedPathDiff[]
*/
private $modifiedDiff = array();

private $path = '';
Expand Down Expand Up @@ -198,7 +207,7 @@ public function getModifiedPaths()

/**
* Returns list of paths with original and new values.
* @return array
* @return ModifiedPathDiff[]
*/
public function getModifiedDiff()
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down
30 changes: 30 additions & 0 deletions src/ModifiedPathDiff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace Swaggest\JsonDiff;


class ModifiedPathDiff
{
public function __construct($path, $original, $new)
{
$this->path = $path;
$this->original = $original;
$this->new = $new;
}

/**
* @var string
*/
public $path;

/**
* @var mixed
*/
public $original;

/**
* @var mixed
*/
public $new;
}
23 changes: 6 additions & 17 deletions tests/src/RearrangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Swaggest\JsonDiff\JsonDiff;
use Swaggest\JsonDiff\JsonPatch;
use Swaggest\JsonDiff\ModifiedPathDiff;

class RearrangeTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit b997a29

Please sign in to comment.