1 | <?php |
||
5 | class Arr |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * Constant that represents the root of an array |
||
10 | */ |
||
11 | const PATH_ROOT = '/'; |
||
12 | |||
13 | /** |
||
14 | * @param $selector |
||
15 | * |
||
16 | * @return array |
||
17 | */ |
||
18 | 36 | protected static function getSelectorParts($selector) |
|
33 | |||
34 | /** |
||
35 | * Retrieves an element from an array via its path |
||
36 | * Path examples: |
||
37 | * key |
||
38 | * key[subkey] |
||
39 | * key[0][subkey] |
||
40 | * |
||
41 | * @param array $array |
||
42 | * @param string $path |
||
43 | * |
||
44 | * @return mixed |
||
45 | */ |
||
46 | 37 | public static function getByPath($array, $path = self::PATH_ROOT) |
|
64 | |||
65 | /** |
||
66 | * Set values in the array by selector |
||
67 | * |
||
68 | * @example |
||
69 | * Arr::setBySelector($data, 'email', '[email protected]'); |
||
70 | * Arr::setBySelector($data, 'addresses[0][line]', null); |
||
71 | * Arr::setBySelector($data, 'addresses[*][line]', null); |
||
72 | * |
||
73 | * @param array $array |
||
74 | * @param string $selector |
||
75 | * @param mixed $value |
||
76 | * @param bool $overwrite true if the $value should overwrite the existing value |
||
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | 3 | public static function setBySelector($array, $selector, $value, $overwrite = false) |
|
114 | |||
115 | /** |
||
116 | * Get values in the array by selector |
||
117 | * |
||
118 | * @example |
||
119 | * Arr::getBySelector($data, 'email'); |
||
120 | * Arr::getBySelector($data, 'addresses[0][line]'); |
||
121 | * Arr::getBySelector($data, 'addresses[*][line]'); |
||
122 | * |
||
123 | * @param $array |
||
124 | * @param $selector |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | 18 | public static function getBySelector($array, $selector) |
|
158 | } |
||
159 |