-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.php
806 lines (706 loc) · 16.9 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
<?php
/**
* A PHP utility class for manipulating collections.
*/
namespace WP_Forge\Collection;
/**
* Class Collection
*/
class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable {
/**
* The items contained in the collection.
*
* @var array
*/
protected $items = [];
/**
* Collection constructor.
*
* @param array $items Initial collection items
*/
public function __construct( array $items = [] ) {
$this->items = $items;
}
/**
* Static method for creating a collection.
*
* @param array $items Initial collection items
*
* @return static
*/
public static function make( array $items = [] ) {
return new static( $items );
}
/**
* Get all of the items in the collection.
*
* @return array
*/
public function all() {
return $this->items;
}
/**
* Push all of the given items onto the collection.
*
* @param array $items Items to add to the collection
*
* @return $this
*/
public function concat( array $items ) {
$collection = new static( $this->all() );
foreach ( $items as $item ) {
$collection->push( $item );
}
return $collection;
}
/**
* Determine if an item exists in the collection.
*
* @param mixed $key Item for which to check
*
* @param bool $strict Whether or not to use a strict comparison
*
* @return bool
*/
public function contains( $key, $strict = true ) {
return in_array( $key, $this->items, $strict );
}
/**
* Get the total number of items in the collection.
*
* @return int
*/
#[\ReturnTypeWillChange]
public function count() {
return count( $this->items );
}
/**
* Get the items in the collection that are not present in the given items.
*
* @param array $items Items to exclude
*
* @return static
*/
public function diff( array $items ) {
return new static( array_diff( $this->items, $items ) );
}
/**
* Execute a callback over each item.
*
* @param callable $callback Callback to be mapped to each item
*
* @return $this
*/
public function each( callable $callback ) {
foreach ( $this->items as $key => $item ) {
$callback( $item, $key );
}
return $this;
}
/**
* Get all items except for those with the specified keys.
*
* @param array|string $keys Key(s) to exclude
*
* @return static
*/
public function except( $keys ) {
$keys = (array) $keys;
return $this->filter(
function ( $value, $key ) use ( $keys ) {
return ! in_array( $key, $keys, true );
}
);
}
/**
* Run a filter over each of the items.
*
* @param callable|null $callback Filter callback
*
* @return static
*/
public function filter( callable $callback = null ) {
if ( null === $callback ) {
return new static( array_filter( $this->items ) );
}
return new static( array_filter( $this->items, $callback, ARRAY_FILTER_USE_BOTH ) );
}
/**
* Get the first item from the collection.
*
* @return mixed
*/
public function first() {
return $this->slice( 0, 1 )->shift();
}
/**
* Flip the items in the collection.
*
* @return static
*/
public function flip() {
return new static( array_flip( $this->items ) );
}
/**
* Remove one or more items from the collection by key.
*
* @param string|array $keys Key(s) to exclude
*
* @return $this
*/
public function forget( $keys ) {
foreach ( (array) $keys as $key ) {
$this->offsetUnset( $key );
}
return $this;
}
/**
* "Paginate" the collection by slicing it into a smaller collection.
*
* @param int $page Page number
* @param int $perPage Number of items per page
*
* @return static
*/
public function forPage( $page, $perPage ) {
$offset = max( 0, ( $page - 1 ) * $perPage );
return $this->slice( $offset, $perPage );
}
/**
* Get an item from the collection by key.
*
* @param mixed $key Key used to find item
* @param mixed $default Default value to return if item doesn't exist
*
* @return mixed
*/
public function get( $key, $default = null ) {
$value = $default;
if ( $this->offsetExists( $key ) ) {
$value = $this->items[ $key ];
}
return $value;
}
/**
* Get an iterator for the items.
*
* @return \ArrayIterator
*/
#[\ReturnTypeWillChange]
public function getIterator() {
return new \ArrayIterator( $this->items );
}
/**
* Group an associative array by a field.
*
* @param callable|string $groupBy Property or index to group by
*
* @return static
*/
public function groupBy( $groupBy ) {
$results = [];
foreach ( $this->items as $item ) {
if ( is_array( $item ) && array_key_exists( $groupBy, $item ) ) {
$results[ $item[ $groupBy ] ][] = $item;
} elseif ( is_object( $item ) && property_exists( $item, $groupBy ) ) {
$results[ $item->{$groupBy} ][] = $item;
}
}
return new static( $results );
}
/**
* Determine if one or more items exist in the collection by key.
*
* @param mixed $key Key for which to check
*
* @return bool
*/
public function has( $key ) {
$keys = is_array( $key ) ? $key : func_get_args();
foreach ( $keys as $value ) {
if ( ! $this->offsetExists( $value ) ) {
return false;
}
}
return true;
}
/**
* Concatenate values of a given key as a string.
*
* @param string $glue String used to join the items
*
* @return string
*/
public function implode( $glue = null ) {
return implode( $glue, $this->items );
}
/**
* Index an associative array by a field.
*
* @param callable|string $indexBy Property or index used to index.
*
* @return static
*/
public function indexBy( $indexBy ) {
$results = [];
foreach ( $this->items as $item ) {
if ( is_array( $item ) && array_key_exists( $indexBy, $item ) ) {
$results[ $item[ $indexBy ] ] = $item;
} elseif ( is_object( $item ) && property_exists( $item, $indexBy ) ) {
$results[ $item->{$indexBy} ] = $item;
}
}
return new static( $results );
}
/**
* Insert a value or key/value pair after a specific key in an array. If key doesn't exist, value is appended
* to the end of the array.
*
* @param mixed $key The key after which the items will be inserted
* @param array $value An array containing the key(s) and value(s) to be inserted
*
* @return $this
*/
public function insertAfter( $key, array $value ) {
if ( ! $this->has( $key ) ) {
return $this->push( $value );
}
$index = array_search( $key, array_keys( $this->items ), true );
$pos = false === $index ? count( $this->items ) : $index + 1;
$this->items = array_merge( array_slice( $this->items, 0, $pos ), $value, array_slice( $this->items, $pos ) );
return $this;
}
/**
* Insert a value or key/value pair before a specific key in an array. If key doesn't exist, value is prepended
* to the beginning of the array.
*
* @param mixed $key The key before which the items will be inserted
* @param array $value An array containing the key(s) and value(s) to be inserted
*
* @return $this
*/
public function insertBefore( $key, array $value ) {
if ( ! $this->has( $key ) ) {
return $this->prepend( $value );
}
$pos = (int) array_search( $key, array_keys( $this->items ), true );
$this->items = array_merge( array_slice( $this->items, 0, $pos ), $value, array_slice( $this->items, $pos ) );
return $this;
}
/**
* Intersect the collection with the given items.
*
* @param array $items Items with which to intersect
*
* @return static
*/
public function intersect( array $items ) {
return new static( array_intersect( $this->items, $items ) );
}
/**
* Intersect the collection with the given items by key.
*
* @param array $keys Keys with which to intersect
*
* @return static
*/
public function intersectByKeys( array $keys ) {
return new static( array_intersect_key( $this->items, $keys ) );
}
/**
* Convert the object into something JSON serializable.
*
* @return array
*/
#[\ReturnTypeWillChange]
public function jsonSerialize() {
return $this->items;
}
/**
* Get the keys of the collection items.
*
* @return static
*/
public function keys() {
return array_keys( $this->items );
}
/**
* Get the last item from the collection.
*
* @return mixed
*/
public function last() {
return $this->slice( - 1, 1 )->pop();
}
/**
* Run the map over each of the items.
*
* @param callable $callback Callback to map to each item
*
* @return static
*/
public function map( callable $callback ) {
$keys = array_keys( $this->items );
$items = array_map( $callback, $this->items, $keys );
return new static( array_combine( $keys, $items ) );
}
/**
* Merge the collection with the given items.
*
* @param array $items Items to merge
*
* @return static
*/
public function merge( array $items ) {
return new static( array_merge( $this->items, $items ) );
}
/**
* Determine if an item exists at an offset.
*
* @param mixed $key Key for which to check
*
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists( $key ) {
return array_key_exists( $key, $this->items );
}
/**
* Get an item at a given offset.
*
* @param mixed $key Key used to find item
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet( $key ) {
return $this->items[ $key ];
}
/**
* Set the item at the given offset.
*
* @param mixed $key Key to set
* @param mixed $value Value to set
*/
#[\ReturnTypeWillChange]
public function offsetSet( $key, $value ) {
if ( null === $key ) {
$this->items[] = $value;
} else {
$this->items[ $key ] = $value;
}
}
/**
* Unset the item at a given offset.
*
* @param mixed $key Key to unset
*/
#[\ReturnTypeWillChange]
public function offsetUnset( $key ) {
unset( $this->items[ $key ] );
}
/**
* Get the items with the specified keys.
*
* @param array $keys Keys to fetch
*
* @return static
*/
public function only( $keys ) {
return $this->filter(
function ( $value, $key ) use ( $keys ) {
return in_array( $key, $keys, true );
}
);
}
/**
* Get the values of a given key.
*
* @param string|array|null $key Key to fetch
* @param string|null $indexKey Key to use as index
*
* @return static
*/
public function pluck( $key, $indexKey = null ) {
return new static( array_column( $this->items, $key, $indexKey ) );
}
/**
* Get and remove the last item from the collection.
*
* @return mixed
*/
public function pop() {
return array_pop( $this->items );
}
/**
* Push an item onto the beginning of the collection.
*
* @param mixed $value Value to prepend
*
* @return $this
*/
public function prepend( $value ) {
array_unshift( $this->items, $value );
return $this;
}
/**
* Get and remove an item from the collection.
*
* @param mixed $key Key by which to fetch
* @param mixed $default Value to return if item doesn't exist
*
* @return mixed
*/
public function pull( $key, $default = null ) {
$value = $this->get( $key, $default );
$this->offsetUnset( $key );
return $value;
}
/**
* Push an item onto the end of the collection.
*
* @param mixed $value Item to append
*
* @return $this
*/
public function push( $value ) {
$this->offsetSet( null, $value );
return $this;
}
/**
* Put an item in the collection by key.
*
* @param mixed $key Key to set
* @param mixed $value Value to set
*
* @return $this
*/
public function put( $key, $value ) {
$this->offsetSet( $key, $value );
return $this;
}
/**
* Get one or a specified number of items randomly from the collection.
*
* @param int $count Number of items to return
*
* @return static
*/
public function random( $count = 1 ) {
$values = [];
$keys = (array) array_rand( $this->items, min( $count, $this->count() ) );
foreach ( $keys as $key ) {
$values[ $key ] = $this->offsetGet( $key );
}
return new static( $values );
}
/**
* Reverse items order.
*
* @return static
*/
public function reverse() {
return new static( array_reverse( $this->items, true ) );
}
/**
* Search the collection for a given value and return the corresponding key if successful.
*
* @param mixed $value Value by which to search
* @param bool $strict Whether or not to use a strict comparison
*
* @return mixed
*/
public function search( $value, $strict = false ) {
return array_search( $value, $this->items, $strict );
}
/**
* Get and remove the first item from the collection.
*
* @return mixed
*/
public function shift() {
return array_shift( $this->items );
}
/**
* Shuffle the items in the collection.
*
* @return static
*/
public function shuffle() {
return new static( shuffle( $this->items ) );
}
/**
* Slice the underlying collection array.
*
* @param int $offset Numeric offset
* @param int $length Length of returned collection
*
* @return static
*/
public function slice( $offset, $length = null ) {
return new static( array_slice( $this->items, $offset, $length, true ) );
}
/**
* Sort through each item with a callback.
*
* @param callable|null $callback Callback used for sorting
*
* @return static
*/
public function sort( callable $callback = null ) {
$items = $this->items;
$callback ? uasort( $items, $callback ) : asort( $items );
return new static( $items );
}
/**
* Sort the collection keys.
*
* @param int $options Can be SORT_REGULAR (0), SORT_NUMERIC (1), SORT_STRING (2), SORT_LOCALE_STRING (5)
* @param bool $descending True to sort descending, false to sort ascending
*
* @return static
*/
public function sortKeys( $options = SORT_REGULAR, $descending = false ) {
$items = $this->items;
$descending ? krsort( $items, $options ) : ksort( $items, $options );
return new static( $items );
}
/**
* Take the first or last {$limit} items.
*
* @param int $limit Number of items to return (negative number returns last items, positive returns first items)
*
* @return static
*/
public function take( $limit ) {
if ( $limit < 0 ) {
return $this->slice( $limit, abs( $limit ) );
}
return $this->slice( 0, $limit );
}
/**
* Pass the collection to the given callback and then return it.
*
* @param callable $callback Callback to use
*
* @return $this
*/
public function tap( callable $callback ) {
$callback( new static( $this->items ) );
return $this;
}
/**
* Get the collection of items as an array.
*
* @return array
*/
public function toArray() {
return $this->all();
}
/**
* Get the collection of items as JSON.
*
* @param int $options Can be JSON_PRETTY_PRINT or other such constants.
*
* @return string
*/
public function toJson( $options = 0 ) {
return json_encode( $this->jsonSerialize(), $options );
}
/**
* Convert the collection to its string representation.
*
* @return string
*/
public function toString() {
return $this->toJson();
}
/**
* Transform each item in the collection using a callback.
*
* @param callable $callback Callback to use
*
* @return $this
*/
public function transform( callable $callback ) {
$this->items = $this->map( $callback )->all();
return $this;
}
/**
* Return only unique items from the collection array.
*
* @return static
*/
public function unique() {
return new static( array_unique( $this->items ) );
}
/**
* Reset the keys on the underlying array.
*
* @return static
*/
public function values() {
return array_values( $this->items );
}
/**
* Apply the callback if the value is truthy. Otherwise, call the fallback (if set).
*
* @param bool $value Evaluated conditional
* @param callable $callback Callback to apply
* @param callable $fallback Fallback value to return
*
* @return mixed
*/
public function when( $value, callable $callback, callable $fallback = null ) {
return $value ? $callback( $this, $value ) : ( $fallback ? $fallback( $this, $value ) : $this );
}
/**
* Filter items by the given key value pair.
*
* @param string $key Key to fetch
* @param mixed $operator Operator to use
* @param mixed $value Value to check
*
* @return static
*/
public function where( $key, $operator, $value = null ) {
if ( func_num_args() === 2 ) {
$value = $operator;
$operator = '=';
}
return $this->filter(
function ( $item ) use ( $key, $operator, $value ) {
$retrieved = isset( $item[ $key ] ) ? $item[ $key ] : null;
switch ( $operator ) {
default:
case '=':
case '==':
return $retrieved == $value;
case '!=':
case '<>':
return $retrieved != $value;
case '<':
return $retrieved < $value;
case '>':
return $retrieved > $value;
case '<=':
return $retrieved <= $value;
case '>=':
return $retrieved >= $value;
case '===':
return $retrieved === $value;
case '!==':
return $retrieved !== $value;
}
}
);
}
/**
* Convert the collection to its string representation.
*
* @return string
*/
public function __toString() {
return $this->toString();
}
}