1 | <?php |
||
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 = []) { |
||
39 | |||
40 | /** |
||
41 | * getArray returns the complete array. |
||
42 | * |
||
43 | * @return array |
||
44 | */ |
||
45 | public function getArray() { |
||
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) { |
||
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) { |
||
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) { |
||
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) { |
||
101 | |||
102 | /** |
||
103 | * Remove an element if it exists. |
||
104 | * |
||
105 | * @param string $path |
||
106 | */ |
||
107 | public function remove($path) { |
||
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() { |
||
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() { |
||
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) { |
||
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) { |
||
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) { |
||
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) { |
||
190 | } |
||
191 |