Skip to content

Commit 05e6127

Browse files
authoredApr 28, 2018
Merge pull request #12 from swaggest/skip_if_isset
option to skip overwriting value if it is set
2 parents 8230d84 + c9719f6 commit 05e6127

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed
 

‎src/JsonPointer.php

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class JsonPointer
1515
*/
1616
const STRICT_MODE = 2;
1717

18+
/**
19+
* Skip action if holder already has a non-null value at path
20+
*/
21+
const SKIP_IF_ISSET = 4;
22+
1823
/**
1924
* @param string $key
2025
* @param bool $isURIFragmentId
@@ -143,6 +148,9 @@ public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIV
143148
}
144149
}
145150
}
151+
if ($ref !== null && $flags & self::SKIP_IF_ISSET) {
152+
return;
153+
}
146154
$ref = $value;
147155
}
148156

‎tests/src/JsonPointerTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ public function testProcess()
1717
JsonPointer::add($json, ['l1','l2','l3'], 'hello!');
1818
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
1919

20+
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!', JsonPointer::SKIP_IF_ISSET);
21+
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
22+
23+
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!');
24+
$this->assertSame('{"l1":{"l2":{"l3":"hello again!"}}}', json_encode($json));
25+
26+
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello!');
27+
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
28+
2029
$this->assertSame('{"l3":"hello!"}', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2'))));
2130

2231
try {

0 commit comments

Comments
 (0)
Please sign in to comment.