Skip to content

Commit 74a3040

Browse files
authored
Merge pull request #3 from aachekalov/reverse_remove_indexes
Reverse remove indexes
2 parents cb86558 + eedca61 commit 74a3040

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/JsonDiff.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ private function process($original, $new)
306306

307307
$originalKeys = $original instanceof \stdClass ? get_object_vars($original) : $original;
308308
$isArray = is_array($original);
309-
$removedOffset = 0;
310309

311310
if ($merge && is_array($new) && !is_array($original)) {
312311
$merge = false;
@@ -318,6 +317,9 @@ private function process($original, $new)
318317

319318
$isUriFragment = (bool)($this->options & self::JSON_URI_FRAGMENT_ID);
320319
$diffCnt = $this->addedCnt + $this->modifiedCnt + $this->removedCnt;
320+
if ($isArray) {
321+
$originalKeys = array_reverse($originalKeys, true);
322+
}
321323
foreach ($originalKeys as $key => $originalValue) {
322324
if ($this->options & self::STOP_ON_DIFF) {
323325
if ($this->modifiedCnt || $this->addedCnt || $this->removedCnt) {
@@ -327,12 +329,8 @@ private function process($original, $new)
327329

328330
$path = $this->path;
329331
$pathItems = $this->pathItems;
330-
$actualKey = $key;
331-
if ($isArray && is_int($actualKey)) {
332-
$actualKey -= $removedOffset;
333-
}
334-
$this->path .= '/' . JsonPointer::escapeSegment((string)$actualKey, $isUriFragment);
335-
$this->pathItems[] = $actualKey;
332+
$this->path .= '/' . JsonPointer::escapeSegment((string)$key, $isUriFragment);
333+
$this->pathItems[] = $key;
336334

337335
if (array_key_exists($key, $newArray)) {
338336
$newOrdered[$key] = $this->process($originalValue, $newArray[$key]);
@@ -343,9 +341,6 @@ private function process($original, $new)
343341
return null;
344342
}
345343
$this->removedPaths [] = $this->path;
346-
if ($isArray) {
347-
$removedOffset++;
348-
}
349344

350345
if ($this->jsonPatch !== null) {
351346
$this->jsonPatch->op(new Remove($this->path));

0 commit comments

Comments
 (0)