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: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | trait ListCollectionTrait |
18: | { |
19: | |
20: | use CollectionTrait { |
21: | getItems as private doGetItems; |
22: | replaceItems as private doReplaceItems; |
23: | } |
24: | |
25: | |
26: | |
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: | |
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: | |
54: | |
55: | |
56: | protected function getItems($items): iterable |
57: | { |
58: | foreach ($this->doGetItems($items) as $value) { |
59: | yield $value; |
60: | } |
61: | } |
62: | |
63: | |
64: | |
65: | |
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: | |