Passed
Push — master ( aa69e8...feec82 )
by Saulius
02:28
created

array_key_assoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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 31
    $result = \array_shift($arrays);
20 31
    while (!empty($arrays)) {
21 31
        $nextArray = \array_shift($arrays);
22 31
        array_merge_with_next_array($nextArray, $result);
23
    }
24
25 31
    return $result;
26
}
27
28
function array_merge_with_next_array($nextArray, &$result): void
29
{
30 31
    foreach ($nextArray as $key => $value) {
31 19
        array_merge_with_current_array_values($key, $value, $result);
32
    }
33 31
}
34
35
function array_merge_with_current_array_values($key, $value, &$result): void
36
{
37 19
    if (\is_int($key)) {
38 11
        array_merge_integer_keyed_value($key, $value, $result);
39 10
    } elseif (can_merge_two_value_arrays($key, $value, $result)) {
40 5
        $result[$key] = array_merge($result[$key], $value);
41
    } else {
42 9
        $result[$key] = $value;
43
    }
44 19
}
45
46
function can_merge_two_value_arrays($key, $value, $result): bool
47
{
48 10
    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 11
    $keys = parse_array_key_path($key);
203
204 11
    while (\count($keys) > 1) {
205 6
        $key = \array_shift($keys);
206
207 6
        if (array_key_isset_and_is_array($array, $key)) {
208 5
            $array = &$array[$key];
209
        }
210
    }
211
212 11
    $key = \array_shift($keys);
213
214 11
    return isset($array[$key]) || \array_key_exists($key, $array);
215
}
216
217
function array_key_isset_and_is_array(array $array, $key): bool
218
{
219 6
    return isset($array[$key]) && \is_array($array[$key]);
220
}
221
222
/**
223
 * @param mixed|string|array $key
224
 */
225
function parse_array_key_path($key): array
226
{
227 19
    return \is_array($key) ? $key : \explode('.', $key);
228
}
229
230
function array_remove_value(&$array, $value): array
231
{
232 3
    $result = [];
233 3
    if (\is_array($array)) {
234 3
        foreach ($array as $key => $val) {
235 2
            if ($val === $value) {
236 2
                $result[$key] = $val;
237 2
                unset($array[$key]);
238
            }
239
        }
240
    }
241
242 3
    return $result;
243
}
244
245
function array_deep_search(array $array, $searchValue)
246
{
247 8
    $result = [];
248 8
    foreach ($array as $key => $value) {
249 6
        if ($path = array_deep_search_value($key, $value, $searchValue)) {
250 6
            $result[] = $path;
251
        }
252
    }
253
254 8
    return $result;
255
}
256
257
function array_deep_search_value($key, $value, $searchValue, $path = [])
258
{
259 7
    if (\is_array($value) && $subPath = array_deep_search($value, $searchValue)) {
260 4
        return array_flatten(array_merge($path, [$key], $subPath));
261
    }
262
263 7
    if ($value === $searchValue) {
264 5
        return [$key];
265
    }
266
267 7
    return [];
268
}
269
270
/**
271
 * @throws \RuntimeException
272
 */
273
function array_flatten(array $array): array
274
{
275
    try {
276 9
        $result = [];
277 9
        foreach (\array_keys($array) as $key) {
278 8
            array_flatten_value($array[$key], $result);
279
        }
280
281 8
        return \array_values(\array_unique($result));
282 1
    } catch (\Throwable $t) {
283 1
        throw new \RuntimeException($t->getMessage());
284
    }
285
}
286
287
/**
288
 * @param mixed $value
289
 */
290
function array_flatten_value($value, array &$result): void
291
{
292 8
    if (\is_scalar($value)) {
293 8
        $result[] = $value;
294
    }
295
296 8
    if (\is_array($value)) {
297 6
        $result = array_merge($result, array_flatten($value));
298
    }
299
300 8
    if (\is_object($value)) {
301 2
        $result[] = (string)$value;
302
    }
303 8
}
304
305
function array_multiple_keys_exists(array $array, array $keys): bool
306
{
307 1
    if (empty($keys)) {
308 1
        return false;
309
    }
310
311 1
    $result = true;
312
313 1
    foreach ($keys as $key) {
314 1
        $result &= array_key_exists($array, $key);
315
    }
316
317 1
    return $result ? true : false;
318
}
319
320
function array_diff_key_assoc(array $array1, array $array2)
321
{
322 1
    return \array_diff_key(array_key_assoc_with_value($array1), array_key_assoc_with_value($array2));
323
}
324
325
function array_key_assoc_with_value(array $array, string $prefix = '')
326
{
327 3
    $result = [];
328
329 3
    foreach ($array as $key => $value) {
330 3
        if (\is_array($value) && !empty($value)) {
331 3
            $result = array_merge($result, array_key_assoc_with_value($value, $prefix.$key.'.'));
332
        } else {
333 3
            $result[$prefix.$key] = $value;
334
        }
335
    }
336
337 3
    return $result;
338
}
339
340
function array_key_assoc(array $array)
341
{
342 1
    return \array_keys(array_key_assoc_with_value($array));
343
}
344