Skip to content

Commit e1d0f04

Browse files
committed
#6 renumerating arrays after item removal
1 parent e9e947d commit e1d0f04

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed

src/JsonDiff.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,28 @@ private function process($original, $new)
220220
$newOrdered = array();
221221

222222
$originalKeys = $original instanceof \stdClass ? get_object_vars($original) : $original;
223+
$isArray = is_array($original);
224+
$removedOffset = 0;
223225

224226
foreach ($originalKeys as $key => $originalValue) {
225227
$path = $this->path;
226228
$pathItems = $this->pathItems;
227-
$this->path .= '/' . JsonPointer::escapeSegment($key, $this->options & self::JSON_URI_FRAGMENT_ID);
228-
$this->pathItems[] = $key;
229+
$actualKey = $key;
230+
if ($isArray) {
231+
$actualKey -= $removedOffset;
232+
}
233+
$this->path .= '/' . JsonPointer::escapeSegment($actualKey, $this->options & self::JSON_URI_FRAGMENT_ID);
234+
$this->pathItems[] = $actualKey;
229235

230236
if (array_key_exists($key, $newArray)) {
231237
$newOrdered[$key] = $this->process($originalValue, $newArray[$key]);
232238
unset($newArray[$key]);
233239
} else {
234240
$this->removedCnt++;
235241
$this->removedPaths [] = $this->path;
242+
if ($isArray) {
243+
$removedOffset++;
244+
}
236245

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

src/JsonPointer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ public static function remove(&$holder, $pathItems)
159159
unset($parent->$refKey);
160160
} else {
161161
unset($parent[$refKey]);
162+
if ($refKey !== count($parent)) {
163+
$parent = array_values($parent);
164+
}
162165
}
163166
}
164167
return $ref;

tests/src/Issues/Issue6Test.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class Issue6Test extends \PHPUnit_Framework_TestCase
1313
{
14-
public function testIssue6()
14+
public function testDefault()
1515
{
1616
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
1717
$json2 = json_decode('[{"name":"b"}]');
@@ -37,7 +37,7 @@ public function testIssue6()
3737
},
3838
{
3939
"op": "remove",
40-
"path": "/2"
40+
"path": "/1"
4141
}
4242
]
4343
JSON
@@ -49,21 +49,42 @@ public function testIssue6()
4949
$this->assertEquals($json2, $json1a);
5050
}
5151

52+
public function testOriginal()
53+
{
54+
$originalJson = '[{"name":"a"},{"name":"b"},{"name":"c"}]';
55+
$newJson = '[{"name":"b"}]';
56+
$diff = new JsonDiff(json_decode($originalJson), json_decode($newJson));
57+
58+
$patchJson = json_decode(json_encode($diff->getPatch()->jsonSerialize()), true);
59+
60+
$original = json_decode($originalJson);
61+
$patch = JsonPatch::import($patchJson);
62+
$patch->apply($original);
63+
$this->assertEquals($original, json_decode($newJson));
64+
}
65+
66+
67+
public function testDoubleInverseRemove()
68+
{
69+
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
70+
$json2 = json_decode('[{"name":"b"}]');
71+
72+
$patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/2"},{"op":"remove","path":"/0"}]'));
73+
74+
$json1a = $json1;
75+
$patch->apply($json1a);
76+
$this->assertEquals(json_encode($json2), json_encode($json1a));
77+
}
5278

53-
public function testIssue6Remove()
79+
public function testDoubleRemove()
5480
{
5581
$json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
5682
$json2 = json_decode('[{"name":"b"}]');
5783

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

6086
$json1a = $json1;
6187
$patch->apply($json1a);
6288
$this->assertEquals(json_encode($json2), json_encode($json1a));
63-
/*
64-
Failed asserting that two strings are equal.
65-
Expected :'[{"name":"b"}]'
66-
Actual :'{"1":{"name":"b"}}'
67-
*/
6889
}
6990
}

0 commit comments

Comments
 (0)