|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Northwoods\Config; |
|
4
|
|
|
|
|
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) |
|
75
|
|
|
{ |
|
76
|
7 |
|
$currentNeedle = current($needle); |
|
77
|
7 |
|
$needle = array_slice($needle, 1); |
|
78
|
7 |
|
if (isset($haystack[$currentNeedle]) && is_array($haystack[$currentNeedle]) && count($needle)) { |
|
79
|
5 |
|
return self::findInMultiArray($needle, $haystack[$currentNeedle]); |
|
80
|
7 |
|
} elseif (isset($haystack[$currentNeedle]) && !is_array($haystack[$currentNeedle]) && count($needle)) { |
|
81
|
|
|
return null; |
|
82
|
7 |
|
} elseif (isset($haystack[$currentNeedle])) { |
|
83
|
5 |
|
return $haystack[$currentNeedle]; |
|
84
|
|
|
} |
|
85
|
2 |
|
return null; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|