Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class JsonPointer
*/
const STRICT_MODE = 2;

/**
* Skip action if holder already has a non-null value at path
*/
const SKIP_IF_ISSET = 4;

/**
* @param string $key
* @param bool $isURIFragmentId
Expand Down Expand Up @@ -143,6 +148,9 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
}
}
}
if ($ref !== null && $flags & self::SKIP_IF_ISSET) {
return;
}
$ref = $value;
}

Expand Down
9 changes: 9 additions & 0 deletions tests/src/JsonPointerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public function testProcess()
JsonPointer::add($json, ['l1','l2','l3'], 'hello!');
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));

JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!', JsonPointer::SKIP_IF_ISSET);
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));

JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!');
$this->assertSame('{"l1":{"l2":{"l3":"hello again!"}}}', json_encode($json));

JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello!');
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));

$this->assertSame('{"l3":"hello!"}', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2'))));

try {
Expand Down