1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Common; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class allows arbitrary data structures to be inserted into, and extracted from, deep arrays |
7
|
|
|
* and JSON-serialized strings. Say, for example, that you have this array as an input: |
8
|
|
|
* |
9
|
|
|
* <pre><code>['foo' => ['bar' => ['baz' => 'some_value']]]</code></pre> |
10
|
|
|
* |
11
|
|
|
* and you wanted to insert or extract an element. Usually, you would use: |
12
|
|
|
* |
13
|
|
|
* <pre><code>$array['foo']['bar']['baz'] = 'new_value';</code></pre> |
14
|
|
|
* |
15
|
|
|
* but sometimes you do not have access to the variable - so a string representation is needed. Using |
16
|
|
|
* XPath-like syntax, this class allows you to do this: |
17
|
|
|
* |
18
|
|
|
* <pre><code>$jsonPath = new JsonPath($array); |
19
|
|
|
* $jsonPath->set('foo.bar.baz', 'new_value'); |
20
|
|
|
* $val = $jsonPath->get('foo.bar.baz'); |
21
|
|
|
* </code></pre> |
22
|
|
|
* |
23
|
|
|
* @package OpenStack\Common |
24
|
|
|
*/ |
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) |
35
|
|
|
{ |
36
|
10 |
|
$this->jsonStructure = is_string($structure) ? json_decode($structure, true) : $structure; |
37
|
10 |
|
} |
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) |
46
|
|
|
{ |
47
|
9 |
|
$this->jsonStructure = $this->setPath($path, $value, $this->jsonStructure); |
48
|
9 |
|
} |
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 |
60
|
9 |
|
{ |
61
|
9 |
|
$nodes = explode('.', $path); |
62
|
|
|
$point = array_shift($nodes); |
63
|
9 |
|
|
64
|
9 |
|
if (!isset($json[$point])) { |
65
|
9 |
|
$json[$point] = []; |
66
|
|
|
} |
67
|
9 |
|
|
68
|
9 |
|
if (!empty($nodes)) { |
69
|
9 |
|
$json[$point] = $this->setPath(implode('.', $nodes), $value, $json[$point]); |
70
|
9 |
|
} else { |
71
|
|
|
$json[$point] = $value; |
72
|
|
|
} |
73
|
9 |
|
|
74
|
|
|
return $json; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the updated structure. |
79
|
|
|
* |
80
|
|
|
* @return mixed |
81
|
9 |
|
*/ |
82
|
|
|
public function getStructure() |
83
|
9 |
|
{ |
84
|
|
|
return $this->jsonStructure; |
85
|
|
|
} |
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) |
95
|
|
|
{ |
96
|
|
|
return $this->getPath($path, $this->jsonStructure); |
97
|
|
|
} |
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) |
108
|
|
|
{ |
109
|
1 |
|
$nodes = explode('.', $path); |
110
|
1 |
|
$point = array_shift($nodes); |
111
|
|
|
|
112
|
|
|
if (!isset($json[$point])) { |
113
|
1 |
|
return null; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
1 |
|
if (empty($nodes)) { |
117
|
|
|
return $json[$point]; |
118
|
|
|
} else { |
119
|
|
|
return $this->getPath(implode('.', $nodes), $json[$point]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|