Total Complexity | 13 |
Total Lines | 127 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
8 | abstract class SpecificClassCollection implements Countable, Iterator |
||
9 | { |
||
10 | private $collection; |
||
11 | private $current; |
||
12 | |||
13 | /** |
||
14 | * SpecificClassCollection constructor. |
||
15 | */ |
||
16 | 10 | public function __construct() |
|
17 | { |
||
18 | 10 | $this->collection = []; |
|
19 | 10 | $this->current = 0; |
|
20 | 10 | } |
|
21 | |||
22 | /** |
||
23 | * Get the number of elements inside |
||
24 | * the collection |
||
25 | * |
||
26 | * @return int |
||
27 | */ |
||
28 | 2 | public function count() |
|
29 | { |
||
30 | 2 | return count($this->collection); |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * Add an element to the collection |
||
35 | * |
||
36 | * @param mixed $element |
||
37 | * @return bool |
||
38 | * @throws InvalidClassException |
||
39 | */ |
||
40 | 8 | public function add($element) |
|
41 | { |
||
42 | 8 | $this->checkElementIsValid($element); |
|
43 | 7 | $this->collection[] = $element; |
|
44 | |||
45 | 7 | return true; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Check if an element can be added to |
||
50 | * the collection |
||
51 | * |
||
52 | * @param mixed $element |
||
53 | * @return bool |
||
54 | * @throws InvalidClassException |
||
55 | */ |
||
56 | 8 | private function checkElementIsValid($element) |
|
57 | { |
||
58 | 8 | $elementClass = get_class($element); |
|
59 | |||
60 | 8 | if ($this->getValidClassName() !== $elementClass && ! is_subclass_of($element, $this->getValidClassName())) { |
|
61 | 1 | throw new InvalidClassException($this->getValidClassName()); |
|
62 | } |
||
63 | |||
64 | 7 | return true; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * Get the FQDN of the class we |
||
69 | * want to store in the collection |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | abstract protected function getValidClassName(); |
||
74 | |||
75 | /** |
||
76 | * Empty the collection |
||
77 | */ |
||
78 | 1 | public function clear() |
|
79 | { |
||
80 | 1 | $this->collection = []; |
|
81 | 1 | $this->rewind(); |
|
82 | 1 | } |
|
83 | |||
84 | /** |
||
85 | * Return the array of elements |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | 1 | public function getElements() |
|
90 | { |
||
91 | 1 | return $this->collection; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @inheritdoc |
||
96 | * @return mixed |
||
97 | */ |
||
98 | 2 | public function current() |
|
99 | { |
||
100 | 2 | return $this->collection[$this->current]; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @inheritdoc |
||
105 | */ |
||
106 | 3 | public function next() |
|
109 | 3 | } |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | * @return int |
||
114 | */ |
||
115 | 3 | public function key() |
|
116 | { |
||
117 | 3 | return $this->current; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | * @return bool |
||
123 | */ |
||
124 | 3 | public function valid() |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * @inheritdoc |
||
131 | */ |
||
132 | 3 | public function rewind() |
|
135 | 3 | } |
|
136 | } |
||
137 |