Skip to content

Commit

Permalink
Avoid associative array conversion when removing array items (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored May 26, 2020
1 parent fccbbd6 commit e452a9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
} else {
if ($flags & self::RECURSIVE_KEY_CREATION && $ref === null) $ref = array();
if ('-' === $key) {
$ref = &$ref[];
$ref = &$ref[count($ref)];
} else {
if (false === $intKey) {
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
Expand Down Expand Up @@ -282,7 +282,6 @@ public static function remove(&$holder, $pathItems, $flags = 0)
unset($parent->$refKey);
} else {
$isAssociative = false;
$ff = $flags & self::TOLERATE_ASSOCIATIVE_ARRAYS;
if ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS) {
$i = 0;
foreach ($parent as $index => $value) {
Expand Down
18 changes: 18 additions & 0 deletions tests/src/Issues/Issue31Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Swaggest\JsonDiff\Tests\Issues;

use Swaggest\JsonDiff\JsonPatch;

class Issue31Test extends \PHPUnit_Framework_TestCase
{
public function testIssue()
{
$reportData = json_decode('{}');
$patch = JsonPatch::import(json_decode('[{"op":"add","path":"","value":["a","b","c","d"]},{"op":"remove","path":"\/3","value":""},{"op":"add","path":"\/-","value":"e"}]'));
$patch->apply($reportData);

$this->assertSame('["a","b","c","e"]', json_encode($reportData));
}

}

0 comments on commit e452a9c

Please sign in to comment.