@@ -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,14 +171,14 @@ 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) && !ArrayHelper::isAssoc($source)) { |
|
| 174 | + while (count($path)) { |
|
| 175 | + if (is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
| 176 | 176 | // the result will be multiple |
| 177 | - if(!is_array($result)) { |
|
| 177 | + if (!is_array($result)) { |
|
| 178 | 178 | $result = []; |
| 179 | 179 | } |
| 180 | 180 | // and we need to use recursive call for each item of this array |
| 181 | - foreach($source as $item) { |
|
| 181 | + foreach ($source as $item) { |
|
| 182 | 182 | $this->_get($item, $path, $result, $errorsCount); |
| 183 | 183 | } |
| 184 | 184 | // we don't need to do something in this recursive branch |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | |
| 188 | 188 | $key = array_pop($path); |
| 189 | 189 | |
| 190 | - if(MapAccess::exists($source, $key)) { |
|
| 190 | + if (MapAccess::exists($source, $key)) { |
|
| 191 | 191 | // go to the next nested level |
| 192 | 192 | $source = MapAccess::get($source, $key); |
| 193 | 193 | } else { |
@@ -200,13 +200,13 @@ discard block |
||
| 200 | 200 | // when it's not the last iteration of the stack |
| 201 | 201 | // and the source is non-associative array (list) |
| 202 | 202 | /** @var mixed $source */ |
| 203 | - if(count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
| 203 | + if (count($path) && is_array($source) && !ArrayHelper::isAssoc($source)) { |
|
| 204 | 204 | // the result will be multiple |
| 205 | - if(!is_array($result)) { |
|
| 205 | + if (!is_array($result)) { |
|
| 206 | 206 | $result = []; |
| 207 | 207 | } |
| 208 | 208 | // and we need to use recursive call for each item of this array |
| 209 | - foreach($source as $item) { |
|
| 209 | + foreach ($source as $item) { |
|
| 210 | 210 | $this->_get($item, $path, $result, $errorsCount); |
| 211 | 211 | } |
| 212 | 212 | // we don't need to do something in this recursive branch |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | |
| 217 | 217 | // now path stack is empty — we reached target value of given path in source argument |
| 218 | 218 | // so if result is multiple |
| 219 | - if(is_array($result)) { |
|
| 219 | + if (is_array($result)) { |
|
| 220 | 220 | // we append source to result |
| 221 | 221 | $result[] = $source; |
| 222 | 222 | } else { |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | $tempPrevKey = null; |
| 244 | 244 | |
| 245 | 245 | // let's iterate every path part to go deeper into nesting |
| 246 | - foreach($path as $key) { |
|
| 247 | - if(isset($temp) && is_scalar($temp)) { |
|
| 246 | + foreach ($path as $key) { |
|
| 247 | + if (isset($temp) && is_scalar($temp)) { |
|
| 248 | 248 | // value in the middle of the path must be an array |
| 249 | 249 | $temp = []; |
| 250 | 250 | } |
@@ -253,8 +253,8 @@ discard block |
||
| 253 | 253 | $tempPrevKey = $key; |
| 254 | 254 | |
| 255 | 255 | // go to the next nested level |
| 256 | - if(is_object($temp)) { |
|
| 257 | - if($strict && !($temp instanceof stdClass) && !ObjectAccess::hasPublicProperty($temp, $key)) { |
|
| 256 | + if (is_object($temp)) { |
|
| 257 | + if ($strict && !($temp instanceof stdClass) && !ObjectAccess::hasPublicProperty($temp, $key)) { |
|
| 258 | 258 | throw NestedAccessorException::createAsCannotSetValue($mode, implode($this->pathDelimiter, $path)); |
| 259 | 259 | } |
| 260 | 260 | $temp = &$temp->{$key}; |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | // now we can save value to the source |
| 268 | - switch($mode) { |
|
| 268 | + switch ($mode) { |
|
| 269 | 269 | case self::SET_MODE_SET: |
| 270 | 270 | $temp = $value; |
| 271 | 271 | break; |
@@ -273,7 +273,7 @@ discard block |
||
| 273 | 273 | $this->_append($temp, $value, $path, $strict); |
| 274 | 274 | break; |
| 275 | 275 | case self::SET_MODE_DELETE: |
| 276 | - if(!$this->_delete($tempPrevSource, $tempPrevKey, $path, $strict)) { |
|
| 276 | + if (!$this->_delete($tempPrevSource, $tempPrevKey, $path, $strict)) { |
|
| 277 | 277 | return $this; |
| 278 | 278 | } |
| 279 | 279 | break; |
@@ -294,13 +294,13 @@ discard block |
||
| 294 | 294 | */ |
| 295 | 295 | protected function _append(&$source, $value, array $path, bool $strict): void |
| 296 | 296 | { |
| 297 | - if(!is_array($source) || ArrayHelper::isAssoc($source)) { |
|
| 298 | - if($strict) { |
|
| 297 | + if (!is_array($source) || ArrayHelper::isAssoc($source)) { |
|
| 298 | + if ($strict) { |
|
| 299 | 299 | throw NestedAccessorException::createAsCannotSetValue( |
| 300 | 300 | self::SET_MODE_APPEND, |
| 301 | 301 | implode($this->pathDelimiter, $path) |
| 302 | 302 | ); |
| 303 | - } elseif(!is_array($source)) { |
|
| 303 | + } elseif (!is_array($source)) { |
|
| 304 | 304 | $source = []; |
| 305 | 305 | } |
| 306 | 306 | } |
@@ -319,8 +319,8 @@ discard block |
||
| 319 | 319 | */ |
| 320 | 320 | protected function _delete(&$source, $key, array $path, bool $strict): bool |
| 321 | 321 | { |
| 322 | - if($key === null || (!is_array($source) && !($source instanceof stdClass))) { |
|
| 323 | - if($strict) { |
|
| 322 | + if ($key === null || (!is_array($source) && !($source instanceof stdClass))) { |
|
| 323 | + if ($strict) { |
|
| 324 | 324 | throw NestedAccessorException::createAsCannotSetValue( |
| 325 | 325 | self::SET_MODE_DELETE, |
| 326 | 326 | implode($this->pathDelimiter, $path) |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | return false; |
| 330 | 330 | } |
| 331 | 331 | } |
| 332 | - if(is_array($source)) { |
|
| 332 | + if (is_array($source)) { |
|
| 333 | 333 | unset($source[$key]); |
| 334 | 334 | } else { |
| 335 | 335 | unset($source->{$key}); |
@@ -344,11 +344,11 @@ discard block |
||
| 344 | 344 | */ |
| 345 | 345 | protected function formatPath($path): array |
| 346 | 346 | { |
| 347 | - if(is_array($path)) { |
|
| 347 | + if (is_array($path)) { |
|
| 348 | 348 | return $path; |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | - if($path === null || $path === '') { |
|
| 351 | + if ($path === null || $path === '') { |
|
| 352 | 352 | return []; |
| 353 | 353 | } |
| 354 | 354 | |