|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\JsonDiff; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class JsonProcessor |
|
7
|
|
|
{ |
|
8
|
|
|
public static function pushByPath(&$holder, $path, $value) |
|
9
|
|
|
{ |
|
10
|
|
|
$pathItems = explode('/', $path); |
|
11
|
|
|
if ('#' === $pathItems[0]) { |
|
12
|
|
|
array_shift($pathItems); |
|
13
|
|
|
} |
|
14
|
|
|
$ref = &$holder; |
|
15
|
|
|
while (null !== $key = array_shift($pathItems)) { |
|
16
|
|
|
if (is_string($key)) { |
|
17
|
|
|
$key = urldecode($key); |
|
18
|
|
|
} |
|
19
|
|
|
if ($ref instanceof \stdClass) { |
|
20
|
|
|
$ref = &$ref->$key; |
|
21
|
|
|
} elseif ($ref === null |
|
22
|
|
|
&& !is_int($key) |
|
23
|
|
|
&& false === filter_var($key, FILTER_VALIDATE_INT) |
|
24
|
|
|
) { |
|
25
|
|
|
$ref = new \stdClass(); |
|
26
|
|
|
$ref = &$ref->$key; |
|
27
|
|
|
} else { |
|
28
|
|
|
$ref = &$ref[$key]; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
$ref = $value; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function getByPath(&$holder, $path) |
|
35
|
|
|
{ |
|
36
|
|
|
$pathItems = explode('/', $path); |
|
37
|
|
|
if ('#' === $pathItems[0]) { |
|
38
|
|
|
array_shift($pathItems); |
|
39
|
|
|
} |
|
40
|
|
|
$ref = &$holder; |
|
41
|
|
|
while (null !== $key = array_shift($pathItems)) { |
|
42
|
|
|
$key = urldecode($key); |
|
43
|
|
|
if ($ref instanceof \stdClass) { |
|
44
|
|
|
if (property_exists($ref, $key)) { |
|
45
|
|
|
$ref = &$ref->$key; |
|
46
|
|
|
} else { |
|
47
|
|
|
throw new Exception('Key not found: ' . $key); |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
if (array_key_exists($key, $ref)) { |
|
51
|
|
|
$ref = &$ref[$key]; |
|
52
|
|
|
} else { |
|
53
|
|
|
throw new Exception('Key not found: ' . $key); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
return $ref; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public static function removeByPath(&$holder, $path) |
|
61
|
|
|
{ |
|
62
|
|
|
$pathItems = explode('/', $path); |
|
63
|
|
|
if ('#' === $pathItems[0]) { |
|
64
|
|
|
array_shift($pathItems); |
|
65
|
|
|
} |
|
66
|
|
|
$ref = &$holder; |
|
67
|
|
|
while (null !== $key = array_shift($pathItems)) { |
|
68
|
|
|
$parent = &$ref; |
|
69
|
|
|
$key = urldecode($key); |
|
70
|
|
|
$refKey = $key; |
|
71
|
|
|
if ($ref instanceof \stdClass) { |
|
72
|
|
|
if (property_exists($ref, $key)) { |
|
73
|
|
|
$ref = &$ref->$key; |
|
74
|
|
|
} else { |
|
75
|
|
|
throw new Exception('Key not found: ' . $key); |
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
if (array_key_exists($key, $ref)) { |
|
79
|
|
|
$ref = &$ref[$key]; |
|
80
|
|
|
} else { |
|
81
|
|
|
throw new Exception('Key not found: ' . $key); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (isset($parent) && isset($refKey)) { |
|
87
|
|
|
if ($parent instanceof \stdClass) { |
|
88
|
|
|
unset($parent->$refKey); |
|
89
|
|
|
} else { |
|
90
|
|
|
unset($parent[$refKey]); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $ref; |
|
94
|
|
|
} |
|
95
|
|
|
} |