Completed
Push — master ( 91c709...1f6826 )
by Antonio Carlos
06:01
created

Arr::hasAny()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.5625

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 24
ccs 9
cts 12
cp 0.75
crap 6.5625
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Arr\Support;
4
5
use ArrayAccess;
6
use IlluminateAgnostic\Arr\Support\Traits\Macroable;
7
use InvalidArgumentException;
8
9
class Arr
10
{
11
    use Macroable;
12
13
    /**
14
     * Determine whether the given value is array accessible.
15
     *
16
     * @param  mixed  $value
17
     * @return bool
18
     */
19 99
    public static function accessible($value)
20
    {
21 99
        return is_array($value) || $value instanceof ArrayAccess;
22
    }
23
24
    /**
25
     * Add an element to an array using "dot" notation if it doesn't exist.
26
     *
27
     * @param  array  $array
28
     * @param  string  $key
29
     * @param  mixed  $value
30
     * @return array
31
     */
32 1
    public static function add($array, $key, $value)
33
    {
34 1
        if (is_null(static::get($array, $key))) {
35 1
            static::set($array, $key, $value);
36
        }
37
38 1
        return $array;
39
    }
40
41
    /**
42
     * Collapse an array of arrays into a single array.
43
     *
44
     * @param  iterable  $array
45
     * @return array
46
     */
47 5
    public static function collapse($array)
48
    {
49 5
        $results = [];
50
51 5
        foreach ($array as $values) {
52 5
            if ($values instanceof Collection) {
53 2
                $values = $values->all();
54 4
            } elseif (! is_array($values)) {
55 1
                continue;
56
            }
57
58 5
            $results[] = $values;
59
        }
60
61 5
        return array_merge([], ...$results);
62
    }
63
64
    /**
65
     * Cross join the given arrays, returning all possible permutations.
66
     *
67
     * @param  iterable  ...$arrays
68
     * @return array
69
     */
70 3
    public static function crossJoin(...$arrays)
71
    {
72 3
        $results = [[]];
73
74 3
        foreach ($arrays as $index => $array) {
75 3
            $append = [];
76
77 3
            foreach ($results as $product) {
78 3
                foreach ($array as $item) {
79 3
                    $product[$index] = $item;
80
81 3
                    $append[] = $product;
82
                }
83
            }
84
85 3
            $results = $append;
86
        }
87
88 3
        return $results;
89
    }
90
91
    /**
92
     * Divide an array into two arrays. One with keys and the other with values.
93
     *
94
     * @param  array  $array
95
     * @return array
96
     */
97 1
    public static function divide($array)
98
    {
99 1
        return [array_keys($array), array_values($array)];
100
    }
101
102
    /**
103
     * Flatten a multi-dimensional associative array with dots.
104
     *
105
     * @param  iterable  $array
106
     * @param  string  $prepend
107
     * @return array
108
     */
109 1
    public static function dot($array, $prepend = '')
110
    {
111 1
        $results = [];
112
113 1
        foreach ($array as $key => $value) {
114 1
            if (is_array($value) && ! empty($value)) {
115 1
                $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
0 ignored issues
show
Documentation introduced by
$value is of type array, but the function expects a object<IlluminateAgnostic\Arr\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
116
            } else {
117 1
                $results[$prepend.$key] = $value;
118
            }
119
        }
120
121 1
        return $results;
122
    }
123
124
    /**
125
     * Get all of the given array except for a specified array of keys.
126
     *
127
     * @param  array  $array
128
     * @param  array|string  $keys
129
     * @return array
130
     */
131 5
    public static function except($array, $keys)
132
    {
133 5
        static::forget($array, $keys);
134
135 5
        return $array;
136
    }
137
138
    /**
139
     * Determine if the given key exists in the provided array.
140
     *
141
     * @param  \ArrayAccess|array  $array
142
     * @param  string|int  $key
143
     * @return bool
144
     */
145 91
    public static function exists($array, $key)
146
    {
147 91
        if ($array instanceof ArrayAccess) {
148 5
            return $array->offsetExists($key);
149
        }
150
151 89
        return array_key_exists($key, $array);
152
    }
153
154
    /**
155
     * Return the first element in an array passing a given truth test.
156
     *
157
     * @param  iterable  $array
158
     * @param  callable|null  $callback
159
     * @param  mixed  $default
160
     * @return mixed
161
     */
162 29
    public static function first($array, callable $callback = null, $default = null)
163
    {
164 29
        if (is_null($callback)) {
165 18
            if (empty($array)) {
166 3
                return value($default);
167
            }
168
169 17
            foreach ($array as $item) {
170 17
                return $item;
171
            }
172
        }
173
174 12
        foreach ($array as $key => $value) {
175 12
            if ($callback($value, $key)) {
176 10
                return $value;
177
            }
178
        }
179
180 9
        return value($default);
181
    }
182
183
    /**
184
     * Return the last element in an array passing a given truth test.
185
     *
186
     * @param  array  $array
187
     * @param  callable|null  $callback
188
     * @param  mixed  $default
189
     * @return mixed
190
     */
191 13
    public static function last($array, callable $callback = null, $default = null)
192
    {
193 13
        if (is_null($callback)) {
194 11
            return empty($array) ? value($default) : end($array);
195
        }
196
197 3
        return static::first(array_reverse($array, true), $callback, $default);
0 ignored issues
show
Documentation introduced by
array_reverse($array, true) is of type array, but the function expects a object<IlluminateAgnostic\Arr\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
198
    }
199
200
    /**
201
     * Flatten a multi-dimensional array into a single level.
202
     *
203
     * @param  iterable  $array
204
     * @param  int  $depth
205
     * @return array
206
     */
207 5
    public static function flatten($array, $depth = INF)
208
    {
209 5
        $result = [];
210
211 5
        foreach ($array as $item) {
212 5
            $item = $item instanceof Collection ? $item->all() : $item;
213
214 5
            if (! is_array($item)) {
215 5
                $result[] = $item;
216
            } else {
217 5
                $values = $depth === 1
218 3
                    ? array_values($item)
219 5
                    : static::flatten($item, $depth - 1);
0 ignored issues
show
Documentation introduced by
$item is of type array, but the function expects a object<IlluminateAgnostic\Arr\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
220
221 5
                foreach ($values as $value) {
222 5
                    $result[] = $value;
223
                }
224
            }
225
        }
226
227 5
        return $result;
228
    }
229
230
    /**
231
     * Remove one or many array items from a given array using "dot" notation.
232
     *
233
     * @param  array  $array
234
     * @param  array|string  $keys
235
     * @return void
236
     */
237 10
    public static function forget(&$array, $keys)
238
    {
239 10
        $original = &$array;
240
241 10
        $keys = (array) $keys;
242
243 10
        if (count($keys) === 0) {
244 1
            return;
245
        }
246
247 10
        foreach ($keys as $key) {
248
            // if the exact key exists in the top-level, remove it
249 10
            if (static::exists($array, $key)) {
250 7
                unset($array[$key]);
251
252 7
                continue;
253
            }
254
255 8
            $parts = explode('.', $key);
256
257
            // clean up before each pass
258 8
            $array = &$original;
259
260 8
            while (count($parts) > 1) {
261 3
                $part = array_shift($parts);
262
263 3
                if (isset($array[$part]) && is_array($array[$part])) {
264 3
                    $array = &$array[$part];
265
                } else {
266 2
                    continue 2;
267
                }
268
            }
269
270 7
            unset($array[array_shift($parts)]);
271
        }
272 10
    }
273
274
    /**
275
     * Get an item from an array using "dot" notation.
276
     *
277
     * @param  \ArrayAccess|array  $array
278
     * @param  string|int|null  $key
279
     * @param  mixed  $default
280
     * @return mixed
281
     */
282 6
    public static function get($array, $key, $default = null)
283
    {
284 6
        if (! static::accessible($array)) {
285 1
            return value($default);
286
        }
287
288 6
        if (is_null($key)) {
289 1
            return $array;
290
        }
291
292 6
        if (static::exists($array, $key)) {
293 4
            return $array[$key];
294
        }
295
296 4
        if (strpos($key, '.') === false) {
297 2
            return $array[$key] ?? value($default);
298
        }
299
300 3
        foreach (explode('.', $key) as $segment) {
301 3
            if (static::accessible($array) && static::exists($array, $segment)) {
302 2
                $array = $array[$segment];
303
            } else {
304 3
                return value($default);
305
            }
306
        }
307
308 1
        return $array;
309
    }
310
311
    /**
312
     * Check if an item or items exist in an array using "dot" notation.
313
     *
314
     * @param  \ArrayAccess|array  $array
315
     * @param  string|array  $keys
316
     * @return bool
317
     */
318 2
    public static function has($array, $keys)
319
    {
320 2
        $keys = (array) $keys;
321
322 2
        if (! $array || $keys === []) {
323 1
            return false;
324
        }
325
326 2
        foreach ($keys as $key) {
327 2
            $subKeyArray = $array;
328
329 2
            if (static::exists($array, $key)) {
330 2
                continue;
331
            }
332
333 2
            foreach (explode('.', $key) as $segment) {
334 2
                if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
335 2
                    $subKeyArray = $subKeyArray[$segment];
336
                } else {
337 2
                    return false;
338
                }
339
            }
340
        }
341
342 2
        return true;
343
    }
344
345
    /**
346
     * Determine if any of the keys exist in an array using "dot" notation.
347
     *
348
     * @param  \ArrayAccess|array  $array
349
     * @param  string|array  $keys
350
     * @return bool
351
     */
352 1
    public static function hasAny($array, $keys)
353
    {
354 1
        if (is_null($keys)) {
355
            return false;
356
        }
357
358 1
        $keys = (array) $keys;
359
360 1
        if (! $array) {
361
            return false;
362
        }
363
364 1
        if ($keys === []) {
365
            return false;
366
        }
367
368 1
        foreach ($keys as $key) {
369 1
            if (static::has($array, $key)) {
370 1
                return true;
371
            }
372
        }
373
374 1
        return false;
375
    }
376
377
    /**
378
     * Determines if an array is associative.
379
     *
380
     * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
381
     *
382
     * @param  array  $array
383
     * @return bool
384
     */
385 2
    public static function isAssoc(array $array)
386
    {
387 2
        $keys = array_keys($array);
388
389 2
        return array_keys($keys) !== $keys;
390
    }
391
392
    /**
393
     * Get a subset of the items from the given array.
394
     *
395
     * @param  array  $array
396
     * @param  array|string  $keys
397
     * @return array
398
     */
399 2
    public static function only($array, $keys)
400
    {
401 2
        return array_intersect_key($array, array_flip((array) $keys));
402
    }
403
404
    /**
405
     * Pluck an array of values from an array.
406
     *
407
     * @param  iterable  $array
408
     * @param  string|array  $value
409
     * @param  string|array|null  $key
410
     * @return array
411
     */
412 23
    public static function pluck($array, $value, $key = null)
413
    {
414 23
        $results = [];
415
416 23
        [$value, $key] = static::explodePluckParameters($value, $key);
417
418 23
        foreach ($array as $item) {
419 23
            $itemValue = data_get($item, $value);
420
421
            // If the key is "null", we will just append the value to the array and keep
422
            // looping. Otherwise we will key the array using the value of the key we
423
            // received from the developer. Then we'll return the final array form.
424 23
            if (is_null($key)) {
425 21
                $results[] = $itemValue;
426
            } else {
427 7
                $itemKey = data_get($item, $key);
428
429 7
                if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
430 1
                    $itemKey = (string) $itemKey;
431
                }
432
433 7
                $results[$itemKey] = $itemValue;
434
            }
435
        }
436
437 23
        return $results;
438
    }
439
440
    /**
441
     * Explode the "value" and "key" arguments passed to "pluck".
442
     *
443
     * @param  string|array  $value
444
     * @param  string|array|null  $key
445
     * @return array
446
     */
447 23
    protected static function explodePluckParameters($value, $key)
448
    {
449 23
        $value = is_string($value) ? explode('.', $value) : $value;
450
451 23
        $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
452
453 23
        return [$value, $key];
454
    }
455
456
    /**
457
     * Push an item onto the beginning of an array.
458
     *
459
     * @param  array  $array
460
     * @param  mixed  $value
461
     * @param  mixed  $key
462
     * @return array
463
     */
464 3
    public static function prepend($array, $value, $key = null)
465
    {
466 3
        if (is_null($key)) {
467 3
            array_unshift($array, $value);
468
        } else {
469 2
            $array = [$key => $value] + $array;
470
        }
471
472 3
        return $array;
473
    }
474
475
    /**
476
     * Get a value from the array, and remove it.
477
     *
478
     * @param  array  $array
479
     * @param  string  $key
480
     * @param  mixed  $default
481
     * @return mixed
482
     */
483 4
    public static function pull(&$array, $key, $default = null)
484
    {
485 4
        $value = static::get($array, $key, $default);
486
487 4
        static::forget($array, $key);
488
489 4
        return $value;
490
    }
491
492
    /**
493
     * Get one or a specified number of random values from an array.
494
     *
495
     * @param  array  $array
496
     * @param  int|null  $number
497
     * @return mixed
498
     *
499
     * @throws \InvalidArgumentException
500
     */
501 9
    public static function random($array, $number = null)
502
    {
503 9
        $requested = is_null($number) ? 1 : $number;
504
505 9
        $count = count($array);
506
507 9
        if ($requested > $count) {
508 3
            throw new InvalidArgumentException(
509 3
                "You requested {$requested} items, but there are only {$count} items available."
510
            );
511
        }
512
513 6
        if (is_null($number)) {
514 3
            return $array[array_rand($array)];
515
        }
516
517 6
        if ((int) $number === 0) {
518 6
            return [];
519
        }
520
521 3
        $keys = array_rand($array, $number);
522
523 3
        $results = [];
524
525 3
        foreach ((array) $keys as $key) {
526 3
            $results[] = $array[$key];
527
        }
528
529 3
        return $results;
530
    }
531
532
    /**
533
     * Set an array item to a given value using "dot" notation.
534
     *
535
     * If no key is given to the method, the entire array will be replaced.
536
     *
537
     * @param  array  $array
538
     * @param  string|null  $key
539
     * @param  mixed  $value
540
     * @return array
541
     */
542 3
    public static function set(&$array, $key, $value)
543
    {
544 3
        if (is_null($key)) {
545 1
            return $array = $value;
546
        }
547
548 3
        $keys = explode('.', $key);
549
550 3
        while (count($keys) > 1) {
551 3
            $key = array_shift($keys);
552
553
            // If the key doesn't exist at this depth, we will just create an empty array
554
            // to hold the next value, allowing us to create the arrays to hold final
555
            // values at the correct depth. Then we'll keep digging into the array.
556 3
            if (! isset($array[$key]) || ! is_array($array[$key])) {
557 2
                $array[$key] = [];
558
            }
559
560 3
            $array = &$array[$key];
561
        }
562
563 3
        $array[array_shift($keys)] = $value;
564
565 3
        return $array;
566
    }
567
568
    /**
569
     * Shuffle the given array and return the result.
570
     *
571
     * @param  array  $array
572
     * @param  int|null  $seed
573
     * @return array
574
     */
575 3
    public static function shuffle($array, $seed = null)
576
    {
577 3
        if (is_null($seed)) {
578 1
            shuffle($array);
579
        } else {
580 2
            mt_srand($seed);
581 2
            shuffle($array);
582 2
            mt_srand();
583
        }
584
585 3
        return $array;
586
    }
587
588
    /**
589
     * Sort the array using the given callback or "dot" notation.
590
     *
591
     * @param  array  $array
592
     * @param  callable|string|null  $callback
593
     * @return array
594
     */
595 1
    public static function sort($array, $callback = null)
596
    {
597 1
        return Collection::make($array)->sortBy($callback)->all();
598
    }
599
600
    /**
601
     * Recursively sort an array by keys and values.
602
     *
603
     * @param  array  $array
604
     * @return array
605
     */
606 1
    public static function sortRecursive($array)
607
    {
608 1
        foreach ($array as &$value) {
609 1
            if (is_array($value)) {
610 1
                $value = static::sortRecursive($value);
611
            }
612
        }
613
614 1
        if (static::isAssoc($array)) {
615 1
            ksort($array);
616
        } else {
617 1
            sort($array);
618
        }
619
620 1
        return $array;
621
    }
622
623
    /**
624
     * Convert the array into a query string.
625
     *
626
     * @param  array  $array
627
     * @return string
628
     */
629 1
    public static function query($array)
630
    {
631 1
        return http_build_query($array, null, '&', PHP_QUERY_RFC3986);
632
    }
633
634
    /**
635
     * Filter the array using the given callback.
636
     *
637
     * @param  array  $array
638
     * @param  callable  $callback
639
     * @return array
640
     */
641 56
    public static function where($array, callable $callback)
642
    {
643 56
        return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
644
    }
645
646
    /**
647
     * If the given value is not an array and not null, wrap it in one.
648
     *
649
     * @param  mixed  $value
650
     * @return array
651
     */
652 11
    public static function wrap($value)
653
    {
654 11
        if (is_null($value)) {
655 1
            return [];
656
        }
657
658 11
        return is_array($value) ? $value : [$value];
659
    }
660
}
661