Skip to content

Commit

Permalink
Merge pull request #19 from atomatis/feature/add_full_modified_property
Browse files Browse the repository at this point in the history
Add modifier full diff property
  • Loading branch information
vearutop authored Apr 24, 2019
2 parents de18f54 + c952788 commit 2f51b1a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ Returns modifications as partial value of original.
#### `getModifiedNew`
Returns modifications as partial value of new.

#### `getModifiedDiff`
Returns list of paths with original and new values.

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

Expand Down
16 changes: 16 additions & 0 deletions src/JsonDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class JsonDiff
private $modifiedNew;
private $modifiedCnt = 0;
private $modifiedPaths = array();
private $modifiedDiff = array();

private $path = '';
private $pathItems = array();
Expand Down Expand Up @@ -195,6 +196,15 @@ public function getModifiedPaths()
return $this->modifiedPaths;
}

/**
* Returns list of paths with original and new values.
* @return array
*/
public function getModifiedDiff()
{
return $this->modifiedDiff;
}

/**
* Returns new value, rearranged with original order.
* @return array|object
Expand Down Expand Up @@ -273,6 +283,12 @@ private function process($original, $new)
if ($merge) {
JsonPointer::add($this->merge, $this->pathItems, $new, JsonPointer::RECURSIVE_KEY_CREATION);
}

$this->modifiedDiff[] = [
'path' => $this->path,
'original' => $original,
'new' => $new,
];
}
return $new;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/src/RearrangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ 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,
],
], $r->getModifiedDiff());
}


Expand Down

0 comments on commit 2f51b1a

Please sign in to comment.