/
Collection.php
759 lines (657 loc) · 14.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
<?php
declare(strict_types=1);
namespace Bow\Support;
use Generator as PHPGenerator;
class Collection implements \Countable, \JsonSerializable, \IteratorAggregate, \ArrayAccess
{
/**
* The collection store
*
* @var array
*/
protected array $storage = [];
/**
* Collection constructor
*
* @param array $arr
*/
public function __construct(array $storage = [])
{
$this->storage = $storage;
}
/**
* The first element of the list
*
* @return mixed
*/
public function first(): mixed
{
return current($this->storage);
}
/**
* The last element of the list
*
* @return mixed
*/
public function last(): mixed
{
$element = end($this->storage);
reset($this->storage);
return $element;
}
/**
* Check existence of a key in the session collection
*
* @param string $key
* @param bool $strict
* @return bool
*/
public function has(string $key, bool $strict = false): bool
{
// When $strict is true, he check $key not how a key but a value.
$isset = isset($this->storage[$key]);
if ($isset) {
if ($strict === true) {
$isset = $isset && !empty($this->storage[$key]);
}
}
return $isset;
}
/**
* Check if a collection is empty.
*
* @return bool
*/
public function isEmpty(): bool
{
$isEmpty = empty($this->storage);
if ($isEmpty === false) {
if ($this->length() == 1) {
if (is_null($this->values()[0])) {
$isEmpty = true;
}
}
}
return $isEmpty;
}
/**
* Allows to recover a value or value collection.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key = null, mixed $default = null)
{
if (is_null($key)) {
return $this->storage;
}
if ($this->has($key)) {
return $this->storage[$key] == null
? $default
: $this->storage[$key];
}
if ($default !== null) {
if (is_callable($default)) {
return call_user_func($default);
}
return $default;
}
return null;
}
/**
* Get the list of values of collection
*
* @return Collection
*/
public function values(): Collection
{
$r = [];
foreach ($this->storage as $value) {
array_push($r, $value);
}
return new Collection($r);
}
/**
* Get the list of keys of collection
*
* @return Collection
*/
public function keys(): Collection
{
$r = [];
foreach ($this->storage as $key => $value) {
array_push($r, $key);
}
return new Collection($r);
}
/**
* Count the collection element.
*
* @return int
*/
public function count(): int
{
return count($this->storage);
}
/**
* To retrieve a value or value collection form d'instance de collection.
*
* @param string $key
* @return Collection
*/
public function collectionify(string $key): Collection
{
$data = [];
if ($this->has($key)) {
$data = $this->storage[$key];
if (!is_array($data)) {
$data = [$data];
}
}
return new Collection($data);
}
/**
* Delete an entry in the collection
*
* @param string $key
* @return Collection
*/
public function delete(string $key): Collection
{
unset($this->storage[$key]);
return $this;
}
/**
* Modify an entry in the collection or the addition if not
*
* @param string $key
* @param mixed $value
* @return mixed
*/
public function set(string $key, mixed $value): mixed
{
if ($this->has($key)) {
$old = $this->storage[$key];
$this->storage[$key] = $value;
return $old;
}
$this->storage[$key] = $value;
return null;
}
/**
* Browse all the values of the collection
*
* @param callable $cb
*/
public function each(callable $cb): void
{
foreach ($this->storage as $key => $value) {
call_user_func_array($cb, [$value, $key]);
}
}
/**
* Merge the collection with a painting or other collection
*
* @param Collection|array $array
* @return Collection
* @throws \ErrorException
*/
public function merge(Collection|array $array): Collection
{
if (is_array($array)) {
$this->storage = array_merge(
$this->storage,
$array
);
} elseif ($array instanceof Collection) {
$this->storage = array_merge(
$this->storage,
$array->toArray()
);
} else {
throw new \ErrorException(
'Must be take 1 parameter to be array or Collection',
E_ERROR
);
}
return $this;
}
/**
* Map
*
* @param callable $cb
* @return Collection
*/
public function map(callable $cb): Collection
{
$data = $this->storage;
$new = [];
foreach ($data as $key => $value) {
$new[$key] = call_user_func_array($cb, [$value, $key]);
}
return new Collection($new);
}
/**
* Filter
*
* @param callable $cb
* @return Collection
*/
public function filter(callable $cb): Collection
{
$data = [];
foreach ($this->storage as $key => $value) {
if (call_user_func_array($cb, [$value, $key])) {
$data[] = $this->storage[$key];
}
}
return new Collection($data);
}
/**
* Fill storage
*
* @param mixed $data
* @param int $offset
* @return array
*/
public function fill(mixed $data, int $offset): mixed
{
$old = $this->storage;
$len = count($old);
for ($i = $len, $len += $offset; $i < $len; $i++) {
$this->storage[$i] = $data;
}
return $old;
}
/**
* Reduce
*
* @param callable $cb
* @param mixed $next
* @return Collection
*/
public function reduce(callable $cb, $next = null): Collection
{
foreach ($this->storage as $key => $current) {
$next = call_user_func_array($cb, [
$next, $current, $key, $this->storage
]);
}
return $this;
}
/**
* Implode
*
* @param string $sep
* @return string
*/
public function implode(string $sep): string
{
return implode($sep, $this->toArray());
}
/**
* Sum
*
* @param callable $cb
* @return int|float
*/
public function sum(callable $cb = null): int|float
{
$sum = 0;
$this->recursive(
$this->storage,
function ($value) use (&$sum) {
if (is_numeric($value)) {
$sum += $value;
}
}
);
if ($cb !== null) {
call_user_func_array($cb, [$sum]);
}
return $sum;
}
/**
* Max
*
* @param ?callable $cb
* @return int|float
*/
public function max(?callable $cb = null): int|float
{
return $this->aggregate('max', $cb);
}
/**
* Max
*
* @param ?callable $cb
* @return int|float
*/
public function min(?callable $cb = null)
{
return $this->aggregate('min', $cb);
}
/**
* Aggregate Execute max|min
*
* @param callable $cb
* @param string $type
* @return int|float
*/
private function aggregate($type, $cb = null)
{
$data = [];
$this->recursive(
$this->storage,
function ($value) use (&$data) {
if (is_numeric($value)) {
$data[] = $value;
}
}
);
$result = call_user_func_array($type, $data);
if (is_callable($cb)) {
call_user_func_array($cb, [$result]);
}
return $result;
}
/**
* Returns the key list and return an instance of Collection.
*
* @param array $except
* @return Collection
*/
public function excepts(array $except): Collection
{
$data = [];
$this->recursive(
$this->storage,
function ($value, $key) use (&$data, $except) {
if (in_array($key, $except)) {
$data[$key] = $value;
}
}
);
return new Collection($data);
}
/**
* Ignore the key that is given to it and return an instance of Collection.
*
* @param array $ignores
* @return Collection
*/
public function ignores(array $ignores): Collection
{
$data = [];
$this->recursive(
$this->storage,
function ($value, $key) use (&$data, $ignores) {
if (!in_array($key, $ignores)) {
$data[$key] = $value;
}
}
);
return new Collection($data);
}
/**
* Reverse collection
*
* @return Collection
*/
public function reverse(): Collection
{
return new Collection(array_reverse($this->storage));
}
/**
* Update an existing value in the collection
*
* @param string|integer $key
* @param mixed $data
* @param bool $override
* @return bool
*/
public function update(mixed $key, mixed $data, bool $override = false): bool
{
if (!$this->has($key)) {
return false;
}
if (!is_array($this->storage[$key]) || $override === true) {
$this->storage[$key] = $data;
return true;
}
if (!is_array($data)) {
$data = [$data];
}
$this->storage[$key] = array_merge($this->storage[$key], $data);
return true;
}
/**
* Launches the collection content as generator
*
* @return PHPGenerator
*/
public function yieldify(): PHPGenerator
{
foreach ($this->storage as $key => $value) {
yield (object) [
'value' => $value,
'key' => $key,
'done' => false
];
}
yield (object) [
'value' => null,
'key' => null,
'done' => true
];
}
/**
* Get the data in JSON format
*
* @param int $option
* @return string
*/
public function toJson(int $option = 0): string
{
return json_encode($this->storage, $option);
}
/**
* Length of the collection
*
* @return int
*/
public function length(): int
{
return count($this->storage);
}
/**
* Deletes the first item in the collection
*
* @return mixed
*/
public function shift(): mixed
{
$data = $this->storage;
return array_shift($data);
}
/**
* Deletes the last item in the collection
*
* @return array
*/
public function pop(): mixed
{
return array_pop($this->storage);
}
/**
* Returns the elements of the collection in table format
*
* @return array
*/
public function toArray(): array
{
$collection = [];
$this->recursive(
$this->storage,
function ($value, $key) use (&$collection) {
if (is_object($value)) {
$collection[$key] = (array) $value;
} else {
$collection[$key] = $value;
}
}
);
return $collection;
}
/**
* Returns the elements of the collection
*
* @return array
*/
public function all(): array
{
return $this->storage;
}
/**
* Add after the last item in the collection
*
* @param mixed $value
* @param int|string $key
* @return Collection
*/
public function push(mixed $value, mixed $key = null): Collection
{
if ($key == null) {
$this->storage[] = $value;
} else {
$this->storage[$key] = $value;
}
return $this;
}
/**
* Recursive walk of a table or object
*
* @param array $data
* @param callable $cb
*/
private function recursive(array $data, callable $cb)
{
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value)) {
$this->recursive((array) $value, $cb);
} else {
$cb($value, $key);
}
}
}
/**
* __get
*
* @param mixed $name
* @return mixed
*/
public function __get($name)
{
return $this->get($name);
}
/**
* __set
*
* @param mixed $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
{
$this->storage[$name] = $value;
}
/**
* __isset
*
* @param mixed $name
* @return bool
*/
public function __isset($name)
{
return $this->has($name);
}
/**
* __unset
*
* @param mixed $name
* @return void
*/
public function __unset($name)
{
$this->delete($name);
}
/**
* __toString
*
* @return string
*/
public function __toString()
{
return $this->toJson();
}
/**
* jsonSerialize
*
* @return string
*/
public function jsonSerialize(): mixed
{
return $this->storage;
}
/**
* getIterator
*
* @return \ArrayIterator
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->storage);
}
/**
* offsetExists
*
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return $this->has($offset);
}
/**
* offsetGet
*
* @param mixed $offset
* @return mixed
*/
public function offsetGet($offset): mixed
{
return $this->get($offset);
}
/**
* offsetSet
*
* @param mixed $offset
* @param mixed $value
* @return void
*/
public function offsetSet($offset, $value): void
{
$this->set($offset, $value);
}
/**
* offsetUnset
*
* @param mixed $offset
* @return void
*/
public function offsetUnset($offset): void
{
unset($this->storage[$offset]);
}
}