| @@ 10-85 (lines=76) @@ | ||
| 7 | use Illuminate\Contracts\Support\Arrayable; |
|
| 8 | use Iterator; |
|
| 9 | ||
| 10 | abstract class ValueObjectCollection implements |
|
| 11 | ArrayAccess, |
|
| 12 | Iterator, |
|
| 13 | Countable, |
|
| 14 | Arrayable |
|
| 15 | { |
|
| 16 | /** @var array */ |
|
| 17 | protected $collection; |
|
| 18 | ||
| 19 | /** @var int */ |
|
| 20 | protected $position = 0; |
|
| 21 | ||
| 22 | public function __construct(array $collection = []) |
|
| 23 | { |
|
| 24 | $this->collection = $collection; |
|
| 25 | } |
|
| 26 | ||
| 27 | public function current() |
|
| 28 | { |
|
| 29 | return $this->collection[$this->position]; |
|
| 30 | } |
|
| 31 | ||
| 32 | public function offsetGet($offset) |
|
| 33 | { |
|
| 34 | return isset($this->collection[$offset]) ? $this->collection[$offset] : null; |
|
| 35 | } |
|
| 36 | ||
| 37 | public function offsetSet($offset, $value) |
|
| 38 | { |
|
| 39 | if (is_null($offset)) { |
|
| 40 | $this->collection[] = $value; |
|
| 41 | } else { |
|
| 42 | $this->collection[$offset] = $value; |
|
| 43 | } |
|
| 44 | } |
|
| 45 | ||
| 46 | public function offsetExists($offset) |
|
| 47 | { |
|
| 48 | return array_key_exists($offset, $this->collection); |
|
| 49 | } |
|
| 50 | ||
| 51 | public function offsetUnset($offset) |
|
| 52 | { |
|
| 53 | unset($this->collection[$offset]); |
|
| 54 | } |
|
| 55 | ||
| 56 | public function next() |
|
| 57 | { |
|
| 58 | $this->position++; |
|
| 59 | } |
|
| 60 | ||
| 61 | public function key() |
|
| 62 | { |
|
| 63 | return $this->position; |
|
| 64 | } |
|
| 65 | ||
| 66 | public function valid() |
|
| 67 | { |
|
| 68 | return array_key_exists($this->position, $this->collection); |
|
| 69 | } |
|
| 70 | ||
| 71 | public function rewind() |
|
| 72 | { |
|
| 73 | $this->position = 0; |
|
| 74 | } |
|
| 75 | ||
| 76 | public function toArray(): array |
|
| 77 | { |
|
| 78 | return $this->collection; |
|
| 79 | } |
|
| 80 | ||
| 81 | public function count(): int |
|
| 82 | { |
|
| 83 | return count($this->collection); |
|
| 84 | } |
|
| 85 | } |
|
| 86 | ||
| @@ 9-83 (lines=75) @@ | ||
| 6 | use Countable; |
|
| 7 | use ArrayAccess; |
|
| 8 | ||
| 9 | abstract class ValueObjectList implements |
|
| 10 | ArrayAccess, |
|
| 11 | Iterator, |
|
| 12 | Countable |
|
| 13 | { |
|
| 14 | /** @var array */ |
|
| 15 | protected $list; |
|
| 16 | ||
| 17 | /** @var int */ |
|
| 18 | protected $position = 0; |
|
| 19 | ||
| 20 | public function __construct(array $collection = []) |
|
| 21 | { |
|
| 22 | $this->list = $collection; |
|
| 23 | } |
|
| 24 | ||
| 25 | public function current() |
|
| 26 | { |
|
| 27 | return $this->list[$this->position]; |
|
| 28 | } |
|
| 29 | ||
| 30 | public function offsetGet($offset) |
|
| 31 | { |
|
| 32 | return isset($this->list[$offset]) ? $this->list[$offset] : null; |
|
| 33 | } |
|
| 34 | ||
| 35 | public function offsetSet($offset, $value) |
|
| 36 | { |
|
| 37 | if (is_null($offset)) { |
|
| 38 | $this->list[] = $value; |
|
| 39 | } else { |
|
| 40 | $this->list[$offset] = $value; |
|
| 41 | } |
|
| 42 | } |
|
| 43 | ||
| 44 | public function offsetExists($offset) |
|
| 45 | { |
|
| 46 | return array_key_exists($offset, $this->list); |
|
| 47 | } |
|
| 48 | ||
| 49 | public function offsetUnset($offset) |
|
| 50 | { |
|
| 51 | unset($this->list[$offset]); |
|
| 52 | } |
|
| 53 | ||
| 54 | public function next() |
|
| 55 | { |
|
| 56 | $this->position++; |
|
| 57 | } |
|
| 58 | ||
| 59 | public function key() |
|
| 60 | { |
|
| 61 | return $this->position; |
|
| 62 | } |
|
| 63 | ||
| 64 | public function valid() |
|
| 65 | { |
|
| 66 | return array_key_exists($this->position, $this->list); |
|
| 67 | } |
|
| 68 | ||
| 69 | public function rewind() |
|
| 70 | { |
|
| 71 | $this->position = 0; |
|
| 72 | } |
|
| 73 | ||
| 74 | public function toArray(): array |
|
| 75 | { |
|
| 76 | return $this->list; |
|
| 77 | } |
|
| 78 | ||
| 79 | public function count(): int |
|
| 80 | { |
|
| 81 | return count($this->list); |
|
| 82 | } |
|
| 83 | } |
|
| 84 | ||