Skip to content
Snippets Groups Projects
Unverified Commit 05e61270 authored by Viacheslav Poturaev's avatar Viacheslav Poturaev Committed by GitHub
Browse files

Merge pull request #12 from swaggest/skip_if_isset

option to skip overwriting value if it is set
parents 8230d847 c9719f6b
Branches
Tags v3.4.2
No related merge requests found
......@@ -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
......@@ -143,6 +148,9 @@ class JsonPointer
}
}
}
if ($ref !== null && $flags & self::SKIP_IF_ISSET) {
return;
}
$ref = $value;
}
......
......@@ -17,6 +17,15 @@ class JsonPointerTest extends \PHPUnit_Framework_TestCase
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 {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment