This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.php
626 lines (539 loc) · 14 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
<?php
declare(strict_types=1);
namespace Palmtree\EasyCollection;
/**
* @template TKey of array-key
* @template T
*
* @template-implements \IteratorAggregate<TKey,T>
* @template-implements \ArrayAccess<TKey,T>
*
* @psalm-consistent-constructor
*/
class Collection implements \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerializable
{
/**
* @var array<TKey,T>
*/
private array $elements = [];
/**
* @param iterable<TKey,T> $elements
*/
public function __construct(iterable $elements = [])
{
foreach ($elements as $key => $element) {
$this->set($key, $element);
}
}
/**
* @template UKey of array-key
* @template U
*
* @param iterable<UKey,U> $elements
*
* @return Collection<UKey, U>
*/
public static function create(iterable $elements = []): self
{
return new static($elements);
}
/**
* Returns the element with the given key.
*
* @param TKey $key
*
* @return T
*/
public function get(int|string $key): mixed
{
return $this->elements[$key];
}
/**
* Sets the given element to the given key in the collection.
*
* @param TKey $key
* @param T $element
*
* @return Collection<TKey, T>
*/
public function set(int|string $key, mixed $element): self
{
$this->elements[$key] = $element;
return $this;
}
/**
* Adds the given element onto the end of the collection.
*
* @param T ...$element
*
* @return Collection<TKey,T>
*/
public function add(mixed ...$element): self
{
foreach ($element as $el) {
/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->elements[] = $el;
}
return $this;
}
/**
* Removes the element with the given key from the collection and returns it.
*
* @param TKey $key
*
* @return T|null
*/
public function remove(int|string $key)
{
if (!$this->containsKey($key)) {
return null;
}
$removed = $this->elements[$key];
unset($this->elements[$key]);
return $removed;
}
/**
* Removes the given element from the collection and returns whether it existed or not.
*
* @param T $element
*/
public function removeElement(mixed $element): bool
{
$key = $this->key($element);
if ($key === false) {
return false;
}
unset($this->elements[$key]);
return true;
}
/**
* Returns a new collection containing the current collection's keys.
*
* @return Collection<int, TKey>
*/
public function keys(): self
{
return new self(array_keys($this->elements));
}
/**
* Returns a new collection containing the current collection's values.
*
* @return Collection<int, T>
*/
public function values(): self
{
return new self(array_values($this->elements));
}
/**
* Returns the first key in the collection.
*
* @return TKey|null
*/
public function firstKey(): int|string|null
{
return array_key_first($this->elements);
}
/**
* Returns the last key in the collection.
*
* @return TKey|null
*/
public function lastKey(): int|string|null
{
return array_key_last($this->elements);
}
/**
* Returns the first element in the collection.
*
* @return T|null
*/
public function first(): mixed
{
$firstKey = $this->firstKey();
if ($firstKey === null) {
return null;
}
return $this->elements[$firstKey];
}
/**
* Returns the last element in the collection.
*
* @return T|null
*/
public function last(): mixed
{
$lastKey = $this->lastKey();
if ($lastKey === null) {
return null;
}
return $this->elements[$lastKey];
}
/**
* Returns the given element's key if it is present within the collection or false otherwise.
*
* @param T $element
*
* @return TKey|false
*/
public function key(mixed $element): int|string|false
{
return array_search($element, $this->elements, true);
}
/**
* Returns whether the given element exists in the collection.
*
* @param T $element
*/
public function contains(mixed $element): bool
{
return \in_array($element, $this->elements, true);
}
/**
* Returns whether the given key exists in the collection.
*
* @param TKey $key
*/
public function containsKey(int|string $key): bool
{
return isset($this->elements[$key]) || \array_key_exists($key, $this->elements);
}
/**
* Applies the given callback function to each element in the collection.
*
* @psalm-param callable(T, TKey=, int=):bool $callback
*/
public function each(callable $callback): self
{
$loopIndex = 0;
foreach ($this->elements as $key => $element) {
$result = $callback($element, $key, $loopIndex++);
if ($result === false) {
break;
}
}
return $this;
}
/**
* Returns the first matching element which passes the predicate function.
*
* @param callable(T, TKey=):bool $predicate
*
* @return T|null
*/
public function find(callable $predicate): mixed
{
foreach ($this->elements as $element) {
if ($predicate($element)) {
return $element;
}
}
return null;
}
/**
* Returns a new collection containing all elements which pass the predicate function.
*
* @param ?callable(T, TKey=):bool $predicate
*
* @return Collection<TKey,T>
*
* @psalm-suppress PossiblyNullArgument
*/
public function filter(?callable $predicate = null): self
{
return new self(array_filter($this->elements, $predicate, \ARRAY_FILTER_USE_BOTH));
}
/**
* Returns a new collection whose values are mapped by the callback function.
*
* @param callable(T, TKey=):U $callback
*
* @return Collection<TKey, U>
*
* @template U
*/
public function map(callable $callback): self
{
$mapped = [];
foreach ($this->elements as $key => $element) {
$mapped[$key] = $callback($element, $key);
}
return new self($mapped);
}
/**
* Reduces the collection a single value.
*
* @param callable(mixed, T):mixed $callback
*/
public function reduce(callable $callback, mixed $initial = null): mixed
{
return array_reduce($this->elements, $callback, $initial);
}
/**
* Returns whether at least one element passes the predicate function.
*
* @param callable(T, TKey=):bool $predicate
*/
public function some(callable $predicate): bool
{
foreach ($this->elements as $key => $value) {
if ($predicate($value, $key)) {
return true;
}
}
return false;
}
/**
* Returns whether all elements pass the predicate function.
*
* @param callable(T, TKey=):bool $predicate
*/
public function every(callable $predicate): bool
{
foreach ($this->elements as $key => $value) {
if (!$predicate($value, $key)) {
return false;
}
}
return true;
}
/**
* Returns a new sorted collection. Defaults to SORT_REGULAR behaviour.
* Note that internally this method uses asort so index association is maintained.
*
* @see https://www.php.net/manual/en/function.asort.php
*
* @param int $flags Flags to pass to asort
*
* @return Collection<TKey,T>
*/
public function sort(int $flags = \SORT_REGULAR): self
{
$copy = $this->toArray();
asort($copy, $flags);
return new self($copy);
}
/**
* Returns a new collection which is sorted with a comparator function.
* Note that internally this method uses uasort so index association is maintained.
*
* @see https://www.php.net/manual/en/function.asort.php
* @see https://www.php.net/manual/en/function.uasort.php
*
* @param callable(T,T):int $comparator
*
* @return Collection<TKey,T>
*/
public function usort(callable $comparator): self
{
$copy = $this->toArray();
uasort($copy, $comparator);
return new self($copy);
}
/**
* Returns a new collection which is sorted by the collection's keys.
*
* @see https://www.php.net/manual/en/function.ksort.php
*
* @return Collection<TKey,T>
*/
public function ksort(int $flags = \SORT_REGULAR): self
{
$copy = $this->toArray();
ksort($copy, $flags);
return new self($copy);
}
/**
* Returns whether the collection is empty.
*/
public function isEmpty(): bool
{
return empty($this->elements);
}
/**
* Clears the collection.
*
* @return Collection<TKey,T>
*/
public function clear(): self
{
$this->elements = [];
return $this;
}
/**
* Returns whether the collection is a list as per array_is_list.
*/
public function isList(): bool
{
if (\PHP_VERSION_ID >= 80100) {
return array_is_list($this->elements);
}
return $this->isEmpty() || $this->elements === array_values($this->elements);
}
/**
* Returns the number of elements in the collection.
*/
public function count(): int
{
return \count($this->elements);
}
/**
* @template U
*
* @return Collection<TKey, U>
*
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArrayAccess
*/
public function pluck(string $field): self
{
$plucked = [];
foreach ($this->elements as $key => $value) {
if (\is_array($value) || $value instanceof \ArrayAccess) {
$newValue = $value[$field];
} elseif (\is_object($value)) {
$newValue = $value->$field;
} else {
throw new \LogicException('Collection values must be either arrays or objects');
}
$plucked[$key] = $newValue;
}
return new self($plucked);
}
/**
* @return Collection<TKey, T>
*/
public function slice(int $offset, ?int $length, bool $preserveKeys = false): self
{
return new self(\array_slice($this->elements, $offset, $length, $preserveKeys));
}
/**
* @param iterable<array-key, T> ...$iterables
*
* @return Collection<TKey, T>
*/
public function diff(iterable ...$iterables): self
{
$arrays = [];
foreach ($iterables as $iterable) {
$arrays[] = [...$iterable];
}
return new self(array_diff($this->elements, ...$arrays));
}
/**
* @param iterable<array-key, T> ...$iterables
*
* @return Collection<TKey, T>
*/
public function intersect(iterable ...$iterables): self
{
$arrays = [];
foreach ($iterables as $iterable) {
$arrays[] = [...$iterable];
}
return new self(array_intersect($this->elements, ...$arrays));
}
/**
* @return Collection<TKey, T>
*/
public function unique(bool $strict = false): self
{
if ($strict) {
$result = [];
foreach ($this->elements as $key => $value) {
if (!\in_array($value, $result, true)) {
$result[$key] = $value;
}
}
return new self($result);
}
return new self(array_unique($this->elements));
}
/**
* @psalm-suppress MixedArgumentTypeCoercion
*/
public function implode(string $separator = ','): string
{
return implode($separator, array_values($this->elements));
}
public function reverse(bool $preserveKeys = false): self
{
return new self(array_reverse($this->elements, $preserveKeys));
}
public function flip(): self
{
/** @psalm-suppress PossiblyInvalidArgument */
return new self(array_flip($this->elements));
}
/**
* @return Collection<int, T>
*/
public function flatten(): self
{
$result = [];
array_walk_recursive(
$this->elements,
/** @param T $a */
function (mixed $a) use (&$result) {
\assert(\is_array($result));
$result[] = $a;
}
);
/** @var array<int, T> $result */
return new self($result);
}
/**
* Returns the collection as a native array.
*
* @return array<TKey,T>
*/
public function toArray(): array
{
return $this->elements;
}
/**
* @return \Traversable<TKey,T>
*/
public function getIterator(): \Traversable
{
return new \ArrayIterator($this->elements);
}
/**
* @param TKey $offset
*/
public function offsetExists($offset): bool
{
return $this->containsKey($offset);
}
/**
* @param TKey $offset
*
* @return T
*/
public function offsetGet(mixed $offset): mixed
{
return $this->get($offset);
}
/**
* @param TKey|null $offset
* @param T $value
*/
public function offsetSet(mixed $offset, mixed $value): void
{
if (!isset($offset)) {
$this->add($value);
return;
}
$this->set($offset, $value);
}
/**
* @param TKey $offset
*/
public function offsetUnset(mixed $offset): void
{
$this->remove($offset);
}
public function jsonSerialize(): array
{
return $this->elements;
}
}