alex-patterson-webdev /
domino-game
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Arp\DominoGame\Value; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @author Alex Patterson <[email protected]> |
||
| 9 | * @package Arp\DominoGame\Value |
||
| 10 | */ |
||
| 11 | abstract class AbstractCollection implements CollectionInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The elements within the collection. |
||
| 15 | * |
||
| 16 | * @var array |
||
| 17 | */ |
||
| 18 | protected array $elements = []; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @param array $elements |
||
| 22 | */ |
||
| 23 | public function __construct(array $elements = []) |
||
| 24 | { |
||
| 25 | $this->setElements($elements); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Return an iterable representation of the collection. |
||
| 30 | * |
||
| 31 | * @return \ArrayIterator |
||
| 32 | */ |
||
| 33 | public function getIterator(): \ArrayIterator |
||
| 34 | { |
||
| 35 | return new \ArrayIterator($this->elements); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param iterable $elements |
||
| 40 | */ |
||
| 41 | public function setElements(iterable $elements): void |
||
| 42 | { |
||
| 43 | $this->removeElements($this->elements); |
||
| 44 | $this->addElements($elements); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param iterable $elements |
||
| 49 | */ |
||
| 50 | public function addElements(iterable $elements): void |
||
| 51 | { |
||
| 52 | foreach ($elements as $element) { |
||
| 53 | $this->elements[] = $element; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | public function getElements(): array |
||
| 61 | { |
||
| 62 | return $this->elements; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Check if the collection is empty or not. |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | public function isEmpty(): bool |
||
| 71 | { |
||
| 72 | return (0 === $this->count()); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @return int |
||
| 77 | */ |
||
| 78 | public function count(): int |
||
| 79 | { |
||
| 80 | return count($this->elements); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Remove an element from the collection. |
||
| 85 | * |
||
| 86 | * @param object $element |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function removeElement(object $element): bool |
||
| 91 | { |
||
| 92 | $index = array_search($element, $this->elements, true); |
||
| 93 | |||
| 94 | if (false === $index) { |
||
| 95 | return false; |
||
| 96 | } |
||
| 97 | unset($this->elements[$index]); |
||
| 98 | return true; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Remove elements from the collection. |
||
| 103 | * |
||
| 104 | * @param iterable|null $elements |
||
| 105 | * |
||
| 106 | * @return mixed|void |
||
| 107 | */ |
||
| 108 | public function removeElements(?iterable $elements) |
||
| 109 | { |
||
| 110 | if (null === $elements) { |
||
| 111 | $elements = $this->elements; |
||
| 112 | } |
||
| 113 | foreach ($elements as $element) { |
||
| 114 | $index = array_search($element, $this->elements, true); |
||
| 115 | if (false !== $index) { |
||
| 116 | unset($this->elements[$index]); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Return the first element in the collection. |
||
| 123 | * |
||
| 124 | * @return object|null |
||
| 125 | */ |
||
| 126 | public function first(): ?object |
||
| 127 | { |
||
| 128 | return $this->elements[0] ?? null; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return the last element in the collection. |
||
| 133 | * |
||
| 134 | * @return object|null |
||
| 135 | */ |
||
| 136 | public function last(): ?object |
||
| 137 | { |
||
| 138 | return $this->elements[count($this->elements) - 1] ?? null; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Create a new collection containing a merged array of the current collection's elements and the |
||
| 143 | * elements provided in $collection. |
||
| 144 | * |
||
| 145 | * @param CollectionInterface $collection |
||
| 146 | * |
||
| 147 | * @return CollectionInterface |
||
| 148 | */ |
||
| 149 | public function createMergedCollection(CollectionInterface $collection): CollectionInterface |
||
| 150 | { |
||
| 151 | $elements = array_merge($this->elements, $collection->getElements()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 152 | |||
| 153 | return new static($elements); |
||
| 154 | } |
||
| 155 | } |
||
| 156 |