|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Konfig |
|
4
|
|
|
* |
|
5
|
|
|
* Yet another simple configuration file loader library. |
|
6
|
|
|
* |
|
7
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
|
8
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
|
9
|
|
|
* @link https://xeriab.github.io/projects/konfig |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Exen\Konfig; |
|
12
|
|
|
|
|
13
|
|
|
use ArrayAccess; |
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
class Utils |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Includes the given file and returns the results. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string The path to the file |
|
22
|
|
|
* @return mixed The results of the include |
|
23
|
|
|
* @since 0.1 |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function load($file = null) |
|
26
|
|
|
{ |
|
27
|
|
|
return require_once $file; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Takes a value and checks if it's a Closure or not, if it's a Closure it |
|
32
|
|
|
* will return the result of the closure, if not, it will simply return the |
|
33
|
|
|
* value. |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $var The value to get |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
* @since 0.1 |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function checkValue($var = null) |
|
40
|
|
|
{ |
|
41
|
|
|
return ($var instanceof \Closure) ? $var() : $var; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Gets a dot-notated key from an array, with a default value if it does |
|
46
|
|
|
* not exist. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $array The search array |
|
49
|
|
|
* @param mixed $key The dot-notated key or array of keys |
|
50
|
|
|
* @param string $default The default value |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
* @since 0.1 |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function arrayGet($array, $key, $default = null) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!is_array($array) and !$array instanceof ArrayAccess) { |
|
57
|
|
|
throw new InvalidArgumentException('First parameter must be an array or ArrayAccess object.'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if (is_null($key)) { |
|
61
|
|
|
return $array; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (is_array($key)) { |
|
65
|
|
|
$return = []; |
|
66
|
|
|
|
|
67
|
|
|
foreach ($key as $k) { |
|
68
|
|
|
$return[$k] = static::arrayGet($array, $k, $default); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $return; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
foreach (explode('.', $key) as $key_part) { |
|
75
|
|
|
if (($array instanceof ArrayAccess and isset($array[$key_part])) === false) { |
|
76
|
|
|
if (!is_array($array) or !array_key_exists($key_part, $array)) { |
|
77
|
|
|
return static::checkValue($default); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$array = $array[$key_part]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $array; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Set an array item (dot-notated) to the value. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $array The array to insert it into |
|
91
|
|
|
* @param mixed $key The dot-notated key to set or array of keys |
|
92
|
|
|
* @param mixed $value The value |
|
93
|
|
|
* @return void |
|
94
|
|
|
* @since 0.1 |
|
95
|
|
|
*/ |
|
96
|
|
|
public static function arraySet(&$array, $key, $value = null) |
|
97
|
|
|
{ |
|
98
|
|
|
if (is_null($key)) { |
|
99
|
|
|
$array = $value; |
|
100
|
|
|
return; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (is_array($key)) { |
|
104
|
|
|
foreach ($key as $k => $v) { |
|
105
|
|
|
static::arraySet($array, $k, $v); |
|
106
|
|
|
} |
|
107
|
|
|
} else { |
|
108
|
|
|
$keys = explode('.', $key); |
|
109
|
|
|
|
|
110
|
|
|
while (count($keys) > 1) { |
|
111
|
|
|
$key = array_shift($keys); |
|
112
|
|
|
|
|
113
|
|
|
if (!isset($array[$key]) or !is_array($array[$key])) { |
|
114
|
|
|
$array[$key] = array(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$array = &$array[$key]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$array[array_shift($keys)] = $value; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Merge two arrays recursively, differs in two important ways from array_merge_recursive() |
|
126
|
|
|
* - When there's two different values and not both arrays, the latter value overwrites the earlier |
|
127
|
|
|
* instead of merging both into an array |
|
128
|
|
|
* - Numeric keys that don't conflict aren't changed, only when a numeric key already exists is the |
|
129
|
|
|
* value added using array_push() |
|
130
|
|
|
* |
|
131
|
|
|
* @param array multiple variables all of which must be arrays |
|
132
|
|
|
* @throws InvalidArgumentException |
|
133
|
|
|
* @return array |
|
134
|
|
|
* @since 0.1 |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function arrayMerge() |
|
137
|
|
|
{ |
|
138
|
|
|
$array = func_get_arg(0); |
|
139
|
|
|
$arrays = array_slice(func_get_args(), 1); |
|
140
|
|
|
|
|
141
|
|
|
if (!is_array($array)) { |
|
142
|
|
|
throw new InvalidArgumentException('Exen\Konfig\Utils::arrayMerge() - all arguments must be arrays.'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
foreach ($arrays as $arr) { |
|
146
|
|
|
if (!is_array($arr)) { |
|
147
|
|
|
throw new InvalidArgumentException('Exen\Konfig\Utils::arrayMerge() - all arguments must be arrays.'); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
foreach ($arr as $k => $v) { |
|
151
|
|
|
// Numeric keys are appended |
|
152
|
|
|
if (is_int($k)) { |
|
153
|
|
|
array_key_exists($k, $array) ? array_push($array, $v) : $array[$k] = $v; |
|
154
|
|
|
} elseif (is_array($v) and array_key_exists($k, $array) and is_array($array[$k])) { |
|
155
|
|
|
$array[$k] = static::arrayMerge($array[$k], $v); |
|
156
|
|
|
} else { |
|
157
|
|
|
$array[$k] = $v; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $array; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Unsets dot-notated key from an array |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $array The search array |
|
169
|
|
|
* @param mixed $key The dot-notated key or array of keys |
|
170
|
|
|
* @return mixed |
|
171
|
|
|
* @since 0.1 |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function arrayDelete(&$array, $key) |
|
174
|
|
|
{ |
|
175
|
|
|
if (is_null($key)) { |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
if (is_array($key)) { |
|
180
|
|
|
$return = []; |
|
181
|
|
|
|
|
182
|
|
|
foreach ($key as $k) { |
|
183
|
|
|
$return[$k] = static::arrayDelete($array, $k); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $return; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
$key_parts = explode('.', $key); |
|
190
|
|
|
|
|
191
|
|
|
if (!is_array($array) or !array_key_exists($key_parts[0], $array)) { |
|
192
|
|
|
return false; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$this_key = array_shift($key_parts); |
|
196
|
|
|
|
|
197
|
|
|
if (!empty($key_parts)) { |
|
198
|
|
|
$key = implode('.', $key_parts); |
|
199
|
|
|
return static::arrayDelete($array[$this_key], $key); |
|
200
|
|
|
} else { |
|
201
|
|
|
unset($array[$this_key]); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return true; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get array keys recursively |
|
209
|
|
|
* |
|
210
|
|
|
* @param array $array The search array |
|
211
|
|
|
* @param int $maxDepth The search maximum depth |
|
212
|
|
|
* @param int $depth The search depth |
|
213
|
|
|
* @param array $arrayKeys The array keys |
|
214
|
|
|
* @return array |
|
215
|
|
|
* @since 0.1 |
|
216
|
|
|
*/ |
|
217
|
|
|
public static function arrayKeys($array, $maxDepth = INF, $depth = 0, $arrayKeys = []) |
|
218
|
|
|
{ |
|
219
|
|
|
if ($depth < $maxDepth) { |
|
220
|
|
|
$depth++; |
|
221
|
|
|
$keys = array_keys($array); |
|
222
|
|
|
|
|
223
|
|
|
foreach ($keys as $key) { |
|
224
|
|
|
if (is_array($array[$key])) { |
|
225
|
|
|
$arrayKeys[$key] = arrayKeys($array[$key], $maxDepth, $depth); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $array; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
#: END OF ./src/Utils.php FILE |
|
235
|
|
|
|