| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Collection; |
| 4: | |
| 5: | use Salient\Contract\Core\Arrayable; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | trait ListCollectionTrait |
| 15: | { |
| 16: | |
| 17: | use CollectionTrait { |
| 18: | getItems as doGetItems; |
| 19: | replaceItems as private doReplaceItems; |
| 20: | } |
| 21: | |
| 22: | |
| 23: | |
| 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: | |
| 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: | |
| 51: | |
| 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: | |
| 63: | |
| 64: | |
| 65: | private function getItems($items): iterable |
| 66: | { |
| 67: | foreach ($this->doGetItems($items) as $value) { |
| 68: | yield $value; |
| 69: | } |
| 70: | } |
| 71: | } |
| 72: | |