Total Complexity | 16 |
Total Lines | 88 |
Duplicated Lines | 0 % |
Coverage | 87.8% |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
5 | abstract class AbstractCollection implements \Countable, \ArrayAccess, \Iterator |
||
6 | { |
||
7 | protected array $items; |
||
8 | protected int $position; |
||
9 | |||
10 | 6 | protected function __construct() |
|
11 | { |
||
12 | 6 | $this->items = []; |
|
13 | 6 | $this->position = 0; |
|
14 | 6 | } |
|
15 | |||
16 | 6 | public static function create() |
|
17 | { |
||
18 | 6 | return new static(); |
|
19 | } |
||
20 | |||
21 | 2 | public function items(): array |
|
22 | { |
||
23 | 2 | return $this->items; |
|
24 | } |
||
25 | |||
26 | 2 | public function count() |
|
27 | { |
||
28 | 2 | return count($this->items); |
|
29 | } |
||
30 | |||
31 | public function offsetExists($offset) |
||
32 | { |
||
33 | return isset($this->items[$offset]); |
||
34 | } |
||
35 | |||
36 | 4 | public function offsetGet($offset) |
|
37 | { |
||
38 | 4 | return $this->items[$offset]; |
|
39 | } |
||
40 | |||
41 | 2 | public function offsetSet($offset, $value) |
|
42 | { |
||
43 | 2 | $this->checkValueIsValidForCollection($value); |
|
44 | 2 | $this->items[$offset] = $value; |
|
45 | 2 | } |
|
46 | |||
47 | public function offsetUnset($offset) |
||
50 | } |
||
51 | |||
52 | 6 | private function checkValueIsValidForCollection($value): void |
|
53 | { |
||
54 | 6 | if (get_class($value) != $this->itemClass()) { |
|
55 | 2 | throw new \InvalidArgumentException("Provided value is not a valid " . $this->itemClass()); |
|
56 | } |
||
57 | 4 | } |
|
58 | |||
59 | 2 | public function current() |
|
60 | { |
||
61 | 2 | return $this->items[$this->position]; |
|
62 | } |
||
63 | |||
64 | 2 | public function next() |
|
65 | { |
||
66 | 2 | $this->position++; |
|
67 | 2 | } |
|
68 | |||
69 | 2 | public function key() |
|
70 | { |
||
71 | 2 | return $this->position; |
|
72 | } |
||
73 | |||
74 | 2 | public function valid() |
|
75 | { |
||
76 | 2 | return isset($this->items[$this->position]); |
|
77 | } |
||
78 | |||
79 | 2 | public function rewind() |
|
80 | { |
||
81 | 2 | $this->position = 0; |
|
82 | 2 | } |
|
83 | |||
84 | 4 | public function add($item) { |
|
90 | } |
||
91 | |||
92 | protected abstract function itemClass(): string; |
||
93 | } |
||
94 |