1 | <?php |
||
5 | class Parser |
||
6 | { |
||
7 | /** |
||
8 | * @param string $name |
||
9 | * @return array |
||
10 | */ |
||
11 | 21 | public static function getKey($name) |
|
12 | { |
||
13 | 21 | $file = $key = $sub = null; |
|
14 | 21 | $parts = explode('.', $name); |
|
15 | 21 | if (isset($parts[0])) { |
|
16 | 21 | $file = $parts[0]; |
|
17 | } |
||
18 | 21 | if (isset($parts[1])) { |
|
19 | 18 | $key = $parts[1]; |
|
20 | } |
||
21 | 21 | if (isset($parts[2])) { |
|
22 | 9 | $sub = []; |
|
23 | 9 | foreach (array_slice($parts, 2) as $subkey) { |
|
24 | 9 | $sub[] = $subkey; |
|
25 | } |
||
26 | } |
||
27 | |||
28 | 21 | return array($file, $key, $sub); |
|
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param array $haystack |
||
33 | * @param null|string $key |
||
34 | * @param null|array $sub |
||
35 | * @param null|mixed $default |
||
36 | * @return mixed |
||
37 | */ |
||
38 | 19 | public static function getValue(array $haystack = null, $key = null, $sub = null, $default = null) |
|
39 | { |
||
40 | 19 | if (empty($key) && !isset($haystack)) { |
|
41 | return $default; |
||
42 | } elseif (empty($key)) { |
||
43 | 3 | if (!isset($haystack) && null !== $default) { |
|
44 | return $default; |
||
45 | } elseif (isset($haystack)) { |
||
46 | 3 | return $haystack; |
|
47 | } |
||
48 | return null; |
||
49 | } elseif (!empty($key) && empty($sub)) { |
||
50 | 9 | if (empty($haystack[$key]) && null !== $default) { |
|
51 | 3 | return $default; |
|
52 | 6 | } elseif (isset($haystack[$key])) { |
|
53 | 6 | return $haystack[$key]; |
|
54 | } |
||
55 | return null; |
||
56 | 7 | } elseif (is_array($sub)) { |
|
57 | 7 | $array = isset($haystack[$key]) ? $haystack[$key] : []; |
|
58 | 7 | $value = self::findInMultiArray($sub, $array); |
|
59 | 7 | if (empty($value) && null !== $default) { |
|
60 | 2 | return $default; |
|
61 | } elseif (isset($value)) { |
||
62 | 5 | return $value; |
|
63 | } |
||
64 | return null; |
||
65 | } |
||
66 | return null; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param array $needle |
||
71 | * @param array $haystack |
||
72 | * @return mixed |
||
73 | */ |
||
74 | 7 | private static function findInMultiArray(array $needle, array $haystack) |
|
87 | } |
||
88 |