Completed
Push — master ( 965662...ac7f34 )
by Xeriab
07:12
created

Arr::keys()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 15
nc 2
nop 4
1
<?php
2
3
/**
4
 * Konfig.
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * PHP version 5
9
 *
10
 * @category Library
11
 * @package  Konfig
12
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
13
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
14
 * @link     https://xeriab.github.io/projects/konfig
15
 */
16
17
namespace Exen\Konfig;
18
19
use ArrayAccess;
20
use InvalidArgumentException;
21
22
/**
23
 * Arr.
24
 *
25
 * Konfig's array utilities class
26
 *
27
 * @category Main
28
 * @package  Konfig
29
 * @author   Xeriab Nabil (aka KodeBurner) <[email protected]>
30
 * @license  https://raw.github.com/xeriab/konfig/master/LICENSE MIT
31
 * @link     https://xeriab.github.io/projects/konfig
32
 */
33
final class Arr
34
{
35
    /**
36
     * Gets a dot-notated key from an array, with a default value if it does
37
     * not exist.
38
     *
39
     * @param array  $array   The search array
40
     * @param string $key     The dot-notated key or array of keys
41
     * @param string $default The default value
42
     *
43
     * @return mixed
44
     *
45
     * @since              0.1.0
46
     * @codeCoverageIgnore
47
     */
48
    public static function get(array $array, $key, $default = null)
49
    {
50
        if (!is_array($array) && !$array instanceof ArrayAccess) {
51
            throw new InvalidArgumentException(
52
                'First parameter must be an array or ArrayAccess object.'
53
            );
54
        }
55
56
        if (is_null($key)) {
57
            return $array;
58
        }
59
60
        if (is_array($key)) {
61
            $return = [];
62
63
            foreach ($key as $k) {
64
                $return[$k] = self::get($array, $k, $default);
65
            }
66
67
            return $return;
68
        }
69
70
        foreach (explode('.', $key) as $key_part) {
71
            if (($array instanceof ArrayAccess
72
                && isset($array[$key_part])) === false
73
            ) {
74
                if (!is_array($array)
75
                    || !array_key_exists($key_part, $array)
76
                ) {
77
                    return Utils::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
     *
94
     * @return             void Void
95
     * @since              0.1.0
96
     * @codeCoverageIgnore
97
     */
98
    public static function set(array &$array, $key, $value = null)
99
    {
100
        if (is_null($key)) {
101
            $array = $value;
102
103
            return;
104
        }
105
106
        if (is_array($key)) {
107
            foreach ($key as $k => $v) {
108
                self::set($array, $k, $v);
109
            }
110
        } else {
111
            $keys = explode('.', $key);
112
113
            while (count($keys) > 1) {
114
                $key = array_shift($keys);
115
116
                if (!isset($array[$key])
117
                    || !is_array($array[$key])
118
                ) {
119
                    $array[$key] = [];
120
                }
121
122
                $array = &$array[$key];
123
            }
124
125
            $array[array_shift($keys)] = $value;
126
        }
127
    }
128
129
    /**
130
     * Merge two arrays recursively.
131
     *
132
     * @throws InvalidArgumentException
133
     *
134
     * @return array
135
     *
136
     * @since              0.1.0
137
     * @codeCoverageIgnore
138
     */
139
    public static function merge()
140
    {
141
        $array = func_get_arg(0);
142
        $arrays = array_slice(func_get_args(), 1);
143
144
        if (!is_array($array)) {
145
            throw new InvalidArgumentException(
146
                'Exen\Konfig\Arr::merge() - all arguments must be arrays.'
147
            );
148
        }
149
150
        foreach ($arrays as $arr) {
151
            if (!is_array($arr)) {
152
                throw new InvalidArgumentException(
153
                    'Exen\Konfig\Arr::merge() - all arguments must be arrays.'
154
                );
155
            }
156
157
            foreach ($arr as $key => $value) {
158
                // Numeric keys are appended
159
                if (is_int($key)) {
160
                    array_key_exists($key, $array) ?
161
                        array_push($array, $value) :
162
                        $array[$key] = $value;
163
                } elseif (is_array($value)
164
                    && array_key_exists($key, $array)
165
                    && is_array($array[$key])
166
                ) {
167
                    $array[$key] = self::merge($array[$key], $value);
168
                } else {
169
                    $array[$key] = $value;
170
                }
171
            }
172
        }
173
174
        return $array;
175
    }
176
177
    /**
178
     * Merge two arrays recursively | assoc.
179
     *
180
     * @throws InvalidArgumentException
181
     *
182
     * @return array
183
     *
184
     * @since              0.1.0
185
     * @codeCoverageIgnore
186
     */
187
    public static function mergeAssoc()
188
    {
189
        $array = func_get_arg(0);
190
        $arrays = array_slice(func_get_args(), 1);
191
192
        if (!is_array($array)) {
193
            throw new InvalidArgumentException(
194
                'Exen\Konfig\Arr::mergeAssoc() - all arguments must be arrays.'
195
            );
196
        }
197
198
        foreach ($arrays as $arr) {
199
            if (!is_array($arr)) {
200
                throw new InvalidArgumentException(
201
                    'Exen\Konfig\Arr::mergeAssoc() - all arguments must be arrays.'
202
                );
203
            }
204
205
            foreach ($arr as $key => $value) {
206
                if (is_array($value)
207
                    && array_key_exists($key, $array)
208
                    && is_array($array[$key])
209
                ) {
210
                    $array[$key] = self::mergeAssoc($array[$key], $value);
211
                } else {
212
                    $array[$key] = $value;
213
                }
214
            }
215
        }
216
217
        return $array;
218
    }
219
220
    /**
221
     * Un-sets dot-notated key from an array.
222
     *
223
     * @param array $array The search array
224
     * @param mixed $key   The dot-notated key or array of keys
225
     *
226
     * @return mixed
227
     *
228
     * @since              0.1.0
229
     * @codeCoverageIgnore
230
     */
231
    public static function delete(array &$array, $key)
232
    {
233
        if (is_null($key)) {
234
            return false;
235
        }
236
237
        if (is_array($key)) {
238
            $return = [];
239
240
            foreach ($key as $k) {
241
                $return[$k] = self::delete($array, $k);
242
            }
243
244
            return $return;
245
        }
246
247
        $key_parts = explode('.', $key);
248
249
        if (!is_array($array)
250
            || !array_key_exists($key_parts[0], $array)
251
        ) {
252
            return false;
253
        }
254
255
        $this_key = array_shift($key_parts);
256
257
        if (!empty($key_parts)) {
258
            $key = implode('.', $key_parts);
259
260
            return self::delete($array[$this_key], $key);
261
        } else {
262
            unset($array[$this_key]);
263
        }
264
265
        return true;
266
    }
267
268
    /**
269
     * Get array keys recursively.
270
     *
271
     * @param array $array     The search array
272
     * @param int   $maxDepth  The search maximum depth
273
     * @param int   $depth     The search depth
274
     * @param array $arraykeys The array keys
275
     *
276
     * @return array
277
     *
278
     * @since              0.1.0
279
     * @codeCoverageIgnore
280
     */
281
    public static function keys(
282
        array $array,
283
        $maxDepth = INF,
284
        $depth = 0,
285
        array $arraykeys = []
286
    ) {
287
        if ($depth < $maxDepth) {
288
            ++$depth;
289
            $keys = array_keys($array);
290
291
            foreach ($keys as $key) {
292
                if (is_array($array[$key])) {
293
                    $arraykeys[$key] = self::keys(
294
                        $array[$key],
295
                        $maxDepth,
296
                        $depth
297
                    );
298
                }
299
            }
300
        }
301
302
        return $arraykeys;
303
    }
304
305
    /**
306
     * Get array keys recursively.
307
     *
308
     * @param array  $array  The search array
309
     * @param string $search The search value
310
     *
311
     * @return array
312
     *
313
     * @since              0.1.2
314
     * @codeCoverageIgnore
315
     */
316
    public static function recursiveKeys(array $array, $search = null)
317
    {
318
        $return = (
319
            $search !== null ?
320
            array_keys($array, $search) :
321
            array_keys($array)
322
        );
323
324
        foreach ($array as $sub) {
325
            if (is_array($sub)) {
326
                $return = (
327
                    $search !== null ?
328
                    self::merge(
329
                        $return,
330
                        self::recursiveKeys($sub, $search)
331
                    ) : self::merge(
332
                        $return,
333
                        self::recursiveKeys($sub)
334
                    )
335
                );
336
            }
337
        }
338
339
        return $return;
340
    }
341
342
    /**
343
     * __toString.
344
     *
345
     * @return string
346
     *
347
     * @since              0.1.2
348
     * @codeCoverageIgnore
349
     */
350
    public function __toString()
351
    {
352
        return 'Exen\Konfig\Arr'.PHP_EOL;
353
    }
354
}
355
356
// END OF ./src/Arr.php FILE
357