Passed
Push — master ( 5063cc...785b16 )
by Saulius
02:22
created

is_array_and_key_exists()   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 (\is_callable($key)) {
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_and_key_exists($array, $key)) {
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_or_key_exists($array, $key) ? $array[$key] : $default;
95
    }
96
97 2
    return $default;
98
}
99
100
function isset_array_key_or_key_exists($array, $key): bool
101
{
102 6
    return (isset($array[$key]) || \array_key_exists($key, $array));
103
}
104
105
function is_array_and_key_exists($array, $key): bool
106
{
107 33
    return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array));
108
}
109
110
/**
111
 * @param mixed $path
112
 * @param mixed $value
113
 *
114
 * @return mixed
115
 */
116
function array_set_value(array &$array, $path, $value)
117
{
118 7
    if ($path === null) {
119 2
        $array = $value;
120
121 2
        return;
122
    }
123 5
    $keys = \is_array($path) ? $path : \explode('.', $path);
124 5
    while (\count($keys) > 1) {
125 3
        $key = \array_shift($keys);
126
127 3
        array_create_value($array, $key);
128
129 3
        $array = &$array[$key];
130
    }
131
132 5
    $array[\array_shift($keys)] = $value;
133 5
}
134
135
function array_create_value(array &$array, $key): void
136
{
137 3
    if (!isset($array[$key])) {
138 2
        $array[$key] = [];
139
    }
140
141 3
    if (!\is_array($array[$key])) {
142 1
        $array[$key] = [$array[$key]];
143
    }
144 3
}
145
146
function array_remove_key(&$array, $key, $default = null)
147
{
148 8
    $keys = parse_array_key_path($key);
149
150 8
    while (\count($keys) > 1) {
151 4
        $key = \array_shift($keys);
152
153 4
        if (\is_array($array[$key])) {
154 4
            $array = &$array[$key];
155
        }
156
    }
157
158 8
    $key = \array_shift($keys);
159
160 8
    if (can_remove_array_key($array, $key)) {
161 5
        $value = $array[$key];
162 5
        unset($array[$key]);
163
164 5
        return $value;
165
    }
166
167 3
    return $default;
168
}
169
170
function can_remove_array_key($array, $key): bool
171
{
172 8
    return \is_array($array) && (isset($array[$key]) || \array_key_exists($key, $array));
173
}
174
175
function array_key_exists(array $array, $key): bool
176
{
177 8
    $keys = parse_array_key_path($key);
178
179 8
    while (\count($keys) > 1) {
180 4
        $key = \array_shift($keys);
181
182 4
        if (\is_array($array[$key])) {
183 4
            $array = &$array[$key];
184
        }
185
    }
186
187 8
    $key = \array_shift($keys);
188
189 8
    return isset($array[$key]) || \array_key_exists($key, $array);
190
}
191
192
/**
193
 * @param mixed|string|array $key
194
 */
195
function parse_array_key_path($key): array
196
{
197 16
    return \is_array($key) ? $key : \explode('.', $key);
198
}
199
200
function array_remove_value(&$array, $value): array
201
{
202 3
    $result = [];
203 3
    if (\is_array($array)) {
204 3
        foreach ($array as $key => $val) {
205 2
            if ($val === $value) {
206 2
                $result[$key] = $val;
207 2
                unset($array[$key]);
208
            }
209
        }
210
    }
211
212 3
    return $result;
213
}
214
215
function array_deep_search(array $array, $searchValue)
216
{
217 8
    $result = [];
218 8
    foreach ($array as $key => $value) {
219 6
        if ($path = array_deep_search_value($key, $value, $searchValue)) {
220 6
            $result[] = $path;
221
        }
222
    }
223
224 8
    return $result;
225
}
226
227
function array_deep_search_value($key, $value, $searchValue, $path = [])
228
{
229 7
    if (\is_array($value) && $subPath = array_deep_search($value, $searchValue)) {
230 4
        return array_flatten(array_merge($path, [$key], $subPath));
231
    }
232
233 7
    if ($value === $searchValue) {
234 5
        return [$key];
235
    }
236
237 7
    return [];
238
}
239
240
/**
241
 * @throws \RuntimeException
242
 */
243
function array_flatten(array $array): array
244
{
245
    try {
246 9
        $result = [];
247 9
        foreach (\array_keys($array) as $key) {
248 8
            array_flatten_value($array[$key], $result);
249
        }
250 8
        return \array_values(\array_unique($result));
251 1
    } catch (\Throwable $t) {
252 1
        throw new \RuntimeException($t->getMessage());
253
    }
254
}
255
256
/**
257
 * @param mixed $value
258
 */
259
function array_flatten_value($value, array &$result): void
260
{
261 8
    if (\is_scalar($value)) {
262 8
        $result[] = $value;
263
    }
264
265 8
    if (\is_array($value)) {
266 6
        $result = array_merge($result, array_flatten($value));
267
    }
268
269 8
    if (\is_object($value)) {
270 2
        $result[] = (string)$value;
271
    }
272 8
}
273
274
function array_multiple_keys_exists(array $array, array $keys): bool
275
{
276 1
    if (empty($keys)) {
277 1
        return false;
278
    }
279
280 1
    $result = true;
281
282 1
    foreach ($keys as $key) {
283 1
        $result &= array_key_exists($array, $key);
284
    }
285
286 1
    return $result ? true : false;
287
}
288