Completed
Push — master ( 5cda8d...7d10f2 )
by Saulius
01:53
created

array_keys()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 2
dl 0
loc 13
ccs 6
cts 6
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/helpers package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Component\Helper;
14
15
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException;
16
17
function array_merge(... $arrays): array
18
{
19 29
    $result = \array_shift($arrays);
20 29
    while (!empty($arrays)) {
21 29
        $nextArray = \array_shift($arrays);
22 29
        array_merge_with_next_array($nextArray, $result);
23
    }
24
25 29
    return $result;
26
}
27
28
function array_merge_with_next_array($nextArray, &$result): void
29
{
30 29
    foreach ($nextArray as $key => $value) {
31 17
        array_merge_with_current_array_values($key, $value, $result);
32
    }
33 29
}
34
35
function array_merge_with_current_array_values($key, $value, &$result): void
36
{
37 17
    if (\is_int($key)) {
38 11
        array_merge_integer_keyed_value($key, $value, $result);
39 8
    } elseif (can_merge_two_value_arrays($key, $value, $result)) {
40 5
        $result[$key] = array_merge($result[$key], $value);
41
    } else {
42 7
        $result[$key] = $value;
43
    }
44 17
}
45
46
function can_merge_two_value_arrays($key, $value, $result): bool
47
{
48 8
    return \is_array($value) && isset($result[$key]) && \is_array($result[$key]);
49
}
50
51
function array_merge_integer_keyed_value($key, $value, &$result): void
52
{
53 11
    if (\array_key_exists($key, $result)) {
54 11
        $result[] = $value;
55
    } else {
56 5
        $result[$key] = $value;
57
    }
58 11
}
59
60
/**
61
 * @throws PropertyNotAccessibleException
62
 */
63
function array_get_value($array, $key, $default = null)
64
{
65 35
    if (\is_callable($key)) {
66 1
        return $key($array, $default);
67
    }
68
69 34
    return array_get_value_from_array_key_path($array, $key, $default);
70
}
71
72
/**
73
 * @throws PropertyNotAccessibleException
74
 *
75
 */
76
function array_get_value_from_array_key_path($array, $key, $default)
77
{
78 34
    if (\is_array($key)) {
79 2
        $lastKey = array_pop($key);
80 2
        foreach ($key as $keyPart) {
81 2
            $array = array_get_value($array, $keyPart);
82
        }
83 2
        $key = $lastKey;
84
    }
85
86 34
    if (is_array_and_key_exists($array, $key)) {
87 29
        return $array[$key];
88
    }
89
90 9
    return array_get_value_from_array_string_key_path($array, $key, $default);
91
}
92
93
/**
94
 * @throws PropertyNotAccessibleException
95
 */
96
function array_get_value_from_array_string_key_path($array, $key, $default)
97
{
98 9
    if ((bool)($pos = strrpos($key, '.')) !== false) {
99 4
        $array = array_get_value($array, substr($key, 0, $pos), $default);
100 4
        $key = substr($key, $pos + 1);
101
    }
102
103 9
    if (\is_object($array)) {
104 1
        return get_object_property_value($array, $key) ?? $default;
105
    }
106
107 8
    if (\is_array($array)) {
108 6
        return get_array_value_or_fallback_to_default($array, $key, $default);
109
    }
110
111 2
    return $default;
112
}
113
114
function get_array_value_or_fallback_to_default($array, $key, $default)
115
{
116 6
    return (isset($array[$key]) || \array_key_exists($key, $array)) ? $array[$key] : $default;
117
}
118
119
function is_array_and_key_exists($array, $key): bool
120
{
121 34
    return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array));
122
}
123
124
/**
125
 * @param mixed $path
126
 * @param mixed $value
127
 *
128
 * @return mixed
129
 */
130
function array_set_value(array &$array, $path, $value)
131
{
132 7
    if ($path === null) {
133 2
        $array = $value;
134
135 2
        return;
136
    }
137 5
    $keys = \is_array($path) ? $path : \explode('.', $path);
138 5
    while (\count($keys) > 1) {
139 3
        $key = \array_shift($keys);
140
141 3
        array_create_value($array, $key);
142
143 3
        $array = &$array[$key];
144
    }
145
146 5
    $array[\array_shift($keys)] = $value;
147 5
}
148
149
function array_create_value(array &$array, $key): void
150
{
151 3
    if (!isset($array[$key])) {
152 2
        $array[$key] = [];
153
    }
154
155 3
    if (!\is_array($array[$key])) {
156 1
        $array[$key] = [$array[$key]];
157
    }
158 3
}
159
160
function array_remove_key(&$array, $key, $default = null)
161
{
162 8
    $array = &array_remove_shift_key_array_value($array, $key);
163
164 8
    if (can_remove_array_key($array, $key)) {
165 5
        return array_unset_key($array, $key);
166
    }
167
168 3
    return $default;
169
}
170
171
function &array_remove_shift_key_array_value(&$array, &$key): array
172
{
173 8
    $keys = parse_array_key_path($key);
174 8
    while (\count($keys) > 1) {
175 4
        $key = \array_shift($keys);
176
177 4
        if (\is_array($array[$key])) {
178 4
            $array = &$array[$key];
179
        }
180
    }
181
182 8
    $key = \array_shift($keys);
183
184 8
    return $array;
185
}
186
187
function array_unset_key(&$array, $key)
188
{
189 5
    $value = $array[$key];
190 5
    unset($array[$key]);
191
192 5
    return $value;
193
}
194
195
function can_remove_array_key($array, $key): bool
196
{
197 8
    return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array));
198
}
199
200
function array_key_exists(array $array, $key): bool
201
{
202 12
    if (array_string_key_exists($array, $key)) {
203 4
        return true;
204
    }
205
206 9
    $keys = parse_array_key_path($key);
207
208 9
    while (\count($keys) > 1) {
209 6
        $key = \array_shift($keys);
210
211 6
        if (array_key_isset_and_is_array($array, $key)) {
212 5
            $array = &$array[$key];
213
        }
214
    }
215
216 9
    $key = \array_shift($keys);
217
218 9
    return isset($array[$key]) || \array_key_exists($key, $array);
219
}
220
221
function array_string_key_exists(array $array, $key): bool
222
{
223 12
    return \is_string($key) && \array_key_exists($key, $array);
224
}
225
226
function array_key_isset_and_is_array(array $array, $key): bool
227
{
228 6
    return isset($array[$key]) && \is_array($array[$key]);
229
}
230
231
/**
232
 * @param mixed|string|array $key
233
 */
234
function parse_array_key_path($key): array
235
{
236 17
    return \is_array($key) ? $key : \explode('.', $key);
237
}
238
239
function array_remove_value(&$array, $value): array
240
{
241 3
    $result = [];
242 3
    if (\is_array($array)) {
243 3
        foreach ($array as $key => $val) {
244 2
            if ($val === $value) {
245 2
                $result[$key] = $val;
246 2
                unset($array[$key]);
247
            }
248
        }
249
    }
250
251 3
    return $result;
252
}
253
254
function array_deep_search(array $array, $searchValue)
255
{
256 8
    $result = [];
257 8
    foreach ($array as $key => $value) {
258 6
        if ($path = array_deep_search_value($key, $value, $searchValue)) {
259 6
            $result[] = $path;
260
        }
261
    }
262
263 8
    return $result;
264
}
265
266
function array_deep_search_value($key, $value, $searchValue, $path = [])
267
{
268 7
    if (\is_array($value) && $subPath = array_deep_search($value, $searchValue)) {
269 4
        return array_flatten(array_merge($path, [$key], $subPath));
270
    }
271
272 7
    if ($value === $searchValue) {
273 5
        return [$key];
274
    }
275
276 7
    return [];
277
}
278
279
/**
280
 * @throws \RuntimeException
281
 */
282
function array_flatten(array $array): array
283
{
284
    try {
285 9
        $result = [];
286 9
        foreach (\array_keys($array) as $key) {
287 8
            array_flatten_value($array[$key], $result);
288
        }
289
290 8
        return \array_values(\array_unique($result));
291 1
    } catch (\Throwable $t) {
292 1
        throw new \RuntimeException($t->getMessage());
293
    }
294
}
295
296
/**
297
 * @param mixed $value
298
 */
299
function array_flatten_value($value, array &$result): void
300
{
301 8
    if (\is_scalar($value)) {
302 8
        $result[] = $value;
303
    }
304
305 8
    if (\is_array($value)) {
306 6
        $result = array_merge($result, array_flatten($value));
307
    }
308
309 8
    if (\is_object($value)) {
310 2
        $result[] = (string)$value;
311
    }
312 8
}
313
314
function array_multiple_keys_exists(array $array, array $keys): bool
315
{
316 1
    if (empty($keys)) {
317 1
        return false;
318
    }
319
320 1
    $result = true;
321
322 1
    foreach ($keys as $key) {
323 1
        $result &= array_key_exists($array, $key);
324
    }
325
326 1
    return $result ? true : false;
327
}
328
329
function array_keys(array $array, string $prefix = '')
330
{
331 1
    $result = [];
332
333 1
    foreach ($array as $key => $value) {
334 1
        if (\is_array($value) && !empty($value)) {
335 1
            $result = array_merge($result, array_keys($value, $prefix.$key.'.'));
336
        } else {
337 1
            $result[$prefix.$key] = $value;
338
        }
339
    }
340
341 1
    return $result;
342
}
343