| @@ 19-40 (lines=22) @@ | ||
| 16 | * @return mixed |
|
| 17 | * @throws \InvalidArgumentException |
|
| 18 | */ |
|
| 19 | public static function readSingleFromPath($array, $path) |
|
| 20 | { |
|
| 21 | if (self::isInvalidPath($path)) { |
|
| 22 | return null; |
|
| 23 | } |
|
| 24 | ||
| 25 | $path = explode('.', $path); |
|
| 26 | $lastKey = array_pop($path); |
|
| 27 | $pointer = &$array; |
|
| 28 | ||
| 29 | foreach ($path as $newPosition) { |
|
| 30 | if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
|
| 31 | unset($pointer); |
|
| 32 | return null; |
|
| 33 | } |
|
| 34 | $pointer = &$pointer[$newPosition]; |
|
| 35 | } |
|
| 36 | ||
| 37 | $value = $pointer[$lastKey]; |
|
| 38 | unset($pointer); |
|
| 39 | return $value; |
|
| 40 | } |
|
| 41 | ||
| 42 | private static function isInvalidPath($path) |
|
| 43 | { |
|
| @@ 79-99 (lines=21) @@ | ||
| 76 | * @param string $type |
|
| 77 | * @return null |
|
| 78 | */ |
|
| 79 | public static function castInPath(&$array, $path, $type) |
|
| 80 | { |
|
| 81 | if (self::isInvalidPath($path)) { |
|
| 82 | return; |
|
| 83 | } |
|
| 84 | ||
| 85 | $path = explode('.', $path); |
|
| 86 | $lastKey = array_pop($path); |
|
| 87 | $pointer = &$array; |
|
| 88 | ||
| 89 | foreach ($path as $newPosition) { |
|
| 90 | if (!isset($pointer[$newPosition]) || !is_array($pointer[$newPosition])) { |
|
| 91 | unset($pointer); |
|
| 92 | return; |
|
| 93 | } |
|
| 94 | $pointer = &$pointer[$newPosition]; |
|
| 95 | } |
|
| 96 | ||
| 97 | settype($pointer[$lastKey], $type); |
|
| 98 | unset($pointer); |
|
| 99 | } |
|
| 100 | ||
| 101 | /** |
|
| 102 | * The writeNestedValue function is simply (over)writing data to arrays, this one tries to merge data when possible |
|