@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | public function setSource(&$source): void |
48 | 48 | { |
49 | 49 | /** @var array<scalar, mixed>|object|mixed|null $source */ |
50 | - if($source === null) { |
|
50 | + if ($source === null) { |
|
51 | 51 | $source = []; |
52 | 52 | } |
53 | 53 | |
54 | - if(is_scalar($source)) { |
|
54 | + if (is_scalar($source)) { |
|
55 | 55 | throw NestedAccessorException::createAsSourceIsScalar($source); |
56 | 56 | } |
57 | 57 | |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | public function get($path = null, bool $strict = true) |
66 | 66 | { |
67 | 67 | // when path is not specified |
68 | - if($path === null || $path === '') { |
|
68 | + if ($path === null || $path === '') { |
|
69 | 69 | // let's return the full source |
70 | 70 | return $this->source; |
71 | 71 | } |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | ); |
86 | 86 | |
87 | 87 | // when strict mode is on and we got errors |
88 | - if($strict && $errorsCount) { |
|
88 | + if ($strict && $errorsCount) { |
|
89 | 89 | throw NestedAccessorException::createAsCannotGetValue( |
90 | 90 | implode($this->pathDelimiter, $path), |
91 | 91 | $errorsCount |
@@ -118,8 +118,8 @@ discard block |
||
118 | 118 | */ |
119 | 119 | public function delete($path, bool $strict = true): self |
120 | 120 | { |
121 | - if(!$this->exist($path)) { |
|
122 | - if($strict) { |
|
121 | + if (!$this->exist($path)) { |
|
122 | + if ($strict) { |
|
123 | 123 | throw NestedAccessorException::createAsCannotSetValue(self::SET_MODE_DELETE, $path); |
124 | 124 | } |
125 | 125 | return $this; |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | try { |
138 | 138 | $this->get($path); |
139 | 139 | return true; |
140 | - } catch(NestedAccessorException $e) { |
|
140 | + } catch (NestedAccessorException $e) { |
|
141 | 141 | return false; |
142 | 142 | } |
143 | 143 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | { |
150 | 150 | try { |
151 | 151 | return $this->get($path) !== null; |
152 | - } catch(NestedAccessorException $e) { |
|
152 | + } catch (NestedAccessorException $e) { |
|
153 | 153 | return false; |
154 | 154 | } |
155 | 155 | } |
@@ -165,14 +165,14 @@ discard block |
||
165 | 165 | protected function _get($source, array $path, &$result, int &$errorsCount): void |
166 | 166 | { |
167 | 167 | // let's iterate every path part from stack |
168 | - while(count($path)) { |
|
169 | - if(is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
168 | + while (count($path)) { |
|
169 | + if (is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
170 | 170 | // the result will be multiple |
171 | - if(!is_array($result)) { |
|
171 | + if (!is_array($result)) { |
|
172 | 172 | $result = []; |
173 | 173 | } |
174 | 174 | // and we need to use recursive call for each item of this array |
175 | - foreach($source as $item) { |
|
175 | + foreach ($source as $item) { |
|
176 | 176 | $this->_get($item, $path, $result, $errorsCount); |
177 | 177 | } |
178 | 178 | // we don't need to do something in this recursive branch |
@@ -181,8 +181,8 @@ discard block |
||
181 | 181 | |
182 | 182 | $key = array_pop($path); |
183 | 183 | |
184 | - if(is_array($source)) { |
|
185 | - if(!array_key_exists($key, $source)) { |
|
184 | + if (is_array($source)) { |
|
185 | + if (!array_key_exists($key, $source)) { |
|
186 | 186 | // path part key is missing in source array |
187 | 187 | $errorsCount++; |
188 | 188 | // we cannot go deeper |
@@ -190,12 +190,12 @@ discard block |
||
190 | 190 | } |
191 | 191 | // go to the next nested level |
192 | 192 | $source = $source[$key]; |
193 | - } elseif(is_object($source)) { |
|
193 | + } elseif (is_object($source)) { |
|
194 | 194 | $getterName = 'get'.ucfirst($key); |
195 | - if(method_exists($source, $getterName)) { |
|
195 | + if (method_exists($source, $getterName)) { |
|
196 | 196 | // go to the next nested level |
197 | 197 | $source = $source->{$getterName}(); |
198 | - } elseif(property_exists($source, $key)) { |
|
198 | + } elseif (property_exists($source, $key)) { |
|
199 | 199 | // go to the next nested level |
200 | 200 | $source = $source->{$key}; |
201 | 201 | } else { |
@@ -213,13 +213,13 @@ discard block |
||
213 | 213 | |
214 | 214 | // when it's not the last iteration of the stack |
215 | 215 | // and the source is non-associative array (list) |
216 | - if(count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
216 | + if (count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
217 | 217 | // the result will be multiple |
218 | - if(!is_array($result)) { |
|
218 | + if (!is_array($result)) { |
|
219 | 219 | $result = []; |
220 | 220 | } |
221 | 221 | // and we need to use recursive call for each item of this array |
222 | - foreach($source as $item) { |
|
222 | + foreach ($source as $item) { |
|
223 | 223 | $this->_get($item, $path, $result, $errorsCount); |
224 | 224 | } |
225 | 225 | // we don't need to do something in this recursive branch |
@@ -229,7 +229,7 @@ discard block |
||
229 | 229 | |
230 | 230 | // now path stack is empty — we reached target value of given path in source argument |
231 | 231 | // so if result is multiple |
232 | - if(is_array($result)) { |
|
232 | + if (is_array($result)) { |
|
233 | 233 | // we append source to result |
234 | 234 | $result[] = $source; |
235 | 235 | } else { |
@@ -256,8 +256,8 @@ discard block |
||
256 | 256 | $tempPrevKey = null; |
257 | 257 | |
258 | 258 | // let's iterate every path part to go deeper into nesting |
259 | - foreach($path as $key) { |
|
260 | - if(isset($temp) && is_scalar($temp)) { |
|
259 | + foreach ($path as $key) { |
|
260 | + if (isset($temp) && is_scalar($temp)) { |
|
261 | 261 | // value in the middle of the path must be an array |
262 | 262 | $temp = []; |
263 | 263 | } |
@@ -266,8 +266,8 @@ discard block |
||
266 | 266 | $tempPrevKey = $key; |
267 | 267 | |
268 | 268 | // go to the next nested level |
269 | - if(is_object($temp)) { |
|
270 | - if($strict && !property_exists($temp, $key)) { |
|
269 | + if (is_object($temp)) { |
|
270 | + if ($strict && !property_exists($temp, $key)) { |
|
271 | 271 | throw NestedAccessorException::createAsCannotSetValue($mode, implode($this->pathDelimiter, $path)); |
272 | 272 | } |
273 | 273 | $temp = &$temp->{$key}; |
@@ -278,18 +278,18 @@ discard block |
||
278 | 278 | } |
279 | 279 | } |
280 | 280 | // now we can save value to the source |
281 | - switch($mode) { |
|
281 | + switch ($mode) { |
|
282 | 282 | case self::SET_MODE_SET: |
283 | 283 | $temp = $value; |
284 | 284 | break; |
285 | 285 | case self::SET_MODE_APPEND: |
286 | - if(!is_array($temp) || ArrayHelper::isAssoc($temp)) { |
|
287 | - if($strict) { |
|
286 | + if (!is_array($temp) || ArrayHelper::isAssoc($temp)) { |
|
287 | + if ($strict) { |
|
288 | 288 | throw NestedAccessorException::createAsCannotSetValue( |
289 | 289 | $mode, |
290 | 290 | implode($this->pathDelimiter, $path) |
291 | 291 | ); |
292 | - } elseif(!is_array($temp)) { |
|
292 | + } elseif (!is_array($temp)) { |
|
293 | 293 | $temp = []; |
294 | 294 | } |
295 | 295 | } |
@@ -297,10 +297,10 @@ discard block |
||
297 | 297 | $temp[] = $value; |
298 | 298 | break; |
299 | 299 | case self::SET_MODE_DELETE: |
300 | - if($tempPrevKey === null || (!is_array($tempPrevSource) && !($tempPrevSource instanceof stdClass))) { |
|
300 | + if ($tempPrevKey === null || (!is_array($tempPrevSource) && !($tempPrevSource instanceof stdClass))) { |
|
301 | 301 | throw NestedAccessorException::createAsCannotSetValue($mode, implode($this->pathDelimiter, $path)); |
302 | 302 | } |
303 | - if(is_array($tempPrevSource)) { |
|
303 | + if (is_array($tempPrevSource)) { |
|
304 | 304 | unset($tempPrevSource[$tempPrevKey]); |
305 | 305 | } else { |
306 | 306 | unset($tempPrevSource->{$tempPrevKey}); |
@@ -318,11 +318,11 @@ discard block |
||
318 | 318 | */ |
319 | 319 | protected function formatPath($path): array |
320 | 320 | { |
321 | - if(is_array($path)) { |
|
321 | + if (is_array($path)) { |
|
322 | 322 | return $path; |
323 | 323 | } |
324 | 324 | |
325 | - if($path === null || $path === '') { |
|
325 | + if ($path === null || $path === '') { |
|
326 | 326 | return []; |
327 | 327 | } |
328 | 328 |
@@ -62,7 +62,7 @@ |
||
62 | 62 | */ |
63 | 63 | public static function createAsCannotSetValue(int $mode, string $path): NestedAccessorException |
64 | 64 | { |
65 | - switch($mode) { |
|
65 | + switch ($mode) { |
|
66 | 66 | case NestedAccessor::SET_MODE_SET: |
67 | 67 | return static::_createAsCannotSetValue($path); |
68 | 68 | case NestedAccessor::SET_MODE_APPEND: |