Completed
Push — master ( d0ff07...576091 )
by Xeriab
04:55
created

Arr::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Konfig
5
 *
6
 * Yet another simple configuration loader library.
7
 *
8
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
9
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
10
 * @link    https://xeriab.github.io/projects/konfig
11
 */
12
13
namespace Exen\Konfig;
14
15
use ArrayAccess;
16
use InvalidArgumentException;
17
18
final class Arr
19
{
20
    
21
    /**
22
     * Gets a dot-notated key from an array, with a default value if it does
23
     * not exist.
24
     *
25
     * @param array $array The search array
26
     * @param mixed $key The dot-notated key or array of keys
27
     * @param string $default The default value
28
     * @return mixed
29
     * @since 0.1.0
30
     */
31
    public static function get(array $array, $key, $default = null)
32
    {
33
        if (!is_array($array) && !$array instanceof ArrayAccess) {
34
            throw new InvalidArgumentException('First parameter must be an array or ArrayAccess object.');
35
        }
36
37
        if (is_null($key)) {
38
            return $array;
39
        }
40
41
        if (is_array($key)) {
42
            $return = [];
43
44
            foreach ($key as $k) {
45
                $return[$k] = self::get($array, $k, $default);
46
            }
47
48
            return $return;
49
        }
50
51
        foreach (explode('.', $key) as $key_part) {
52
            if (($array instanceof ArrayAccess && isset($array[$key_part])) === false) {
53
                if (!is_array($array) or ! array_key_exists($key_part, $array)) {
54
                    return self::checkValue($default);
0 ignored issues
show
Bug introduced by
The method checkValue() does not seem to exist on object<Exen\Konfig\Arr>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
                }
56
            }
57
58
            $array = $array[$key_part];
59
        }
60
61
        return $array;
62
    }
63
64
    /**
65
     * Set an array item (dot-notated) to the value.
66
     *
67
     * @param array $array The array to insert it into
68
     * @param mixed $key The dot-notated key to set or array of keys
69
     * @param mixed $value The value
70
     * @return void
71
     * @since 0.1.0
72
     */
73
    public static function set(&$array, $key, $value = null)
74
    {
75
        if (is_null($key)) {
76
            $array = $value;
77
            return;
78
        }
79
80
        if (is_array($key)) {
81
            foreach ($key as $k => $v) {
82
                self::set($array, $k, $v);
83
            }
84
        } else {
85
            $keys = explode('.', $key);
86
87
            while (count($keys) > 1) {
88
                $key = array_shift($keys);
89
90
                if (!isset($array[$key]) or ! is_array($array[$key])) {
91
                    $array[$key] = array();
92
                }
93
94
                $array = &$array[$key];
95
            }
96
97
            $array[array_shift($keys)] = $value;
98
        }
99
    }
100
101
    /**
102
     * Merge two arrays recursively, differs in two important ways from array_merge_recursive()
103
     * - When there's two different values and not both arrays, the latter value overwrites the earlier
104
     *   instead of merging both into an array
105
     * - Numeric keys that don't conflict aren't changed, only when a numeric key already exists is the
106
     *   value added using array_push()
107
     *
108
     * @param array multiple variables all of which must be arrays
109
     * @throws InvalidArgumentException
110
     * @return array
111
     * @since 0.1.0
112
     */
113
    public static function merge()
114
    {
115
        $array = func_get_arg(0);
116
        $arrays = array_slice(func_get_args(), 1);
117
118
        if (!is_array($array)) {
119
            throw new InvalidArgumentException('Exen\Konfig\Arr::merge() - all arguments must be arrays.');
120
        }
121
122
        foreach ($arrays as $arr) {
123
            if (!is_array($arr)) {
124
                throw new InvalidArgumentException('Exen\Konfig\Arr::merge() - all arguments must be arrays.');
125
            }
126
127
            foreach ($arr as $k => $v) {
128
                // Numeric keys are appended
129
                if (is_int($k)) {
130
                    array_key_exists($k, $array) ? array_push($array, $v) : $array[$k] = $v;
131
                } elseif (is_array($v) && array_key_exists($k, $array) && is_array($array[$k])) {
132
                    $array[$k] = self::merge($array[$k], $v);
133
                } else {
134
                    $array[$k] = $v;
135
                }
136
            }
137
        }
138
139
        return $array;
140
    }
141
    
142
    /**
143
     * Merge 2 arrays recursively, differs in 2 important ways from array_merge_recursive()
144
     * - When there's 2 different values and not both arrays, the latter value overwrites the earlier
145
     *   instead of merging both into an array
146
     * - Numeric keys are never changed
147
     *
148
     * @param array multiple variables all of which must be arrays
149
     * @throws InvalidArgumentException
150
     * @return array
151
     * @since 0.1.0
152
     */
153
    public static function mergeAssoc()
154
    {
155
        $array = func_get_arg(0);
156
        $arrays = array_slice(func_get_args(), 1);
157
158
        if (!is_array($array)) {
159
            throw new InvalidArgumentException('Exen\Konfig\Arr::mergeAssoc() - all arguments must be arrays.');
160
        }
161
162
        foreach ($arrays as $arr) {
163
            if (!is_array($arr)) {
164
                throw new InvalidArgumentException('Exen\Konfig\Arr::mergeAssoc() - all arguments must be arrays.');
165
            }
166
167
            foreach ($arr as $k => $v) {
168
                if (is_array($v) && array_key_exists($k, $array) && is_array($array[$k])) {
169
                    $array[$k] = static::mergeAssoc($array[$k], $v);
170
                } else {
171
                    $array[$k] = $v;
172
                }
173
            }
174
        }
175
176
        return $array;
177
    }
178
179
    /**
180
     * Un-sets dot-notated key from an array
181
     *
182
     * @param array $array The search array
183
     * @param mixed $key The dot-notated key or array of keys
184
     * @return mixed
185
     * @since 0.1.0
186
     */
187
    public static function delete(array &$array, $key)
188
    {
189
        if (is_null($key)) {
190
            return false;
191
        }
192
193
        if (is_array($key)) {
194
            $return = [];
195
196
            foreach ($key as $k) {
197
                $return[$k] = self::delete($array, $k);
198
            }
199
200
            return $return;
201
        }
202
203
        $key_parts = explode('.', $key);
204
205
        if (!is_array($array) or ! array_key_exists($key_parts[0], $array)) {
206
            return false;
207
        }
208
209
        $this_key = array_shift($key_parts);
210
211
        if (!empty($key_parts)) {
212
            $key = implode('.', $key_parts);
213
            return self::delete($array[$this_key], $key);
214
        } else {
215
            unset($array[$this_key]);
216
        }
217
218
        return true;
219
    }
220
221
    /**
222
     * Get array keys recursively
223
     *
224
     * @param array $array The search array
225
     * @param int $maxDepth The search maximum depth
226
     * @param int $depth The search depth
227
     * @param array $arraykeys The array keys
228
     * @return array
229
     * @since 0.1.0
230
     */
231
    public static function keys(array $array, $maxDepth = INF, $depth = 0, array $arraykeys = [])
232
    {
233
        if ($depth < $maxDepth) {
234
            $depth++;
235
            $keys = array_keys($array);
236
237
            foreach ($keys as $key) {
238
                if (is_array($array[$key])) {
239
                    $arraykeys[$key] = self::keys($array[$key], $maxDepth, $depth);
240
                }
241
            }
242
        }
243
244
        return $arraykeys;
245
    }
246
    
247
    /**
248
     * Get array keys recursively
249
     * 
250
     * @param array $array The search array
251
     * @param type $search The search value
252
     * @return array
253
     * @since 0.1.2
254
     */
255
    public static function recursiveKeys(array $array, $search = null)
256
    {
257
        $return = (
258
            $search !== null ? 
259
            array_keys($array, $search) : 
260
            array_keys($array)
261
        );
262
        
263
        foreach ($array as $sub) {
264
            if (is_array($sub)) {
265
                $return = (
266
                    $search !== null ? 
267
                    self::merge($return, self::recursiveKeys($sub, $search)) : 
268
                    self::merge($return, self::recursiveKeys($sub))
269
                );
270
            }
271
        }
272
        
273
        return $return;
274
    }
275
276
    /**
277
     * @return string
278
     * @since 0.1.2
279
     */
280
    public function __toString()
281
    {
282
        return 'Exen\Konfig\Arr' . PHP_EOL;
283
    }
284
}
285
286
#: END OF ./src/Arr.php FILE
287