|
1
|
|
|
<?php |
|
2
|
|
|
namespace nochso\Omni; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* DotArray holds a multi-dimensional array and wraps the static API of `\nochso\Omni\Dot`. |
|
6
|
|
|
* |
|
7
|
|
|
* e.g. |
|
8
|
|
|
* ```php |
|
9
|
|
|
* $array = [ |
|
10
|
|
|
* 'a' => [ |
|
11
|
|
|
* 'b' => 'c' |
|
12
|
|
|
* ] |
|
13
|
|
|
* ]; |
|
14
|
|
|
* $da = new DotArray(); |
|
15
|
|
|
* echo $da->get('a.b'); // 'c' |
|
16
|
|
|
* ``` |
|
17
|
|
|
* |
|
18
|
|
|
* ArrayAccess is also possible: |
|
19
|
|
|
* ```php |
|
20
|
|
|
* $da['a.b'] = 'c'; |
|
21
|
|
|
* ``` |
|
22
|
|
|
* |
|
23
|
|
|
* You can also escape parts of the path: |
|
24
|
|
|
* ```php |
|
25
|
|
|
* $da['a\.b'] === $da->getArray()['a.b'] // true |
|
26
|
|
|
* ``` |
|
27
|
|
|
* |
|
28
|
|
|
* @see \nochso\Omni\Dot |
|
29
|
|
|
*/ |
|
30
|
|
|
class DotArray implements \ArrayAccess, \IteratorAggregate{ |
|
31
|
|
|
private $data; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $array Any (nested) array. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(array $array = []) { |
|
37
|
|
|
$this->data = $array; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* getArray returns the complete array. |
|
42
|
|
|
* |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getArray() { |
|
46
|
|
|
return $this->data; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the value of the element at the given dot key path. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $path Dot-notation key path. e.g. `parent.child` |
|
53
|
|
|
* @param null|mixed $default Default value to return |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public function get($path, $default = null) { |
|
58
|
|
|
return Dot::get($this->data, $path, $default); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Has returns true if an element exists at the given dot key path. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $path Dot-notation key path to search. |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function has($path) { |
|
69
|
|
|
return Dot::has($this->data, $path); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set a value at a certain path by creating missing elements and overwriting non-array values. |
|
74
|
|
|
* |
|
75
|
|
|
* If any of the visited elements is not an array, it will be replaced with an array. |
|
76
|
|
|
* |
|
77
|
|
|
* This will overwrite existing non-array values. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $path |
|
80
|
|
|
* @param mixed $value |
|
81
|
|
|
*/ |
|
82
|
|
|
public function set($path, $value) { |
|
83
|
|
|
Dot::set($this->data, $path, $value); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* trySet sets a value at a certain path, expecting arrays or missing elements along the way. |
|
88
|
|
|
* |
|
89
|
|
|
* If any of the visited elements is not an array, a \RuntimeException is thrown. |
|
90
|
|
|
* |
|
91
|
|
|
* Use this if you want to avoid overwriting existing non-array values. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $path |
|
94
|
|
|
* @param mixed $value |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \RuntimeException |
|
97
|
|
|
*/ |
|
98
|
|
|
public function trySet($path, $value) { |
|
99
|
|
|
Dot::trySet($this->data, $path, $value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Remove an element if it exists. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $path |
|
106
|
|
|
*/ |
|
107
|
|
|
public function remove($path) { |
|
108
|
|
|
Dot::remove($this->data, $path); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Flatten the array into a single dimension array with escaped dot paths as keys. |
|
113
|
|
|
* |
|
114
|
|
|
* Dots within specific keys are escaped. |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
|
|
public function flatten() { |
|
119
|
|
|
return Dot::flatten($this->data); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* getIterator allows you to iterate over a flattened array using `foreach`. |
|
124
|
|
|
* |
|
125
|
|
|
* Keys are escaped and thus safe to use. |
|
126
|
|
|
* |
|
127
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
128
|
|
|
* |
|
129
|
|
|
* @return \Traversable An instance of an object implementing <b>Iterator</b> or <b>Traversable</b> |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getIterator() { |
|
132
|
|
|
return new \ArrayIterator($this->flatten()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* offsetExists allows using `isset($da['a.b'])`. |
|
137
|
|
|
* |
|
138
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetexists.php |
|
139
|
|
|
* |
|
140
|
|
|
* @param mixed $offset An offset to check for. |
|
141
|
|
|
* |
|
142
|
|
|
* @return bool true on success or false on failure. |
|
143
|
|
|
*/ |
|
144
|
|
|
public function offsetExists($offset) { |
|
145
|
|
|
return $this->has($offset); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* offsetGet allows array access, e.g. `$da['a.b']`. |
|
150
|
|
|
* |
|
151
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetget.php |
|
152
|
|
|
* |
|
153
|
|
|
* @param mixed $offset The offset to retrieve. |
|
154
|
|
|
* |
|
155
|
|
|
* @return mixed Can return all value types. |
|
156
|
|
|
*/ |
|
157
|
|
|
public function offsetGet($offset) { |
|
158
|
|
|
return $this->get($offset); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* offsetSet allows writing to arrays `$da['a.b'] = 'foo'`. |
|
163
|
|
|
* |
|
164
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetset.php |
|
165
|
|
|
* |
|
166
|
|
|
* @param mixed $offset The offset to assign the value to. |
|
167
|
|
|
* @param mixed $value The value to set. |
|
168
|
|
|
* |
|
169
|
|
|
* @return void |
|
170
|
|
|
*/ |
|
171
|
|
|
public function offsetSet($offset, $value) { |
|
172
|
|
|
if ($offset === null) { |
|
173
|
|
|
throw new \InvalidArgumentException(sprintf('Appending to %s is not supported.', self::class)); |
|
174
|
|
|
} |
|
175
|
|
|
$this->set($offset, $value); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* offsetUnset allows using `unset($da['a.b'])`. |
|
180
|
|
|
* |
|
181
|
|
|
* @link http://php.net/manual/en/arrayaccess.offsetunset.php |
|
182
|
|
|
* |
|
183
|
|
|
* @param mixed $offset The offset to unset. |
|
184
|
|
|
* |
|
185
|
|
|
* @return void |
|
186
|
|
|
*/ |
|
187
|
|
|
public function offsetUnset($offset) { |
|
188
|
|
|
$this->remove($offset); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|