1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sinergi\Config; |
4
|
|
|
|
5
|
|
|
class Parser |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param string $name |
9
|
|
|
* @return array |
10
|
|
|
*/ |
11
|
|
|
public static function getKey($name) |
12
|
|
|
{ |
13
|
|
|
$file = $key = $sub = null; |
14
|
|
|
$parts = explode('.', $name); |
15
|
|
|
if (isset($parts[0])) { |
16
|
|
|
$file = $parts[0]; |
17
|
|
|
} |
18
|
|
|
if (isset($parts[1])) { |
19
|
|
|
$key = $parts[1]; |
20
|
|
|
} |
21
|
|
|
if (isset($parts[2])) { |
22
|
|
|
$sub = []; |
23
|
|
|
foreach (array_slice($parts, 2) as $subkey) { |
24
|
|
|
$sub[] = $subkey; |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
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
|
|
|
public static function getValue(array $haystack = null, $key = null, $sub = null, $default = null) |
39
|
|
|
{ |
40
|
|
|
if (empty($key) && !isset($haystack)) { |
41
|
|
|
return $default; |
42
|
|
|
} elseif (empty($key)) { |
43
|
|
|
if (!isset($haystack) && null !== $default) { |
44
|
|
|
return $default; |
45
|
|
|
} elseif (isset($haystack)) { |
46
|
|
|
return $haystack; |
47
|
|
|
} |
48
|
|
|
return null; |
49
|
|
|
} elseif (!empty($key) && empty($sub)) { |
50
|
|
|
if (empty($haystack[$key]) && null !== $default) { |
51
|
|
|
return $default; |
52
|
|
|
} elseif (isset($haystack[$key])) { |
53
|
|
|
return $haystack[$key]; |
54
|
|
|
} |
55
|
|
|
return null; |
56
|
|
|
} elseif (is_array($sub)) { |
57
|
|
|
$array = isset($haystack[$key]) ? $haystack[$key] : []; |
58
|
|
|
$value = self::findInMultiArray($sub, $array); |
59
|
|
|
if (empty($value) && null !== $default) { |
60
|
|
|
return $default; |
61
|
|
|
} elseif (isset($value)) { |
62
|
|
|
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
|
|
|
private static function findInMultiArray(array $needle, array $haystack) |
75
|
|
|
{ |
76
|
|
|
$currentNeedle = current($needle); |
77
|
|
|
$needle = array_slice($needle, 1); |
78
|
|
|
if (isset($haystack[$currentNeedle]) && is_array($haystack[$currentNeedle]) && count($needle)) { |
79
|
|
|
return self::findInMultiArray($needle, $haystack[$currentNeedle]); |
80
|
|
|
} elseif (isset($haystack[$currentNeedle]) && !is_array($haystack[$currentNeedle]) && count($needle)) { |
81
|
|
|
return null; |
82
|
|
|
} elseif (isset($haystack[$currentNeedle])) { |
83
|
|
|
return $haystack[$currentNeedle]; |
84
|
|
|
} |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|