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