@@ -49,11 +49,11 @@ discard block |
||
49 | 49 | public function setSource(&$source): void |
50 | 50 | { |
51 | 51 | /** @var array<scalar, mixed>|object|mixed|null $source */ |
52 | - if($source === null) { |
|
52 | + if ($source === null) { |
|
53 | 53 | $source = []; |
54 | 54 | } |
55 | 55 | |
56 | - if(is_scalar($source)) { |
|
56 | + if (is_scalar($source)) { |
|
57 | 57 | throw NestedAccessorException::createAsSourceIsScalar($source); |
58 | 58 | } |
59 | 59 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | public function get($path = null, bool $strict = true) |
68 | 68 | { |
69 | 69 | // when path is not specified |
70 | - if($path === null || $path === '') { |
|
70 | + if ($path === null || $path === '') { |
|
71 | 71 | // let's return the full source |
72 | 72 | return $this->source; |
73 | 73 | } |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | ); |
88 | 88 | |
89 | 89 | // when strict mode is on and we got errors |
90 | - if($strict && $errorsCount) { |
|
90 | + if ($strict && $errorsCount) { |
|
91 | 91 | throw NestedAccessorException::createAsCannotGetValue( |
92 | 92 | implode($this->pathDelimiter, $path), |
93 | 93 | $errorsCount |
@@ -122,8 +122,8 @@ discard block |
||
122 | 122 | { |
123 | 123 | $path = $this->formatPath($path); |
124 | 124 | |
125 | - if(!$this->exist($path)) { |
|
126 | - if($strict) { |
|
125 | + if (!$this->exist($path)) { |
|
126 | + if ($strict) { |
|
127 | 127 | throw NestedAccessorException::createAsCannotSetValue( |
128 | 128 | self::SET_MODE_DELETE, |
129 | 129 | implode($this->pathDelimiter, $path) |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | try { |
144 | 144 | $this->get($path); |
145 | 145 | return true; |
146 | - } catch(NestedAccessorException $e) { |
|
146 | + } catch (NestedAccessorException $e) { |
|
147 | 147 | return false; |
148 | 148 | } |
149 | 149 | } |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | { |
156 | 156 | try { |
157 | 157 | return $this->get($path) !== null; |
158 | - } catch(NestedAccessorException $e) { |
|
158 | + } catch (NestedAccessorException $e) { |
|
159 | 159 | return false; |
160 | 160 | } |
161 | 161 | } |
@@ -171,8 +171,8 @@ discard block |
||
171 | 171 | protected function _get($source, array $path, &$result, int &$errorsCount): void |
172 | 172 | { |
173 | 173 | // let's iterate every path part from stack |
174 | - while(count($path)) { |
|
175 | - if(is_array($source) && $this->isLastKeyInteger($path)) { |
|
174 | + while (count($path)) { |
|
175 | + if (is_array($source) && $this->isLastKeyInteger($path)) { |
|
176 | 176 | $key = array_pop($path); |
177 | 177 | |
178 | 178 | if (!array_key_exists($key, $source)) { |
@@ -185,13 +185,13 @@ discard block |
||
185 | 185 | continue; |
186 | 186 | } |
187 | 187 | |
188 | - if(is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
188 | + if (is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
189 | 189 | // the result will be multiple |
190 | - if(!is_array($result)) { |
|
190 | + if (!is_array($result)) { |
|
191 | 191 | $result = []; |
192 | 192 | } |
193 | 193 | // and we need to use recursive call for each item of this array |
194 | - foreach($source as $item) { |
|
194 | + foreach ($source as $item) { |
|
195 | 195 | $this->_get($item, $path, $result, $errorsCount); |
196 | 196 | } |
197 | 197 | // we don't need to do something in this recursive branch |
@@ -200,7 +200,7 @@ discard block |
||
200 | 200 | |
201 | 201 | $key = array_pop($path); |
202 | 202 | |
203 | - if(MapAccess::exists($source, $key)) { |
|
203 | + if (MapAccess::exists($source, $key)) { |
|
204 | 204 | // go to the next nested level |
205 | 205 | $source = MapAccess::get($source, $key); |
206 | 206 | } else { |
@@ -213,17 +213,17 @@ discard block |
||
213 | 213 | // when it's not the last iteration of the stack |
214 | 214 | // and the source is non-associative array (list) |
215 | 215 | /** @var mixed $source */ |
216 | - if(count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
217 | - if(is_array($source) && $this->isLastKeyInteger($path)) { |
|
216 | + if (count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
217 | + if (is_array($source) && $this->isLastKeyInteger($path)) { |
|
218 | 218 | continue; |
219 | 219 | } |
220 | 220 | |
221 | 221 | // the result will be multiple |
222 | - if(!is_array($result)) { |
|
222 | + if (!is_array($result)) { |
|
223 | 223 | $result = []; |
224 | 224 | } |
225 | 225 | // and we need to use recursive call for each item of this array |
226 | - foreach($source as $item) { |
|
226 | + foreach ($source as $item) { |
|
227 | 227 | $this->_get($item, $path, $result, $errorsCount); |
228 | 228 | } |
229 | 229 | // we don't need to do something in this recursive branch |
@@ -233,7 +233,7 @@ discard block |
||
233 | 233 | |
234 | 234 | // now path stack is empty — we reached target value of given path in source argument |
235 | 235 | // so if result is multiple |
236 | - if(is_array($result)) { |
|
236 | + if (is_array($result)) { |
|
237 | 237 | // we append source to result |
238 | 238 | $result[] = $source; |
239 | 239 | } else { |
@@ -260,8 +260,8 @@ discard block |
||
260 | 260 | $tempPrevKey = null; |
261 | 261 | |
262 | 262 | // let's iterate every path part to go deeper into nesting |
263 | - foreach($path as $key) { |
|
264 | - if(isset($temp) && is_scalar($temp)) { |
|
263 | + foreach ($path as $key) { |
|
264 | + if (isset($temp) && is_scalar($temp)) { |
|
265 | 265 | // value in the middle of the path must be an array |
266 | 266 | $temp = []; |
267 | 267 | } |
@@ -270,8 +270,8 @@ discard block |
||
270 | 270 | $tempPrevKey = $key; |
271 | 271 | |
272 | 272 | // go to the next nested level |
273 | - if(is_object($temp)) { |
|
274 | - if($strict && !($temp instanceof stdClass) && !ObjectAccess::hasPublicProperty($temp, $key)) { |
|
273 | + if (is_object($temp)) { |
|
274 | + if ($strict && !($temp instanceof stdClass) && !ObjectAccess::hasPublicProperty($temp, $key)) { |
|
275 | 275 | throw NestedAccessorException::createAsCannotSetValue($mode, implode($this->pathDelimiter, $path)); |
276 | 276 | } |
277 | 277 | $temp = &$temp->{$key}; |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | } |
283 | 283 | } |
284 | 284 | // now we can save value to the source |
285 | - switch($mode) { |
|
285 | + switch ($mode) { |
|
286 | 286 | case self::SET_MODE_SET: |
287 | 287 | $temp = $value; |
288 | 288 | break; |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | $this->_append($temp, $value, $path, $strict); |
291 | 291 | break; |
292 | 292 | case self::SET_MODE_DELETE: |
293 | - if(!$this->_delete($tempPrevSource, $tempPrevKey, $path, $strict)) { |
|
293 | + if (!$this->_delete($tempPrevSource, $tempPrevKey, $path, $strict)) { |
|
294 | 294 | return $this; |
295 | 295 | } |
296 | 296 | break; |
@@ -311,13 +311,13 @@ discard block |
||
311 | 311 | */ |
312 | 312 | protected function _append(&$source, $value, array $path, bool $strict): void |
313 | 313 | { |
314 | - if(!is_array($source) || ArrayHelper::isAssoc($source)) { |
|
315 | - if($strict) { |
|
314 | + if (!is_array($source) || ArrayHelper::isAssoc($source)) { |
|
315 | + if ($strict) { |
|
316 | 316 | throw NestedAccessorException::createAsCannotSetValue( |
317 | 317 | self::SET_MODE_APPEND, |
318 | 318 | implode($this->pathDelimiter, $path) |
319 | 319 | ); |
320 | - } elseif(!is_array($source)) { |
|
320 | + } elseif (!is_array($source)) { |
|
321 | 321 | $source = []; |
322 | 322 | } |
323 | 323 | } |
@@ -336,8 +336,8 @@ discard block |
||
336 | 336 | */ |
337 | 337 | protected function _delete(&$source, $key, array $path, bool $strict): bool |
338 | 338 | { |
339 | - if($key === null || (!is_array($source) && !($source instanceof stdClass))) { |
|
340 | - if($strict) { |
|
339 | + if ($key === null || (!is_array($source) && !($source instanceof stdClass))) { |
|
340 | + if ($strict) { |
|
341 | 341 | throw NestedAccessorException::createAsCannotSetValue( |
342 | 342 | self::SET_MODE_DELETE, |
343 | 343 | implode($this->pathDelimiter, $path) |
@@ -346,7 +346,7 @@ discard block |
||
346 | 346 | return false; |
347 | 347 | } |
348 | 348 | } |
349 | - if(is_array($source)) { |
|
349 | + if (is_array($source)) { |
|
350 | 350 | unset($source[$key]); |
351 | 351 | } else { |
352 | 352 | unset($source->{$key}); |
@@ -361,11 +361,11 @@ discard block |
||
361 | 361 | */ |
362 | 362 | protected function formatPath($path): array |
363 | 363 | { |
364 | - if(is_array($path)) { |
|
364 | + if (is_array($path)) { |
|
365 | 365 | return $path; |
366 | 366 | } |
367 | 367 | |
368 | - if($path === null || $path === '') { |
|
368 | + if ($path === null || $path === '') { |
|
369 | 369 | return []; |
370 | 370 | } |
371 | 371 |