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