Arr::shuffle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use ArrayAccess;
6
use InvalidArgumentException;
7
use Resta\Support\Traits\Macroable;
8
9
class Arr
10
{
11
    /**
12
     * Determine whether the given value is array accessible.
13
     *
14
     * @param  mixed  $value
15
     * @return bool
16
     */
17
    public static function accessible($value)
18
    {
19
        return is_array($value) || $value instanceof ArrayAccess;
20
    }
21
    /**
22
     * Add an element to an array using "dot" notation if it doesn't exist.
23
     *
24
     * @param  array   $array
25
     * @param  string  $key
26
     * @param  mixed   $value
27
     * @return array
28
     */
29
    public static function add($array, $key, $value)
30
    {
31
        if (is_null(static::get($array, $key))) {
32
            static::set($array, $key, $value);
33
        }
34
        return $array;
35
    }
36
    /**
37
     * Collapse an array of arrays into a single array.
38
     *
39
     * @param  array  $array
40
     * @return array
41
     */
42
    public static function collapse($array)
43
    {
44
        $results = [];
45
        foreach ($array as $values) {
46
            if ($values instanceof Collection) {
0 ignored issues
show
Bug introduced by
The type Resta\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
                $values = $values->all();
48
            } elseif (! is_array($values)) {
49
                continue;
50
            }
51
            $results = array_merge($results, $values);
52
        }
53
        return $results;
54
    }
55
    /**
56
     * Cross join the given arrays, returning all possible permutations.
57
     *
58
     * @param  array  ...$arrays
59
     * @return array
60
     */
61
    public static function crossJoin(...$arrays)
62
    {
63
        $results = [[]];
64
        foreach ($arrays as $index => $array) {
65
            $append = [];
66
            foreach ($results as $product) {
67
                foreach ($array as $item) {
68
                    $product[$index] = $item;
69
                    $append[] = $product;
70
                }
71
            }
72
            $results = $append;
73
        }
74
        return $results;
75
    }
76
    /**
77
     * Divide an array into two arrays. One with keys and the other with values.
78
     *
79
     * @param  array  $array
80
     * @return array
81
     */
82
    public static function divide($array)
83
    {
84
        return [array_keys($array), array_values($array)];
85
    }
86
    /**
87
     * Flatten a multi-dimensional associative array with dots.
88
     *
89
     * @param  array   $array
90
     * @param  string  $prepend
91
     * @return array
92
     */
93
    public static function dot($array, $prepend = '')
94
    {
95
        $results = [];
96
        foreach ($array as $key => $value) {
97
            if (is_array($value) && ! empty($value)) {
98
                $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
99
            } else {
100
                $results[$prepend.$key] = $value;
101
            }
102
        }
103
        return $results;
104
    }
105
    /**
106
     * Get all of the given array except for a specified array of keys.
107
     *
108
     * @param  array  $array
109
     * @param  array|string  $keys
110
     * @return array
111
     */
112
    public static function except($array, $keys)
113
    {
114
        static::forget($array, $keys);
115
        return $array;
116
    }
117
    /**
118
     * Determine if the given key exists in the provided array.
119
     *
120
     * @param  \ArrayAccess|array  $array
121
     * @param  string|int  $key
122
     * @return bool
123
     */
124
    public static function exists($array, $key)
125
    {
126
        if ($array instanceof ArrayAccess) {
127
            return $array->offsetExists($key);
128
        }
129
        return array_key_exists($key, $array);
130
    }
131
    /**
132
     * Return the first element in an array passing a given truth test.
133
     *
134
     * @param  array  $array
135
     * @param  callable|null  $callback
136
     * @param  mixed  $default
137
     * @return mixed
138
     */
139
    public static function first($array, callable $callback = null, $default = null)
140
    {
141
        if (is_null($callback)) {
142
            if (empty($array)) {
143
                return value($default);
144
            }
145
            foreach ($array as $item) {
146
                return $item;
147
            }
148
        }
149
        foreach ($array as $key => $value) {
150
            if (call_user_func($callback, $value, $key)) {
0 ignored issues
show
Bug introduced by
It seems like $callback can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
            if (call_user_func(/** @scrutinizer ignore-type */ $callback, $value, $key)) {
Loading history...
151
                return $value;
152
            }
153
        }
154
        return value($default);
155
    }
156
    /**
157
     * Return the last element in an array passing a given truth test.
158
     *
159
     * @param  array  $array
160
     * @param  callable|null  $callback
161
     * @param  mixed  $default
162
     * @return mixed
163
     */
164
    public static function last($array, callable $callback = null, $default = null)
165
    {
166
        if (is_null($callback)) {
167
            return empty($array) ? value($default) : end($array);
168
        }
169
        return static::first(array_reverse($array, true), $callback, $default);
170
    }
171
    /**
172
     * Flatten a multi-dimensional array into a single level.
173
     *
174
     * @param  array  $array
175
     * @param  int  $depth
176
     * @return array
177
     */
178
    public static function flatten($array, $depth = INF)
179
    {
180
        $result = [];
181
        foreach ($array as $item) {
182
            $item = $item instanceof Collection ? $item->all() : $item;
183
            if (! is_array($item)) {
184
                $result[] = $item;
185
            } elseif ($depth === 1) {
186
                $result = array_merge($result, array_values($item));
187
            } else {
188
                $result = array_merge($result, static::flatten($item, $depth - 1));
0 ignored issues
show
Bug introduced by
$depth - 1 of type double is incompatible with the type integer expected by parameter $depth of Resta\Support\Arr::flatten(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

188
                $result = array_merge($result, static::flatten($item, /** @scrutinizer ignore-type */ $depth - 1));
Loading history...
189
            }
190
        }
191
        return $result;
192
    }
193
    /**
194
     * Remove one or many array items from a given array using "dot" notation.
195
     *
196
     * @param  array  $array
197
     * @param  array|string  $keys
198
     * @return void
199
     */
200
    public static function forget(&$array, $keys)
201
    {
202
        $original = &$array;
203
        $keys = (array) $keys;
204
        if (count($keys) === 0) {
205
            return;
206
        }
207
        foreach ($keys as $key) {
208
            // if the exact key exists in the top-level, remove it
209
            if (static::exists($array, $key)) {
210
                unset($array[$key]);
211
                continue;
212
            }
213
            $parts = explode('.', $key);
214
            // clean up before each pass
215
            $array = &$original;
216
            while (count($parts) > 1) {
217
                $part = array_shift($parts);
218
                if (isset($array[$part]) && is_array($array[$part])) {
219
                    $array = &$array[$part];
220
                } else {
221
                    continue 2;
222
                }
223
            }
224
            unset($array[array_shift($parts)]);
225
        }
226
    }
227
    /**
228
     * Get an item from an array using "dot" notation.
229
     *
230
     * @param  \ArrayAccess|array  $array
231
     * @param  string  $key
232
     * @param  mixed   $default
233
     * @return mixed
234
     */
235
    public static function get($array, $key, $default = null)
236
    {
237
        if (! static::accessible($array)) {
238
            return value($default);
239
        }
240
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
241
            return $array;
242
        }
243
        if (static::exists($array, $key)) {
244
            return $array[$key];
245
        }
246
        if (strpos($key, '.') === false) {
247
            return $array[$key] ?? value($default);
248
        }
249
        foreach (explode('.', $key) as $segment) {
250
            if (static::accessible($array) && static::exists($array, $segment)) {
251
                $array = $array[$segment];
252
            } else {
253
                return value($default);
254
            }
255
        }
256
        return $array;
257
    }
258
    /**
259
     * Check if an item or items exist in an array using "dot" notation.
260
     *
261
     * @param  \ArrayAccess|array  $array
262
     * @param  string|array  $keys
263
     * @return bool
264
     */
265
    public static function has($array, $keys)
266
    {
267
        if (is_null($keys)) {
0 ignored issues
show
introduced by
The condition is_null($keys) is always false.
Loading history...
268
            return false;
269
        }
270
        $keys = (array) $keys;
271
        if (! $array) {
272
            return false;
273
        }
274
        if ($keys === []) {
275
            return false;
276
        }
277
        foreach ($keys as $key) {
278
            $subKeyArray = $array;
279
            if (static::exists($array, $key)) {
280
                continue;
281
            }
282
            foreach (explode('.', $key) as $segment) {
283
                if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
284
                    $subKeyArray = $subKeyArray[$segment];
285
                } else {
286
                    return false;
287
                }
288
            }
289
        }
290
        return true;
291
    }
292
    /**
293
     * Determines if an array is associative.
294
     *
295
     * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
296
     *
297
     * @param  array  $array
298
     * @return bool
299
     */
300
    public static function isAssoc(array $array)
301
    {
302
        $keys = array_keys($array);
303
        return array_keys($keys) !== $keys;
304
    }
305
    /**
306
     * Get a subset of the items from the given array.
307
     *
308
     * @param  array  $array
309
     * @param  array|string  $keys
310
     * @return array
311
     */
312
    public static function only($array, $keys)
313
    {
314
        return array_intersect_key($array, array_flip((array) $keys));
315
    }
316
    /**
317
     * Pluck an array of values from an array.
318
     *
319
     * @param  array  $array
320
     * @param  string|array  $value
321
     * @param  string|array|null  $key
322
     * @return array
323
     */
324
    public static function pluck($array, $value, $key = null)
325
    {
326
        $results = [];
327
        [$value, $key] = static::explodePluckParameters($value, $key);
328
        foreach ($array as $item) {
329
            $itemValue = data_get($item, $value);
330
            // If the key is "null", we will just append the value to the array and keep
331
            // looping. Otherwise we will key the array using the value of the key we
332
            // received from the developer. Then we'll return the final array form.
333
            if (is_null($key)) {
334
                $results[] = $itemValue;
335
            } else {
336
                $itemKey = data_get($item, $key);
337
                if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
338
                    $itemKey = (string) $itemKey;
339
                }
340
                $results[$itemKey] = $itemValue;
341
            }
342
        }
343
        return $results;
344
    }
345
    /**
346
     * Explode the "value" and "key" arguments passed to "pluck".
347
     *
348
     * @param  string|array  $value
349
     * @param  string|array|null  $key
350
     * @return array
351
     */
352
    protected static function explodePluckParameters($value, $key)
353
    {
354
        $value = is_string($value) ? explode('.', $value) : $value;
355
        $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
356
        return [$value, $key];
357
    }
358
    /**
359
     * Push an item onto the beginning of an array.
360
     *
361
     * @param  array  $array
362
     * @param  mixed  $value
363
     * @param  mixed  $key
364
     * @return array
365
     */
366
    public static function prepend($array, $value, $key = null)
367
    {
368
        if (is_null($key)) {
369
            array_unshift($array, $value);
370
        } else {
371
            $array = [$key => $value] + $array;
372
        }
373
        return $array;
374
    }
375
    /**
376
     * Get a value from the array, and remove it.
377
     *
378
     * @param  array   $array
379
     * @param  string  $key
380
     * @param  mixed   $default
381
     * @return mixed
382
     */
383
    public static function pull(&$array, $key, $default = null)
384
    {
385
        $value = static::get($array, $key, $default);
386
        static::forget($array, $key);
387
        return $value;
388
    }
389
    /**
390
     * Get one or a specified number of random values from an array.
391
     *
392
     * @param  array  $array
393
     * @param  int|null  $number
394
     * @return mixed
395
     *
396
     * @throws \InvalidArgumentException
397
     */
398
    public static function random($array, $number = null)
399
    {
400
        $requested = is_null($number) ? 1 : $number;
401
        $count = count($array);
402
        if ($requested > $count) {
403
            throw new InvalidArgumentException(
404
                "You requested {$requested} items, but there are only {$count} items available."
405
            );
406
        }
407
        if (is_null($number)) {
408
            return $array[array_rand($array)];
409
        }
410
        if ((int) $number === 0) {
411
            return [];
412
        }
413
        $keys = array_rand($array, $number);
414
        $results = [];
415
        foreach ((array) $keys as $key) {
416
            $results[] = $array[$key];
417
        }
418
        return $results;
419
    }
420
    /**
421
     * Set an array item to a given value using "dot" notation.
422
     *
423
     * If no key is given to the method, the entire array will be replaced.
424
     *
425
     * @param  array   $array
426
     * @param  string  $key
427
     * @param  mixed   $value
428
     * @return array
429
     */
430
    public static function set(&$array, $key, $value)
431
    {
432
        if (is_null($key)) {
0 ignored issues
show
introduced by
The condition is_null($key) is always false.
Loading history...
433
            return $array = $value;
434
        }
435
        $keys = explode('.', $key);
436
        while (count($keys) > 1) {
437
            $key = array_shift($keys);
438
            // If the key doesn't exist at this depth, we will just create an empty array
439
            // to hold the next value, allowing us to create the arrays to hold final
440
            // values at the correct depth. Then we'll keep digging into the array.
441
            if (! isset($array[$key]) || ! is_array($array[$key])) {
442
                $array[$key] = [];
443
            }
444
            $array = &$array[$key];
445
        }
446
        $array[array_shift($keys)] = $value;
447
        return $array;
448
    }
449
    /**
450
     * Shuffle the given array and return the result.
451
     *
452
     * @param  array  $array
453
     * @param  int|null  $seed
454
     * @return array
455
     */
456
    public static function shuffle($array, $seed = null)
457
    {
458
        if (is_null($seed)) {
459
            shuffle($array);
460
        } else {
461
            srand($seed);
462
            usort($array, function () {
463
                return rand(-1, 1);
464
            });
465
        }
466
        return $array;
467
    }
468
    /**
469
     * Sort the array using the given callback or "dot" notation.
470
     *
471
     * @param  array  $array
472
     * @param  callable|string|null  $callback
473
     * @return array
474
     */
475
    public static function sort($array, $callback = null)
476
    {
477
        return Collection::make($array)->sortBy($callback)->all();
478
    }
479
    /**
480
     * Recursively sort an array by keys and values.
481
     *
482
     * @param  array  $array
483
     * @return array
484
     */
485
    public static function sortRecursive($array)
486
    {
487
        foreach ($array as &$value) {
488
            if (is_array($value)) {
489
                $value = static::sortRecursive($value);
490
            }
491
        }
492
        if (static::isAssoc($array)) {
493
            ksort($array);
494
        } else {
495
            sort($array);
496
        }
497
        return $array;
498
    }
499
    /**
500
     * Convert the array into a query string.
501
     *
502
     * @param  array  $array
503
     * @return string
504
     */
505
    public static function query($array)
506
    {
507
        return http_build_query($array, null, '&', PHP_QUERY_RFC3986);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type string expected by parameter $numeric_prefix of http_build_query(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

507
        return http_build_query($array, /** @scrutinizer ignore-type */ null, '&', PHP_QUERY_RFC3986);
Loading history...
508
    }
509
    /**
510
     * Filter the array using the given callback.
511
     *
512
     * @param  array  $array
513
     * @param  callable  $callback
514
     * @return array
515
     */
516
    public static function where($array, callable $callback)
517
    {
518
        return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
519
    }
520
    /**
521
     * If the given value is not an array and not null, wrap it in one.
522
     *
523
     * @param  mixed  $value
524
     * @return array
525
     */
526
    public static function wrap($value)
527
    {
528
        if (is_null($value)) {
529
            return [];
530
        }
531
        return is_array($value) ? $value : [$value];
532
    }
533
534
    /**
535
     * @param array $removeArray
536
     * @param array $referenceArray
537
     * @return array
538
     */
539
    public static function removeSameValues($removeArray=array(),$referenceArray=array())
540
    {
541
        $list = [];
542
543
        foreach ($removeArray as $remove){
544
            if(!in_array($remove,$referenceArray)){
545
                $list[]=$remove;
546
            }
547
        }
548
        return array_merge($referenceArray,$list);
549
    }
550
551
    /**
552
     * @param $array
553
     * @param $key
554
     * @return null
555
     */
556
    public static function isset($array,$key)
557
    {
558
        if(isset($array[$key])){
559
            return $array[$key];
560
        }
561
        return null;
562
    }
563
564
    /**
565
     * @param $array
566
     * @param null $withKey
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $withKey is correct as it would always require null to be passed?
Loading history...
567
     * @return bool
568
     */
569
    public static function isArrayWithCount($array,$withKey=null)
570
    {
571
        if($withKey===null){
0 ignored issues
show
introduced by
The condition $withKey === null is always true.
Loading history...
572
            return is_array($array) && count($array);
573
        }
574
575
        return isset($array[$withKey]) && self::isArrayWithCount($array[$withKey],null);
576
    }
577
578
    /**
579
     * @param $array1
580
     * @param $array2
581
     * @return int
582
     */
583
    public static function arrayDiffKey($array1,$array2)
584
    {
585
        $forArray1 = array_diff_key($array1,$array2);
586
        $forArray2 = array_diff_key($array2,$array1);
587
588
        if(count($forArray1)=="0" && count($forArray2)=="0"){
589
            return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type integer.
Loading history...
590
        }
591
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type integer.
Loading history...
592
    }
593
594
    /**
595
     * overwrite with
596
     *
597
     * @param $original
598
     * @param array $overwrite
599
     */
600
    public static function overwriteWith($original,$overwrite=array())
601
    {
602
        foreach ($original as $key=>$item){
603
604
            if(array_key_exists($key,$overwrite)){
605
                array_splice($original,$key,0,$overwrite[$key]);
606
            }
607
        }
608
609
        return $original;
610
    }
611
612
    /**
613
     * remove key from array
614
     *
615
     * @param $data
616
     * @param array $remove
617
     * @return array
618
     */
619
    public static function removeKey($data,$remove=array())
620
    {
621
        $list = [];
622
623
        foreach($data as $key=>$value){
624
            if(!in_array($key,$remove)){
625
                $list[$key] = $value;
626
            }
627
        }
628
629
        return $list;
630
    }
631
632
    /**
633
     * @param array $data
634
     * @param $key
635
     * @param $value
636
     */
637
    public static function setNested($data=array(),$key,$value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

637
    public static function setNested($data=array(),$key,/** @scrutinizer ignore-unused */ $value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

637
    public static function setNested($data=array(),/** @scrutinizer ignore-unused */ $key,$value)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
638
    {
639
        dd($data);
640
    }
641
642
}