forked from swaggest/json-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonPointer.php
305 lines (280 loc) · 9.79 KB
/
JsonPointer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
namespace Swaggest\JsonDiff;
class JsonPointer
{
/**
* Create intermediate keys if they don't exist
*/
const RECURSIVE_KEY_CREATION = 1;
/**
* Disallow converting empty array to object for key creation
*/
const STRICT_MODE = 2;
/**
* Skip action if holder already has a non-null value at path
*/
const SKIP_IF_ISSET = 4;
/**
* Allow associative arrays to mimic JSON objects (not recommended)
*/
const TOLERATE_ASSOCIATIVE_ARRAYS = 8;
/**
* @param string $key
* @param bool $isURIFragmentId
* @return string
*/
public static function escapeSegment($key, $isURIFragmentId = false)
{
if ($isURIFragmentId) {
return str_replace(array('%7E', '%2F'), array('~0', '~1'), urlencode($key));
} else {
return str_replace(array('~', '/'), array('~0', '~1'), $key);
}
}
/**
* @param string[] $pathItems
* @param bool $isURIFragmentId
* @return string
*/
public static function buildPath(array $pathItems, $isURIFragmentId = false)
{
$result = $isURIFragmentId ? '#' : '';
foreach ($pathItems as $pathItem) {
$result .= '/' . self::escapeSegment($pathItem, $isURIFragmentId);
}
return $result;
}
/**
* @param string $path
* @return string[]
* @throws Exception
*/
public static function splitPath($path)
{
$pathItems = explode('/', $path);
$first = array_shift($pathItems);
if ($first === '#') {
return self::splitPathURIFragment($pathItems);
} else {
if ($first !== '') {
throw new JsonPointerException('Path must start with "/": ' . $path);
}
return self::splitPathJsonString($pathItems);
}
}
private static function splitPathURIFragment(array $pathItems)
{
$result = array();
foreach ($pathItems as $key) {
$key = str_replace(array('~1', '~0'), array('/', '~'), urldecode($key));
$result[] = $key;
}
return $result;
}
private static function splitPathJsonString(array $pathItems)
{
$result = array();
foreach ($pathItems as $key) {
$key = str_replace(array('~1', '~0'), array('/', '~'), $key);
$result[] = $key;
}
return $result;
}
/**
* @param mixed $holder
* @param string[] $pathItems
* @param mixed $value
* @param int $flags
* @throws Exception
*/
public static function add(&$holder, $pathItems, $value, $flags = self::RECURSIVE_KEY_CREATION)
{
$ref = &$holder;
while (null !== $key = array_shift($pathItems)) {
if ($ref instanceof \stdClass || is_object($ref)) {
if (PHP_VERSION_ID < 70100 && '' === $key) {
throw new JsonPointerException('Empty property name is not supported by PHP <7.1',
Exception::EMPTY_PROPERTY_NAME_UNSUPPORTED);
}
if ($flags & self::RECURSIVE_KEY_CREATION) {
$ref = &$ref->$key;
} else {
if (!isset($ref->$key) && count($pathItems)) {
throw new JsonPointerException('Non-existent path item: ' . $key);
} else {
$ref = &$ref->$key;
}
}
} else { // null or array
$intKey = filter_var($key, FILTER_VALIDATE_INT);
if ($ref === null && (false === $intKey || $intKey !== 0)) {
$key = (string)$key;
if ($flags & self::RECURSIVE_KEY_CREATION) {
$ref = new \stdClass();
$ref = &$ref->{$key};
} else {
throw new JsonPointerException('Non-existent path item: ' . $key);
}
} elseif ([] === $ref && 0 === ($flags & self::STRICT_MODE) && false === $intKey && '-' !== $key) {
$ref = new \stdClass();
$ref = &$ref->{$key};
} else {
if ($flags & self::RECURSIVE_KEY_CREATION && $ref === null) $ref = array();
if ('-' === $key) {
$ref = &$ref[count($ref)];
} else {
if (false === $intKey) {
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
throw new JsonPointerException('Invalid key for array operation');
}
$ref = &$ref[$key];
continue;
}
if (is_array($ref) && array_key_exists($key, $ref) && empty($pathItems)) {
array_splice($ref, $intKey, 0, array($value));
}
if (0 === ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS)) {
if ($intKey > count($ref) && 0 === ($flags & self::RECURSIVE_KEY_CREATION)) {
throw new JsonPointerException('Index is greater than number of items in array');
} elseif ($intKey < 0) {
throw new JsonPointerException('Negative index');
}
}
$ref = &$ref[$intKey];
}
}
}
}
if ($ref !== null && $flags & self::SKIP_IF_ISSET) {
return;
}
$ref = $value;
}
private static function arrayKeyExists($key, array $a)
{
if (array_key_exists($key, $a)) {
return true;
}
$key = (string)$key;
foreach ($a as $k => $v) {
if ((string)$k === $key) {
return true;
}
}
return false;
}
private static function arrayGet($key, array $a)
{
$key = (string)$key;
foreach ($a as $k => $v) {
if ((string)$k === $key) {
return $v;
}
}
return false;
}
/**
* @param mixed $holder
* @param string[] $pathItems
* @return bool|mixed
* @throws Exception
*/
public static function get($holder, $pathItems)
{
$ref = $holder;
while (null !== $key = array_shift($pathItems)) {
if ($ref instanceof \stdClass) {
if (PHP_VERSION_ID < 70100 && '' === $key) {
throw new JsonPointerException('Empty property name is not supported by PHP <7.1',
Exception::EMPTY_PROPERTY_NAME_UNSUPPORTED);
}
$vars = (array)$ref;
if (self::arrayKeyExists($key, $vars)) {
$ref = self::arrayGet($key, $vars);
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
} elseif (is_array($ref)) {
if (self::arrayKeyExists($key, $ref)) {
$ref = $ref[$key];
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
} elseif (is_object($ref)) {
if (isset($ref->$key)) {
$ref = $ref->$key;
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
}
return $ref;
}
/**
* @param mixed $holder
* @param string $pointer
* @return bool|mixed
* @throws Exception
*/
public static function getByPointer($holder, $pointer)
{
return self::get($holder, self::splitPath($pointer));
}
/**
* @param mixed $holder
* @param string[] $pathItems
* @param int $flags
* @return mixed
* @throws Exception
*/
public static function remove(&$holder, $pathItems, $flags = 0)
{
$ref = &$holder;
while (null !== $key = array_shift($pathItems)) {
$parent = &$ref;
$refKey = $key;
if ($ref instanceof \stdClass) {
if (property_exists($ref, $key)) {
$ref = &$ref->$key;
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
} elseif (is_object($ref)) {
if (isset($ref->$key)) {
$ref = &$ref->$key;
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
} else {
if (array_key_exists($key, $ref)) {
$ref = &$ref[$key];
} else {
throw new JsonPointerException('Key not found: ' . $key);
}
}
}
if (isset($parent) && isset($refKey)) {
if ($parent instanceof \stdClass || is_object($parent)) {
unset($parent->$refKey);
} else {
$isAssociative = false;
if ($flags & self::TOLERATE_ASSOCIATIVE_ARRAYS) {
$i = 0;
foreach ($parent as $index => $value) {
if ($i !== $index) {
$isAssociative = true;
break;
}
}
}
unset($parent[$refKey]);
if (!$isAssociative && (int)$refKey !== count($parent)) {
$parent = array_values($parent);
}
}
}
return $ref;
}
}