| Total Complexity | 17 |
| Total Lines | 96 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 11 | class Collection implements ArrayAccess, Iterator, Countable |
||
| 12 | { |
||
| 13 | use ValidatesType; |
||
| 14 | |||
| 15 | /** @var \Spatie\Typed\Type */ |
||
| 16 | private $type; |
||
| 17 | |||
| 18 | /** @var array */ |
||
| 19 | protected $data = []; |
||
| 20 | |||
| 21 | /** @var int */ |
||
| 22 | private $position = 0; |
||
| 23 | |||
| 24 | public function __construct($type) |
||
| 25 | { |
||
| 26 | if ($type instanceof Type) { |
||
| 27 | $this->type = $type; |
||
| 28 | |||
| 29 | return; |
||
| 30 | } |
||
| 31 | |||
| 32 | $firstValue = reset($type); |
||
| 33 | |||
| 34 | $this->type = T::infer($firstValue); |
||
| 35 | |||
| 36 | $this->set($type); |
||
| 37 | } |
||
| 38 | |||
| 39 | public function set(array $data): self |
||
| 40 | { |
||
| 41 | foreach ($data as $item) { |
||
| 42 | $this[] = $item; |
||
| 43 | } |
||
| 44 | |||
| 45 | return $this; |
||
| 46 | } |
||
| 47 | |||
| 48 | public function current() |
||
| 49 | { |
||
| 50 | return $this->data[$this->position]; |
||
| 51 | } |
||
| 52 | |||
| 53 | public function offsetGet($offset) |
||
| 54 | { |
||
| 55 | return isset($this->data[$offset]) ? $this->data[$offset] : null; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function offsetSet($offset, $value) |
||
| 59 | { |
||
| 60 | $value = $this->validateType($this->type, $value); |
||
| 61 | |||
| 62 | if (is_null($offset)) { |
||
| 63 | $this->data[] = $value; |
||
| 64 | } else { |
||
| 65 | $this->data[$offset] = $value; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | public function offsetExists($offset) |
||
| 72 | } |
||
| 73 | |||
| 74 | public function offsetUnset($offset) |
||
| 77 | } |
||
| 78 | |||
| 79 | public function next() |
||
| 80 | { |
||
| 81 | $this->position++; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function key() |
||
| 85 | { |
||
| 86 | return $this->position; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function valid() |
||
| 90 | { |
||
| 91 | return array_key_exists($this->position, $this->data); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function rewind() |
||
| 95 | { |
||
| 96 | $this->position = 0; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function toArray(): array |
||
| 102 | } |
||
| 103 | |||
| 104 | public function count(): int |
||
| 105 | { |
||
| 106 | return count($this->data); |
||
| 107 | } |
||
| 108 | } |
||
| 109 |