1 | <?php declare(strict_types=1); |
||
25 | class JsonPath |
||
26 | { |
||
27 | /** @var array */ |
||
28 | private $jsonStructure; |
||
29 | |||
30 | /** |
||
31 | * @param $structure The initial data structure to extract from and insert into. Typically this will be a |
||
32 | * multidimensional associative array; but well-formed JSON strings are also acceptable. |
||
33 | */ |
||
34 | 10 | public function __construct($structure) |
|
38 | |||
39 | /** |
||
40 | * Set a node in the structure |
||
41 | * |
||
42 | * @param $path The XPath to use |
||
43 | * @param $value The new value of the node |
||
44 | */ |
||
45 | 9 | public function set(string $path, $value) |
|
49 | |||
50 | /** |
||
51 | * Internal method for recursive calls. |
||
52 | * |
||
53 | * @param $path |
||
54 | * @param $value |
||
55 | * @param $json |
||
56 | * |
||
57 | * @return mixed |
||
58 | 9 | */ |
|
59 | private function setPath(string $path, $value, array $json): array |
||
76 | |||
77 | /** |
||
78 | * Return the updated structure. |
||
79 | * |
||
80 | * @return mixed |
||
81 | 9 | */ |
|
82 | public function getStructure() |
||
86 | |||
87 | /** |
||
88 | * Get a path's value. If no path can be matched, NULL is returned. |
||
89 | * |
||
90 | * @param $path |
||
91 | * |
||
92 | 1 | * @return mixed|null |
|
93 | */ |
||
94 | 1 | public function get(string $path) |
|
98 | |||
99 | /** |
||
100 | * Internal method for recursion. |
||
101 | * |
||
102 | * @param $path |
||
103 | * @param $json |
||
104 | 1 | * |
|
105 | * @return null |
||
106 | 1 | */ |
|
107 | 1 | private function getPath(string $path, $json) |
|
122 | } |
||
123 |