Completed
Push — master ( ff3894...5063cc )
by Saulius
01:42
created

can_remove_array_key()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 3
nop 2
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 3
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
function array_merge(... $arrays): array
16
{
17 28
    $result = \array_shift($arrays);
18 28
    while (!empty($arrays)) {
19 28
        $nextArray = \array_shift($arrays);
20 28
        array_merge_with_next_array($nextArray, $result);
21
    }
22
23 28
    return $result;
24
}
25
26
function array_merge_with_next_array($nextArray, &$result): void
27
{
28 28
    foreach ($nextArray as $key => $value) {
29 16
        array_merge_with_current_array_values($key, $value, $result);
30
    }
31 28
}
32
33
function array_merge_with_current_array_values($key, $value, &$result): void
34
{
35 16
    if (\is_int($key)) {
36 11
        array_merge_integer_keyed_value($key, $value, $result);
37 7
    } elseif (can_merge_two_value_arrays($key, $value, $result)) {
38 5
        $result[$key] = array_merge($result[$key], $value);
39
    } else {
40 6
        $result[$key] = $value;
41
    }
42 16
}
43
44
function can_merge_two_value_arrays($key, $value, $result): bool
45
{
46 7
    return \is_array($value) && isset($result[$key]) && \is_array($result[$key]);
47
}
48
49
function array_merge_integer_keyed_value($key, $value, &$result): void
50
{
51 11
    if (\array_key_exists($key, $result)) {
52 11
        $result[] = $value;
53
    } else {
54 5
        $result[$key] = $value;
55
    }
56 11
}
57
58
/**
59
 * @param mixed      $array
60
 * @param mixed      $key
61
 * @param null|mixed $default
62
 *
63
 * @return null|mixed
64
 * @throws Exception\PropertyNotAccessibleException
65
 */
66
function array_get_value($array, $key, $default = null)
67
{
68 34
    if ($key instanceof \Closure) {
69 1
        return $key($array, $default);
70
    }
71
72 33
    if (\is_array($key)) {
73 2
        $lastKey = array_pop($key);
74 2
        foreach ($key as $keyPart) {
75 2
            $array = array_get_value($array, $keyPart);
76
        }
77 2
        $key = $lastKey;
78
    }
79
80 33
    if (\is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array))) {
81 28
        return $array[$key];
82
    }
83
84 9
    if ((bool)($pos = strrpos($key, '.')) !== false) {
85 4
        $array = array_get_value($array, substr($key, 0, $pos), $default);
86 4
        $key = substr($key, $pos + 1);
87
    }
88
89 9
    if (\is_object($array)) {
90 1
        return get_object_property_value($array, $key) ?? $default;
91
    }
92
93 8
    if (\is_array($array)) {
94 6
        return (isset($array[$key]) || \array_key_exists($key, $array)) ? $array[$key] : $default;
95
    }
96
97 2
    return $default;
98
}
99
100
/**
101
 * @param mixed $path
102
 * @param mixed $value
103
 *
104
 * @return mixed
105
 */
106
function array_set_value(array &$array, $path, $value)
107
{
108 7
    if ($path === null) {
109 2
        $array = $value;
110
111 2
        return;
112
    }
113 5
    $keys = \is_array($path) ? $path : \explode('.', $path);
114 5
    while (\count($keys) > 1) {
115 3
        $key = \array_shift($keys);
116
117 3
        if (!isset($array[$key])) {
118 2
            $array[$key] = [];
119
        }
120
121 3
        if (!\is_array($array[$key])) {
122 1
            $array[$key] = [$array[$key]];
123
        }
124
125 3
        $array = &$array[$key];
126
    }
127
128 5
    $array[\array_shift($keys)] = $value;
129 5
}
130
131
function array_remove_key(&$array, $key, $default = null)
132
{
133 8
    $keys = parse_array_key_path($key);
134
135 8
    while (\count($keys) > 1) {
136 4
        $key = \array_shift($keys);
137
138 4
        if (\is_array($array[$key])) {
139 4
            $array = &$array[$key];
140
        }
141
    }
142
143 8
    $key = \array_shift($keys);
144
145 8
    if (can_remove_array_key($array, $key)) {
146 5
        $value = $array[$key];
147 5
        unset($array[$key]);
148
149 5
        return $value;
150
    }
151
152 3
    return $default;
153
}
154
155
function can_remove_array_key($array, $key): bool
156
{
157 8
    return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array));
158
}
159
160
function array_key_exists(array $array, $key): bool
161
{
162 8
    $keys = parse_array_key_path($key);
163
164 8
    while (\count($keys) > 1) {
165 4
        $key = \array_shift($keys);
166
167 4
        if (\is_array($array[$key])) {
168 4
            $array = &$array[$key];
169
        }
170
    }
171
172 8
    $key = \array_shift($keys);
173
174 8
    return isset($array[$key]) || \array_key_exists($key, $array);
175
}
176
177
/**
178
 * @param mixed|string|array $key
179
 */
180
function parse_array_key_path($key): array
181
{
182 16
    return \is_array($key) ? $key : \explode('.', $key);
183
}
184
185
function array_remove_value(&$array, $value): array
186
{
187 3
    $result = [];
188 3
    if (\is_array($array)) {
189 3
        foreach ($array as $key => $val) {
190 2
            if ($val === $value) {
191 2
                $result[$key] = $val;
192 2
                unset($array[$key]);
193
            }
194
        }
195
    }
196
197 3
    return $result;
198
}
199
200
function array_deep_search(array $array, $searchValue)
201
{
202 8
    $result = [];
203 8
    foreach ($array as $key => $value) {
204 6
        if ($path = array_deep_search_value($key, $value, $searchValue)) {
205 6
            $result[] = $path;
206
        }
207
    }
208
209 8
    return $result;
210
}
211
212
function array_deep_search_value($key, $value, $searchValue, $path = [])
213
{
214 7
    if (\is_array($value) && $subPath = array_deep_search($value, $searchValue)) {
215 4
        return array_flatten(array_merge($path, [$key], $subPath));
216
    }
217
218 7
    if ($value === $searchValue) {
219 5
        return [$key];
220
    }
221
222 7
    return [];
223
}
224
225
/**
226
 * @throws \RuntimeException
227
 */
228
function array_flatten(array $array): array
229
{
230
    try {
231 9
        $result = [];
232 9
        foreach (\array_keys($array) as $key) {
233 8
            array_flatten_value($array[$key], $result);
234
        }
235 8
        return \array_values(\array_unique($result));
236 1
    } catch (\Throwable $t) {
237 1
        throw new \RuntimeException($t->getMessage());
238
    }
239
}
240
241
/**
242
 * @param mixed $value
243
 */
244
function array_flatten_value($value, array &$result): void
245
{
246 8
    if (\is_scalar($value)) {
247 8
        $result[] = $value;
248
    }
249
250 8
    if (\is_array($value)) {
251 6
        $result = array_merge($result, array_flatten($value));
252
    }
253
254 8
    if (\is_object($value)) {
255 2
        $result[] = (string)$value;
256
    }
257 8
}
258
259
function array_multiple_keys_exists(array $array, array $keys): bool
260
{
261 1
    if (empty($keys)) {
262 1
        return false;
263
    }
264
265 1
    $result = true;
266
267 1
    foreach ($keys as $key) {
268 1
        $result &= array_key_exists($array, $key);
269
    }
270
271 1
    return $result ? true : false;
272
}
273