Total Complexity | 16 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
12 | class ArrayHelper |
||
13 | { |
||
14 | /** |
||
15 | * @param $array |
||
16 | * @param $key |
||
17 | * @param null $default |
||
|
|||
18 | * @return mixed|null |
||
19 | */ |
||
20 | public static function getValue($array, $key, $default = null) |
||
21 | { |
||
22 | if ($key instanceof Closure) { |
||
23 | return $key($array, $default); |
||
24 | } |
||
25 | |||
26 | if (is_array($key)) { |
||
27 | $lastKey = array_pop($key); |
||
28 | foreach ($key as $keyPart) { |
||
29 | $array = static::getValue($array, $keyPart); |
||
30 | } |
||
31 | $key = $lastKey; |
||
32 | } |
||
33 | |||
34 | if (is_array($array) && (isset($array[$key]) || array_key_exists($key, $array))) { |
||
35 | return $array[$key]; |
||
36 | } |
||
37 | |||
38 | if (($pos = strrpos($key, '.')) !== false) { |
||
39 | $array = static::getValue($array, substr($key, 0, $pos), $default); |
||
40 | $key = substr($key, $pos + 1); |
||
41 | } |
||
42 | |||
43 | if (is_object($array)) { |
||
44 | // this is expected to fail if the property does not exist, or __get() is not implemented |
||
45 | // it is not reliably possible to check whether a property is accessible beforehand |
||
46 | return $array->$key; |
||
47 | } elseif (is_array($array)) { |
||
48 | return (isset($array[$key]) || array_key_exists($key, $array)) ? $array[$key] : $default; |
||
49 | } |
||
50 | |||
51 | return $default; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param $array |
||
56 | * @param $key |
||
57 | * @param null $default |
||
58 | * @return mixed|null |
||
59 | */ |
||
60 | public static function remove(&$array, $key, $default = null) |
||
70 | } |
||
71 | } |
||
72 |