Skip to content

Commit

Permalink
#6 renumerating arrays after item removal
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Feb 9, 2018
1 parent e9e947d commit e1d0f04
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/JsonDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,19 +220,28 @@ private function process($original, $new)
$newOrdered = array();

$originalKeys = $original instanceof \stdClass ? get_object_vars($original) : $original;
$isArray = is_array($original);
$removedOffset = 0;

foreach ($originalKeys as $key => $originalValue) {
$path = $this->path;
$pathItems = $this->pathItems;
$this->path .= '/' . JsonPointer::escapeSegment($key, $this->options & self::JSON_URI_FRAGMENT_ID);
$this->pathItems[] = $key;
$actualKey = $key;
if ($isArray) {
$actualKey -= $removedOffset;
}
$this->path .= '/' . JsonPointer::escapeSegment($actualKey, $this->options & self::JSON_URI_FRAGMENT_ID);
$this->pathItems[] = $actualKey;

if (array_key_exists($key, $newArray)) {
$newOrdered[$key] = $this->process($originalValue, $newArray[$key]);
unset($newArray[$key]);
} else {
$this->removedCnt++;
$this->removedPaths [] = $this->path;
if ($isArray) {
$removedOffset++;
}

$this->jsonPatch->op(new Remove($this->path));

Expand Down
3 changes: 3 additions & 0 deletions src/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ public static function remove(&$holder, $pathItems)
unset($parent->$refKey);
} else {
unset($parent[$refKey]);
if ($refKey !== count($parent)) {
$parent = array_values($parent);
}
}
}
return $ref;
Expand Down
39 changes: 30 additions & 9 deletions tests/src/Issues/Issue6Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class Issue6Test extends \PHPUnit_Framework_TestCase
{
public function testIssue6()
public function testDefault()
{
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
$json2 = json_decode('[{"name":"b"}]');
Expand All @@ -37,7 +37,7 @@ public function testIssue6()
},
{
"op": "remove",
"path": "/2"
"path": "/1"
}
]
JSON
Expand All @@ -49,21 +49,42 @@ public function testIssue6()
$this->assertEquals($json2, $json1a);
}

public function testOriginal()
{
$originalJson = '[{"name":"a"},{"name":"b"},{"name":"c"}]';
$newJson = '[{"name":"b"}]';
$diff = new JsonDiff(json_decode($originalJson), json_decode($newJson));

$patchJson = json_decode(json_encode($diff->getPatch()->jsonSerialize()), true);

$original = json_decode($originalJson);
$patch = JsonPatch::import($patchJson);
$patch->apply($original);
$this->assertEquals($original, json_decode($newJson));
}


public function testDoubleInverseRemove()
{
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
$json2 = json_decode('[{"name":"b"}]');

$patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/2"},{"op":"remove","path":"/0"}]'));

$json1a = $json1;
$patch->apply($json1a);
$this->assertEquals(json_encode($json2), json_encode($json1a));
}

public function testIssue6Remove()
public function testDoubleRemove()
{
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
$json2 = json_decode('[{"name":"b"}]');

$patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/0"},{"op":"remove","path":"/2"}]'));
$patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/0"},{"op":"remove","path":"/1"}]'));

$json1a = $json1;
$patch->apply($json1a);
$this->assertEquals(json_encode($json2), json_encode($json1a));
/*
Failed asserting that two strings are equal.
Expected :'[{"name":"b"}]'
Actual :'{"1":{"name":"b"}}'
*/
}
}

0 comments on commit e1d0f04

Please sign in to comment.