Total Complexity | 16 |
Total Lines | 53 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | abstract class ArrayUtil |
||
6 | { |
||
7 | public const ACCESS_SEPARATOR = '.'; |
||
8 | |||
9 | public static function get(string $path, array $array, $default = null) |
||
10 | { |
||
11 | $splitPath = explode(self::ACCESS_SEPARATOR, $path); |
||
12 | |||
13 | while (is_array($array) && ($key = array_shift($splitPath))) { |
||
14 | if (array_key_exists($key, $array)) { |
||
15 | $array = &$array[$key]; |
||
16 | } else { |
||
17 | $array = null; |
||
18 | } |
||
19 | } |
||
20 | |||
21 | return is_null($array) || !empty($splitPath) ? $default : $array; |
||
22 | } |
||
23 | |||
24 | public static function set(string $path, array &$array, $value = null): void |
||
25 | { |
||
26 | $splitPath = explode(self::ACCESS_SEPARATOR, $path); |
||
27 | |||
28 | while (is_array($array) && ($key = array_shift($splitPath))) { |
||
29 | if (!$splitPath) { |
||
30 | $array[$key] = $value; |
||
31 | } else { |
||
32 | if (array_key_exists($key, $array) && is_array($array[$key])) { |
||
33 | $array = &$array[$key]; |
||
34 | } else { |
||
35 | $array[$key] = []; |
||
36 | $array = &$array[$key]; |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Retorna um nova array de strings dentro do array informado. |
||
44 | * |
||
45 | * @param array $data |
||
46 | * @return array |
||
47 | */ |
||
48 | public static function extractStrings(array $data): array |
||
58 | } |
||
59 | |||
60 | } |