Completed
Push — master ( 2011bb...c3e86f )
by Saulius
03:40
created

array_key_exists()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 6
nop 2
dl 0
loc 15
ccs 7
cts 7
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 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_string_key_exists(array $array, $key): bool
218
{
219
    return \is_string($key) && \array_key_exists($key, $array);
220
}
221
222
function array_key_isset_and_is_array(array $array, $key): bool
223
{
224 6
    return isset($array[$key]) && \is_array($array[$key]);
225
}
226
227
/**
228
 * @param mixed|string|array $key
229
 */
230
function parse_array_key_path($key): array
231
{
232 19
    return \is_array($key) ? $key : \explode('.', $key);
233
}
234
235
function array_remove_value(&$array, $value): array
236
{
237 3
    $result = [];
238 3
    if (\is_array($array)) {
239 3
        foreach ($array as $key => $val) {
240 2
            if ($val === $value) {
241 2
                $result[$key] = $val;
242 2
                unset($array[$key]);
243
            }
244
        }
245
    }
246
247 3
    return $result;
248
}
249
250
function array_deep_search(array $array, $searchValue)
251
{
252 8
    $result = [];
253 8
    foreach ($array as $key => $value) {
254 6
        if ($path = array_deep_search_value($key, $value, $searchValue)) {
255 6
            $result[] = $path;
256
        }
257
    }
258
259 8
    return $result;
260
}
261
262
function array_deep_search_value($key, $value, $searchValue, $path = [])
263
{
264 7
    if (\is_array($value) && $subPath = array_deep_search($value, $searchValue)) {
265 4
        return array_flatten(array_merge($path, [$key], $subPath));
266
    }
267
268 7
    if ($value === $searchValue) {
269 5
        return [$key];
270
    }
271
272 7
    return [];
273
}
274
275
/**
276
 * @throws \RuntimeException
277
 */
278
function array_flatten(array $array): array
279
{
280
    try {
281 9
        $result = [];
282 9
        foreach (\array_keys($array) as $key) {
283 8
            array_flatten_value($array[$key], $result);
284
        }
285
286 8
        return \array_values(\array_unique($result));
287 1
    } catch (\Throwable $t) {
288 1
        throw new \RuntimeException($t->getMessage());
289
    }
290
}
291
292
/**
293
 * @param mixed $value
294
 */
295
function array_flatten_value($value, array &$result): void
296
{
297 8
    if (\is_scalar($value)) {
298 8
        $result[] = $value;
299
    }
300
301 8
    if (\is_array($value)) {
302 6
        $result = array_merge($result, array_flatten($value));
303
    }
304
305 8
    if (\is_object($value)) {
306 2
        $result[] = (string)$value;
307
    }
308 8
}
309
310
function array_multiple_keys_exists(array $array, array $keys): bool
311
{
312 1
    if (empty($keys)) {
313 1
        return false;
314
    }
315
316 1
    $result = true;
317
318 1
    foreach ($keys as $key) {
319 1
        $result &= array_key_exists($array, $key);
320
    }
321
322 1
    return $result ? true : false;
323
}
324
325
function array_keys(array $array, string $prefix = '')
326
{
327 1
    $result = [];
328
329 1
    foreach ($array as $key => $value) {
330 1
        if (\is_array($value) && !empty($value)) {
331 1
            $result = array_merge($result, array_keys($value, $prefix.$key.'.'));
332
        } else {
333 1
            $result[$prefix.$key] = $value;
334
        }
335
    }
336
337 1
    return $result;
338
}
339