1: <?php declare(strict_types=1);
2:
3: namespace Salient\Collection;
4:
5: use Salient\Contract\Collection\CollectionInterface;
6: use Salient\Contract\Core\Arrayable;
7:
8: /**
9: * @api
10: *
11: * @template TKey of int
12: * @template TValue
13: * @template TKeyless
14: *
15: * @phpstan-require-implements CollectionInterface
16: */
17: trait ListCollectionTrait
18: {
19: /** @use CollectionTrait<int,TValue,TKeyless> */
20: use CollectionTrait {
21: getItems as private doGetItems;
22: replaceItems as private doReplaceItems;
23: }
24:
25: /**
26: * @inheritDoc
27: */
28: public function merge($items)
29: {
30: $items = $this->getItemsArray($items);
31: if (!$items) {
32: return $this;
33: }
34: $merged = array_merge($this->Items, $items);
35: return $this->replaceItems($merged, true);
36: }
37:
38: /**
39: * @inheritDoc
40: */
41: public function shift(&$first = null)
42: {
43: if (!$this->Items) {
44: $first = null;
45: return $this;
46: }
47: $items = $this->Items;
48: $first = array_shift($items);
49: return $this->replaceItems($items, true);
50: }
51:
52: /**
53: * @param Arrayable<array-key,TValue>|iterable<array-key,TValue> $items
54: * @return iterable<TValue>
55: */
56: protected function getItems($items): iterable
57: {
58: foreach ($this->doGetItems($items) as $value) {
59: yield $value;
60: }
61: }
62:
63: /**
64: * @param array<int,TValue> $items
65: * @return static
66: */
67: protected function replaceItems(array $items, bool $trustKeys = false, bool $getClone = true)
68: {
69: if (!$trustKeys) {
70: $items = array_values($items);
71: }
72: return $this->doReplaceItems($items, $trustKeys, $getClone);
73: }
74: }
75: