1 | <?php |
||
46 | class ArrayAccessor |
||
47 | { |
||
48 | |||
49 | /** |
||
50 | * @var |
||
51 | */ |
||
52 | protected $data; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $pathSeparator = ':'; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $includePathSeparatorInKeys = false; |
||
63 | |||
64 | /** |
||
65 | * @param array $data |
||
66 | * @param string $pathSeparator |
||
67 | * @param bool $includePathSeparatorInKeys |
||
68 | */ |
||
69 | 198 | public function __construct(array $data = array(), $pathSeparator = ':', $includePathSeparatorInKeys = false) |
|
75 | |||
76 | /** |
||
77 | * @param mixed $data |
||
78 | */ |
||
79 | 26 | public function setData($data) |
|
83 | |||
84 | /** |
||
85 | * @return array |
||
86 | */ |
||
87 | 34 | public function getData() |
|
91 | |||
92 | /** |
||
93 | * @param $path |
||
94 | * @param null $defaultIfEmpty |
||
95 | * @return array|null |
||
96 | */ |
||
97 | 195 | public function get($path, $defaultIfEmpty = null) |
|
98 | { |
||
99 | 195 | $pathArray = $this->getPathAsArray($path); |
|
100 | 195 | $pathSegmentCount = count($pathArray); |
|
101 | |||
102 | switch ($pathSegmentCount) { |
||
103 | // direct access for small paths |
||
104 | 195 | case 1: |
|
105 | 38 | return isset($this->data[$pathArray[0]]) ? |
|
106 | 38 | $this->data[$pathArray[0]] : $defaultIfEmpty; |
|
107 | 194 | case 2: |
|
108 | 50 | return isset($this->data[$pathArray[0]][$pathArray[1]]) ? |
|
109 | 50 | $this->data[$pathArray[0]][$pathArray[1]] : $defaultIfEmpty; |
|
110 | 177 | case 3: |
|
111 | 37 | return isset($this->data[$pathArray[0]][$pathArray[1]][$pathArray[2]]) ? |
|
112 | 37 | $this->data[$pathArray[0]][$pathArray[1]][$pathArray[2]] : $defaultIfEmpty; |
|
113 | default: |
||
114 | // when we have a longer path we use a loop to get the element |
||
115 | 171 | $loopResult = $this->getDeepElementWithLoop($pathArray); |
|
116 | 171 | return $loopResult !== null ? $loopResult : $defaultIfEmpty; |
|
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param $pathArray |
||
122 | * @return array|null |
||
123 | */ |
||
124 | 171 | protected function getDeepElementWithLoop($pathArray) |
|
125 | { |
||
126 | 171 | $currentElement = $this->data; |
|
127 | 171 | foreach ($pathArray as $key => $pathSegment) { |
|
128 | // if the current path segment was not found we can stop searching |
||
129 | 171 | if (!isset($currentElement[$pathSegment])) { |
|
130 | 144 | break; |
|
131 | } |
||
132 | 146 | $currentElement = $currentElement[$pathSegment]; |
|
133 | 146 | unset($pathArray[$key]); |
|
134 | } |
||
135 | |||
136 | // if segments are left the item does not exist |
||
137 | 171 | if (count($pathArray) > 0) { |
|
138 | 144 | return null; |
|
139 | } |
||
140 | |||
141 | // if no items left, we've found the last element |
||
142 | 114 | return $currentElement; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param $path |
||
147 | * @return bool |
||
148 | */ |
||
149 | 5 | public function has($path) |
|
153 | |||
154 | /** |
||
155 | * @param $path |
||
156 | * @param $value |
||
157 | */ |
||
158 | 17 | public function set($path, $value) |
|
159 | { |
||
160 | 17 | $pathArray = $this->getPathAsArray($path); |
|
161 | 17 | $pathSegmentCount = count($pathArray); |
|
162 | |||
163 | switch ($pathSegmentCount) { |
||
164 | // direct access for small paths |
||
165 | 17 | case 1: |
|
166 | 10 | $this->data[$pathArray[0]] = $value; |
|
167 | 10 | return; |
|
168 | 12 | case 2: |
|
169 | 10 | $this->data[$pathArray[0]][$pathArray[1]] = $value; |
|
170 | 10 | return; |
|
171 | default: |
||
172 | 2 | $this->setDeepElementWithLoop($pathArray, $value); |
|
173 | } |
||
174 | 2 | } |
|
175 | |||
176 | /** |
||
177 | * @param $pathArray |
||
178 | * @param mixed $value |
||
179 | */ |
||
180 | 2 | protected function setDeepElementWithLoop($pathArray, $value) |
|
181 | { |
||
182 | 2 | $currentElement = &$this->data; |
|
183 | 2 | foreach ($pathArray as $key => $pathSegment) { |
|
184 | 2 | if (!isset($currentElement[$pathSegment])) { |
|
185 | 2 | $currentElement[$pathSegment] = array(); |
|
186 | } |
||
187 | |||
188 | 2 | unset($pathArray[$key]); |
|
189 | // if segments are left the item does not exist |
||
190 | 2 | if (count($pathArray) === 0) { |
|
191 | 2 | $currentElement[$pathSegment] = $value; |
|
192 | 2 | return; |
|
193 | } |
||
194 | |||
195 | 2 | $currentElement = &$currentElement[$pathSegment]; |
|
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param string $path |
||
201 | */ |
||
202 | 7 | public function reset($path) |
|
203 | { |
||
204 | 7 | $pathArray = $this->getPathAsArray($path); |
|
205 | 7 | $pathSegmentCount = count($pathArray); |
|
206 | |||
207 | switch ($pathSegmentCount) { |
||
208 | // direct access for small paths |
||
209 | 7 | case 1: |
|
210 | unset($this->data[$pathArray[0]]); |
||
211 | return; |
||
212 | 7 | case 2: |
|
213 | 3 | unset($this->data[$pathArray[0]][$pathArray[1]]); |
|
214 | 3 | return; |
|
215 | default: |
||
216 | 4 | $this->resetDeepElementWithLoop($pathArray); |
|
217 | } |
||
218 | 4 | } |
|
219 | |||
220 | /** |
||
221 | * @param array $pathArray |
||
222 | */ |
||
223 | 4 | protected function resetDeepElementWithLoop($pathArray) |
|
224 | { |
||
225 | 4 | $currentElement = &$this->data; |
|
226 | |||
227 | 4 | foreach ($pathArray as $key => $pathSegment) { |
|
228 | 4 | unset($pathArray[$key]); |
|
229 | // if segments are left the item does not exist |
||
230 | 4 | if (count($pathArray) === 0) { |
|
231 | 4 | unset($currentElement[$pathSegment]); |
|
232 | // when the element is empty after unsetting the path segment, we can remove it completely |
||
233 | 4 | if (empty($currentElement)) { |
|
234 | 4 | unset($currentElement); |
|
235 | } |
||
236 | } else { |
||
237 | 4 | $currentElement = &$currentElement[$pathSegment]; |
|
238 | } |
||
239 | } |
||
240 | 4 | } |
|
241 | |||
242 | /** |
||
243 | * @param string $path |
||
244 | * @return array |
||
245 | */ |
||
246 | 197 | protected function getPathAsArray($path) |
|
257 | } |
||
258 |