Completed
Push — master ( 59b034...c3efb4 )
by Saulius
01:45
created

array_remove_key()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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