Skip to content

Commit e452a9c

Browse files
authored
Avoid associative array conversion when removing array items (#32)
1 parent fccbbd6 commit e452a9c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/JsonPointer.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
134134
} else {
135135
if ($flags & self::RECURSIVE_KEY_CREATION && $ref === null) $ref = array();
136136
if ('-' === $key) {
137-
$ref = &$ref[];
137+
$ref = &$ref[count($ref)];
138138
} else {
139139
if (false === $intKey) {
140140
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
@@ -282,7 +282,6 @@ public static function remove(&$holder, $pathItems, $flags = 0)
282282
unset($parent->$refKey);
283283
} else {
284284
$isAssociative = false;
285-
$ff = $flags & self::TOLERATE_ASSOCIATIVE_ARRAYS;
286285
if ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS) {
287286
$i = 0;
288287
foreach ($parent as $index => $value) {

tests/src/Issues/Issue31Test.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff\Tests\Issues;
4+
5+
use Swaggest\JsonDiff\JsonPatch;
6+
7+
class Issue31Test extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testIssue()
10+
{
11+
$reportData = json_decode('{}');
12+
$patch = JsonPatch::import(json_decode('[{"op":"add","path":"","value":["a","b","c","d"]},{"op":"remove","path":"\/3","value":""},{"op":"add","path":"\/-","value":"e"}]'));
13+
$patch->apply($reportData);
14+
15+
$this->assertSame('["a","b","c","e"]', json_encode($reportData));
16+
}
17+
18+
}

0 commit comments

Comments
 (0)