Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 399 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
use ArgumentCountError;
6
use ArrayAccess;
7
use Illuminate\Support\Traits\Macroable;
8
use InvalidArgumentException;
9
 
10
class Arr
11
{
12
    use Macroable;
13
 
14
    /**
15
     * Determine whether the given value is array accessible.
16
     *
17
     * @param  mixed  $value
18
     * @return bool
19
     */
20
    public static function accessible($value)
21
    {
22
        return is_array($value) || $value instanceof ArrayAccess;
23
    }
24
 
25
    /**
26
     * Add an element to an array using "dot" notation if it doesn't exist.
27
     *
28
     * @param  array  $array
29
     * @param  string|int|float  $key
30
     * @param  mixed  $value
31
     * @return array
32
     */
33
    public static function add($array, $key, $value)
34
    {
35
        if (is_null(static::get($array, $key))) {
36
            static::set($array, $key, $value);
37
        }
38
 
39
        return $array;
40
    }
41
 
42
    /**
43
     * Collapse an array of arrays into a single array.
44
     *
45
     * @param  iterable  $array
46
     * @return array
47
     */
48
    public static function collapse($array)
49
    {
50
        $results = [];
51
 
52
        foreach ($array as $values) {
53
            if ($values instanceof Collection) {
54
                $values = $values->all();
55
            } elseif (! is_array($values)) {
56
                continue;
57
            }
58
 
59
            $results[] = $values;
60
        }
61
 
62
        return array_merge([], ...$results);
63
    }
64
 
65
    /**
66
     * Cross join the given arrays, returning all possible permutations.
67
     *
68
     * @param  iterable  ...$arrays
69
     * @return array
70
     */
71
    public static function crossJoin(...$arrays)
72
    {
73
        $results = [[]];
74
 
75
        foreach ($arrays as $index => $array) {
76
            $append = [];
77
 
78
            foreach ($results as $product) {
79
                foreach ($array as $item) {
80
                    $product[$index] = $item;
81
 
82
                    $append[] = $product;
83
                }
84
            }
85
 
86
            $results = $append;
87
        }
88
 
89
        return $results;
90
    }
91
 
92
    /**
93
     * Divide an array into two arrays. One with keys and the other with values.
94
     *
95
     * @param  array  $array
96
     * @return array
97
     */
98
    public static function divide($array)
99
    {
100
        return [array_keys($array), array_values($array)];
101
    }
102
 
103
    /**
104
     * Flatten a multi-dimensional associative array with dots.
105
     *
106
     * @param  iterable  $array
107
     * @param  string  $prepend
108
     * @return array
109
     */
110
    public static function dot($array, $prepend = '')
111
    {
112
        $results = [];
113
 
114
        foreach ($array as $key => $value) {
115
            if (is_array($value) && ! empty($value)) {
116
                $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
117
            } else {
118
                $results[$prepend.$key] = $value;
119
            }
120
        }
121
 
122
        return $results;
123
    }
124
 
125
    /**
126
     * Convert a flatten "dot" notation array into an expanded array.
127
     *
128
     * @param  iterable  $array
129
     * @return array
130
     */
131
    public static function undot($array)
132
    {
133
        $results = [];
134
 
135
        foreach ($array as $key => $value) {
136
            static::set($results, $key, $value);
137
        }
138
 
139
        return $results;
140
    }
141
 
142
    /**
143
     * Get all of the given array except for a specified array of keys.
144
     *
145
     * @param  array  $array
146
     * @param  array|string|int|float  $keys
147
     * @return array
148
     */
149
    public static function except($array, $keys)
150
    {
151
        static::forget($array, $keys);
152
 
153
        return $array;
154
    }
155
 
156
    /**
157
     * Determine if the given key exists in the provided array.
158
     *
159
     * @param  \ArrayAccess|array  $array
160
     * @param  string|int  $key
161
     * @return bool
162
     */
163
    public static function exists($array, $key)
164
    {
165
        if ($array instanceof Enumerable) {
166
            return $array->has($key);
167
        }
168
 
169
        if ($array instanceof ArrayAccess) {
170
            return $array->offsetExists($key);
171
        }
172
 
173
        if (is_float($key)) {
174
            $key = (string) $key;
175
        }
176
 
177
        return array_key_exists($key, $array);
178
    }
179
 
180
    /**
181
     * Return the first element in an array passing a given truth test.
182
     *
183
     * @param  iterable  $array
184
     * @param  callable|null  $callback
185
     * @param  mixed  $default
186
     * @return mixed
187
     */
188
    public static function first($array, callable $callback = null, $default = null)
189
    {
190
        if (is_null($callback)) {
191
            if (empty($array)) {
192
                return value($default);
193
            }
194
 
195
            foreach ($array as $item) {
196
                return $item;
197
            }
198
        }
199
 
200
        foreach ($array as $key => $value) {
201
            if ($callback($value, $key)) {
202
                return $value;
203
            }
204
        }
205
 
206
        return value($default);
207
    }
208
 
209
    /**
210
     * Return the last element in an array passing a given truth test.
211
     *
212
     * @param  array  $array
213
     * @param  callable|null  $callback
214
     * @param  mixed  $default
215
     * @return mixed
216
     */
217
    public static function last($array, callable $callback = null, $default = null)
218
    {
219
        if (is_null($callback)) {
220
            return empty($array) ? value($default) : end($array);
221
        }
222
 
223
        return static::first(array_reverse($array, true), $callback, $default);
224
    }
225
 
226
    /**
227
     * Flatten a multi-dimensional array into a single level.
228
     *
229
     * @param  iterable  $array
230
     * @param  int  $depth
231
     * @return array
232
     */
233
    public static function flatten($array, $depth = INF)
234
    {
235
        $result = [];
236
 
237
        foreach ($array as $item) {
238
            $item = $item instanceof Collection ? $item->all() : $item;
239
 
240
            if (! is_array($item)) {
241
                $result[] = $item;
242
            } else {
243
                $values = $depth === 1
244
                    ? array_values($item)
245
                    : static::flatten($item, $depth - 1);
246
 
247
                foreach ($values as $value) {
248
                    $result[] = $value;
249
                }
250
            }
251
        }
252
 
253
        return $result;
254
    }
255
 
256
    /**
257
     * Remove one or many array items from a given array using "dot" notation.
258
     *
259
     * @param  array  $array
260
     * @param  array|string|int|float  $keys
261
     * @return void
262
     */
263
    public static function forget(&$array, $keys)
264
    {
265
        $original = &$array;
266
 
267
        $keys = (array) $keys;
268
 
269
        if (count($keys) === 0) {
270
            return;
271
        }
272
 
273
        foreach ($keys as $key) {
274
            // if the exact key exists in the top-level, remove it
275
            if (static::exists($array, $key)) {
276
                unset($array[$key]);
277
 
278
                continue;
279
            }
280
 
281
            $parts = explode('.', $key);
282
 
283
            // clean up before each pass
284
            $array = &$original;
285
 
286
            while (count($parts) > 1) {
287
                $part = array_shift($parts);
288
 
289
                if (isset($array[$part]) && static::accessible($array[$part])) {
290
                    $array = &$array[$part];
291
                } else {
292
                    continue 2;
293
                }
294
            }
295
 
296
            unset($array[array_shift($parts)]);
297
        }
298
    }
299
 
300
    /**
301
     * Get an item from an array using "dot" notation.
302
     *
303
     * @param  \ArrayAccess|array  $array
304
     * @param  string|int|null  $key
305
     * @param  mixed  $default
306
     * @return mixed
307
     */
308
    public static function get($array, $key, $default = null)
309
    {
310
        if (! static::accessible($array)) {
311
            return value($default);
312
        }
313
 
314
        if (is_null($key)) {
315
            return $array;
316
        }
317
 
318
        if (static::exists($array, $key)) {
319
            return $array[$key];
320
        }
321
 
322
        if (! str_contains($key, '.')) {
323
            return $array[$key] ?? value($default);
324
        }
325
 
326
        foreach (explode('.', $key) as $segment) {
327
            if (static::accessible($array) && static::exists($array, $segment)) {
328
                $array = $array[$segment];
329
            } else {
330
                return value($default);
331
            }
332
        }
333
 
334
        return $array;
335
    }
336
 
337
    /**
338
     * Check if an item or items exist in an array using "dot" notation.
339
     *
340
     * @param  \ArrayAccess|array  $array
341
     * @param  string|array  $keys
342
     * @return bool
343
     */
344
    public static function has($array, $keys)
345
    {
346
        $keys = (array) $keys;
347
 
348
        if (! $array || $keys === []) {
349
            return false;
350
        }
351
 
352
        foreach ($keys as $key) {
353
            $subKeyArray = $array;
354
 
355
            if (static::exists($array, $key)) {
356
                continue;
357
            }
358
 
359
            foreach (explode('.', $key) as $segment) {
360
                if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
361
                    $subKeyArray = $subKeyArray[$segment];
362
                } else {
363
                    return false;
364
                }
365
            }
366
        }
367
 
368
        return true;
369
    }
370
 
371
    /**
372
     * Determine if any of the keys exist in an array using "dot" notation.
373
     *
374
     * @param  \ArrayAccess|array  $array
375
     * @param  string|array  $keys
376
     * @return bool
377
     */
378
    public static function hasAny($array, $keys)
379
    {
380
        if (is_null($keys)) {
381
            return false;
382
        }
383
 
384
        $keys = (array) $keys;
385
 
386
        if (! $array) {
387
            return false;
388
        }
389
 
390
        if ($keys === []) {
391
            return false;
392
        }
393
 
394
        foreach ($keys as $key) {
395
            if (static::has($array, $key)) {
396
                return true;
397
            }
398
        }
399
 
400
        return false;
401
    }
402
 
403
    /**
404
     * Determines if an array is associative.
405
     *
406
     * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
407
     *
408
     * @param  array  $array
409
     * @return bool
410
     */
411
    public static function isAssoc(array $array)
412
    {
413
        $keys = array_keys($array);
414
 
415
        return array_keys($keys) !== $keys;
416
    }
417
 
418
    /**
419
     * Determines if an array is a list.
420
     *
421
     * An array is a "list" if all array keys are sequential integers starting from 0 with no gaps in between.
422
     *
423
     * @param  array  $array
424
     * @return bool
425
     */
426
    public static function isList($array)
427
    {
428
        return ! self::isAssoc($array);
429
    }
430
 
431
    /**
432
     * Join all items using a string. The final items can use a separate glue string.
433
     *
434
     * @param  array  $array
435
     * @param  string  $glue
436
     * @param  string  $finalGlue
437
     * @return string
438
     */
439
    public static function join($array, $glue, $finalGlue = '')
440
    {
441
        if ($finalGlue === '') {
442
            return implode($glue, $array);
443
        }
444
 
445
        if (count($array) === 0) {
446
            return '';
447
        }
448
 
449
        if (count($array) === 1) {
450
            return end($array);
451
        }
452
 
453
        $finalItem = array_pop($array);
454
 
455
        return implode($glue, $array).$finalGlue.$finalItem;
456
    }
457
 
458
    /**
459
     * Key an associative array by a field or using a callback.
460
     *
461
     * @param  array  $array
462
     * @param  callable|array|string  $keyBy
463
     * @return array
464
     */
465
    public static function keyBy($array, $keyBy)
466
    {
467
        return Collection::make($array)->keyBy($keyBy)->all();
468
    }
469
 
470
    /**
471
     * Prepend the key names of an associative array.
472
     *
473
     * @param  array  $array
474
     * @param  string  $prependWith
475
     * @return array
476
     */
477
    public static function prependKeysWith($array, $prependWith)
478
    {
479
        return Collection::make($array)->mapWithKeys(function ($item, $key) use ($prependWith) {
480
            return [$prependWith.$key => $item];
481
        })->all();
482
    }
483
 
484
    /**
485
     * Get a subset of the items from the given array.
486
     *
487
     * @param  array  $array
488
     * @param  array|string  $keys
489
     * @return array
490
     */
491
    public static function only($array, $keys)
492
    {
493
        return array_intersect_key($array, array_flip((array) $keys));
494
    }
495
 
496
    /**
497
     * Pluck an array of values from an array.
498
     *
499
     * @param  iterable  $array
500
     * @param  string|array|int|null  $value
501
     * @param  string|array|null  $key
502
     * @return array
503
     */
504
    public static function pluck($array, $value, $key = null)
505
    {
506
        $results = [];
507
 
508
        [$value, $key] = static::explodePluckParameters($value, $key);
509
 
510
        foreach ($array as $item) {
511
            $itemValue = data_get($item, $value);
512
 
513
            // If the key is "null", we will just append the value to the array and keep
514
            // looping. Otherwise we will key the array using the value of the key we
515
            // received from the developer. Then we'll return the final array form.
516
            if (is_null($key)) {
517
                $results[] = $itemValue;
518
            } else {
519
                $itemKey = data_get($item, $key);
520
 
521
                if (is_object($itemKey) && method_exists($itemKey, '__toString')) {
522
                    $itemKey = (string) $itemKey;
523
                }
524
 
525
                $results[$itemKey] = $itemValue;
526
            }
527
        }
528
 
529
        return $results;
530
    }
531
 
532
    /**
533
     * Explode the "value" and "key" arguments passed to "pluck".
534
     *
535
     * @param  string|array  $value
536
     * @param  string|array|null  $key
537
     * @return array
538
     */
539
    protected static function explodePluckParameters($value, $key)
540
    {
541
        $value = is_string($value) ? explode('.', $value) : $value;
542
 
543
        $key = is_null($key) || is_array($key) ? $key : explode('.', $key);
544
 
545
        return [$value, $key];
546
    }
547
 
548
    /**
549
     * Run a map over each of the items in the array.
550
     *
551
     * @param  array  $array
552
     * @param  callable  $callback
553
     * @return array
554
     */
555
    public static function map(array $array, callable $callback)
556
    {
557
        $keys = array_keys($array);
558
 
559
        try {
560
            $items = array_map($callback, $array, $keys);
561
        } catch (ArgumentCountError) {
562
            $items = array_map($callback, $array);
563
        }
564
 
565
        return array_combine($keys, $items);
566
    }
567
 
568
    /**
569
     * Push an item onto the beginning of an array.
570
     *
571
     * @param  array  $array
572
     * @param  mixed  $value
573
     * @param  mixed  $key
574
     * @return array
575
     */
576
    public static function prepend($array, $value, $key = null)
577
    {
578
        if (func_num_args() == 2) {
579
            array_unshift($array, $value);
580
        } else {
581
            $array = [$key => $value] + $array;
582
        }
583
 
584
        return $array;
585
    }
586
 
587
    /**
588
     * Get a value from the array, and remove it.
589
     *
590
     * @param  array  $array
591
     * @param  string|int  $key
592
     * @param  mixed  $default
593
     * @return mixed
594
     */
595
    public static function pull(&$array, $key, $default = null)
596
    {
597
        $value = static::get($array, $key, $default);
598
 
599
        static::forget($array, $key);
600
 
601
        return $value;
602
    }
603
 
604
    /**
605
     * Convert the array into a query string.
606
     *
607
     * @param  array  $array
608
     * @return string
609
     */
610
    public static function query($array)
611
    {
612
        return http_build_query($array, '', '&', PHP_QUERY_RFC3986);
613
    }
614
 
615
    /**
616
     * Get one or a specified number of random values from an array.
617
     *
618
     * @param  array  $array
619
     * @param  int|null  $number
620
     * @param  bool  $preserveKeys
621
     * @return mixed
622
     *
623
     * @throws \InvalidArgumentException
624
     */
625
    public static function random($array, $number = null, $preserveKeys = false)
626
    {
627
        $requested = is_null($number) ? 1 : $number;
628
 
629
        $count = count($array);
630
 
631
        if ($requested > $count) {
632
            throw new InvalidArgumentException(
633
                "You requested {$requested} items, but there are only {$count} items available."
634
            );
635
        }
636
 
637
        if (is_null($number)) {
638
            return $array[array_rand($array)];
639
        }
640
 
641
        if ((int) $number === 0) {
642
            return [];
643
        }
644
 
645
        $keys = array_rand($array, $number);
646
 
647
        $results = [];
648
 
649
        if ($preserveKeys) {
650
            foreach ((array) $keys as $key) {
651
                $results[$key] = $array[$key];
652
            }
653
        } else {
654
            foreach ((array) $keys as $key) {
655
                $results[] = $array[$key];
656
            }
657
        }
658
 
659
        return $results;
660
    }
661
 
662
    /**
663
     * Set an array item to a given value using "dot" notation.
664
     *
665
     * If no key is given to the method, the entire array will be replaced.
666
     *
667
     * @param  array  $array
668
     * @param  string|int|null  $key
669
     * @param  mixed  $value
670
     * @return array
671
     */
672
    public static function set(&$array, $key, $value)
673
    {
674
        if (is_null($key)) {
675
            return $array = $value;
676
        }
677
 
678
        $keys = explode('.', $key);
679
 
680
        foreach ($keys as $i => $key) {
681
            if (count($keys) === 1) {
682
                break;
683
            }
684
 
685
            unset($keys[$i]);
686
 
687
            // If the key doesn't exist at this depth, we will just create an empty array
688
            // to hold the next value, allowing us to create the arrays to hold final
689
            // values at the correct depth. Then we'll keep digging into the array.
690
            if (! isset($array[$key]) || ! is_array($array[$key])) {
691
                $array[$key] = [];
692
            }
693
 
694
            $array = &$array[$key];
695
        }
696
 
697
        $array[array_shift($keys)] = $value;
698
 
699
        return $array;
700
    }
701
 
702
    /**
703
     * Shuffle the given array and return the result.
704
     *
705
     * @param  array  $array
706
     * @param  int|null  $seed
707
     * @return array
708
     */
709
    public static function shuffle($array, $seed = null)
710
    {
711
        if (is_null($seed)) {
712
            shuffle($array);
713
        } else {
714
            mt_srand($seed);
715
            shuffle($array);
716
            mt_srand();
717
        }
718
 
719
        return $array;
720
    }
721
 
722
    /**
723
     * Sort the array using the given callback or "dot" notation.
724
     *
725
     * @param  array  $array
726
     * @param  callable|array|string|null  $callback
727
     * @return array
728
     */
729
    public static function sort($array, $callback = null)
730
    {
731
        return Collection::make($array)->sortBy($callback)->all();
732
    }
733
 
734
    /**
735
     * Recursively sort an array by keys and values.
736
     *
737
     * @param  array  $array
738
     * @param  int  $options
739
     * @param  bool  $descending
740
     * @return array
741
     */
742
    public static function sortRecursive($array, $options = SORT_REGULAR, $descending = false)
743
    {
744
        foreach ($array as &$value) {
745
            if (is_array($value)) {
746
                $value = static::sortRecursive($value, $options, $descending);
747
            }
748
        }
749
 
750
        if (static::isAssoc($array)) {
751
            $descending
752
                    ? krsort($array, $options)
753
                    : ksort($array, $options);
754
        } else {
755
            $descending
756
                    ? rsort($array, $options)
757
                    : sort($array, $options);
758
        }
759
 
760
        return $array;
761
    }
762
 
763
    /**
764
     * Conditionally compile classes from an array into a CSS class list.
765
     *
766
     * @param  array  $array
767
     * @return string
768
     */
769
    public static function toCssClasses($array)
770
    {
771
        $classList = static::wrap($array);
772
 
773
        $classes = [];
774
 
775
        foreach ($classList as $class => $constraint) {
776
            if (is_numeric($class)) {
777
                $classes[] = $constraint;
778
            } elseif ($constraint) {
779
                $classes[] = $class;
780
            }
781
        }
782
 
783
        return implode(' ', $classes);
784
    }
785
 
786
    /**
787
     * Filter the array using the given callback.
788
     *
789
     * @param  array  $array
790
     * @param  callable  $callback
791
     * @return array
792
     */
793
    public static function where($array, callable $callback)
794
    {
795
        return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH);
796
    }
797
 
798
    /**
799
     * Filter items where the value is not null.
800
     *
801
     * @param  array  $array
802
     * @return array
803
     */
804
    public static function whereNotNull($array)
805
    {
806
        return static::where($array, function ($value) {
807
            return ! is_null($value);
808
        });
809
    }
810
 
811
    /**
812
     * If the given value is not an array and not null, wrap it in one.
813
     *
814
     * @param  mixed  $value
815
     * @return array
816
     */
817
    public static function wrap($value)
818
    {
819
        if (is_null($value)) {
820
            return [];
821
        }
822
 
823
        return is_array($value) ? $value : [$value];
824
    }
825
}