Total Complexity | 17 |
Total Lines | 99 |
Duplicated Lines | 0 % |
Changes | 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 | /** |
||
25 | * @var \Spatie\Typed\Type|array $type |
||
26 | */ |
||
27 | public function __construct($type) |
||
28 | { |
||
29 | if ($type instanceof Type) { |
||
30 | $this->type = $type; |
||
31 | |||
32 | return; |
||
33 | } |
||
34 | |||
35 | $firstValue = reset($type); |
||
36 | |||
37 | $this->type = T::infer($firstValue); |
||
38 | |||
39 | $this->set($type); |
||
40 | } |
||
41 | |||
42 | public function set(array $data): Collection |
||
43 | { |
||
44 | foreach ($data as $item) { |
||
45 | $this[] = $item; |
||
46 | } |
||
47 | |||
48 | return $this; |
||
49 | } |
||
50 | |||
51 | public function current() |
||
52 | { |
||
53 | return $this->data[$this->position]; |
||
54 | } |
||
55 | |||
56 | public function offsetGet($offset) |
||
57 | { |
||
58 | return isset($this->data[$offset]) ? $this->data[$offset] : null; |
||
59 | } |
||
60 | |||
61 | public function offsetSet($offset, $value) |
||
62 | { |
||
63 | $value = $this->validateType($this->type, $value); |
||
64 | |||
65 | if (is_null($offset)) { |
||
66 | $this->data[] = $value; |
||
67 | } else { |
||
68 | $this->data[$offset] = $value; |
||
69 | } |
||
70 | } |
||
71 | |||
72 | public function offsetExists($offset) |
||
75 | } |
||
76 | |||
77 | public function offsetUnset($offset) |
||
80 | } |
||
81 | |||
82 | public function next() |
||
83 | { |
||
84 | $this->position++; |
||
85 | } |
||
86 | |||
87 | public function key() |
||
88 | { |
||
89 | return $this->position; |
||
90 | } |
||
91 | |||
92 | public function valid() |
||
93 | { |
||
94 | return array_key_exists($this->position, $this->data); |
||
95 | } |
||
96 | |||
97 | public function rewind() |
||
98 | { |
||
99 | $this->position = 0; |
||
100 | } |
||
101 | |||
102 | public function toArray(): array |
||
105 | } |
||
106 | |||
107 | public function count(): int |
||
108 | { |
||
109 | return count($this->data); |
||
110 | } |
||
111 | } |
||
112 |