1 | <?php |
||
18 | class EnumMap implements ArrayAccess, Countable, Iterator |
||
19 | { |
||
20 | /** |
||
21 | * The classname of the enumeration type |
||
22 | * @var string |
||
23 | */ |
||
24 | private $enumeration; |
||
25 | |||
26 | /** |
||
27 | * Internal map of ordinal number and value |
||
28 | * @var array |
||
29 | */ |
||
30 | private $map = []; |
||
31 | |||
32 | /** |
||
33 | * List of ordinal numbers |
||
34 | * @var int[] |
||
35 | */ |
||
36 | private $ordinals = []; |
||
37 | |||
38 | /** |
||
39 | * Current iterator position |
||
40 | * @var int |
||
41 | */ |
||
42 | private $pos = 0; |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * @param string $enumeration The classname of the enumeration type |
||
47 | * @throws InvalidArgumentException |
||
48 | */ |
||
49 | 18 | public function __construct($enumeration) |
|
59 | |||
60 | /** |
||
61 | * Get the classname of the enumeration |
||
62 | * @return string |
||
63 | */ |
||
64 | 2 | public function getEnumeration() |
|
68 | |||
69 | /** |
||
70 | * Attach a new enumerator or overwrite an existing one |
||
71 | * @param Enum|null|boolean|int|float|string $enumerator |
||
72 | * @param mixed $data |
||
73 | * @return void |
||
74 | * @throws InvalidArgumentException On an invalid given enumerator |
||
75 | */ |
||
76 | 6 | public function attach($enumerator, $data = null) |
|
80 | |||
81 | /** |
||
82 | * Test if the given enumerator exists |
||
83 | * @param Enum|null|boolean|int|float|string $enumerator |
||
84 | * @return boolean |
||
85 | */ |
||
86 | 6 | public function contains($enumerator) |
|
90 | |||
91 | /** |
||
92 | * Detach an enumerator |
||
93 | * @param Enum|null|boolean|int|float|string $enumerator |
||
94 | * @return void |
||
95 | * @throws InvalidArgumentException On an invalid given enumerator |
||
96 | */ |
||
97 | 4 | public function detach($enumerator) |
|
101 | |||
102 | /** |
||
103 | * Test if the given enumerator exists |
||
104 | * @param Enum|null|boolean|int|float|string $enumerator |
||
105 | * @return boolean |
||
106 | * @see contains() |
||
107 | */ |
||
108 | 12 | public function offsetExists($enumerator) |
|
119 | |||
120 | /** |
||
121 | * Get mapped data for the given enumerator |
||
122 | * @param Enum|null|boolean|int|float|string $enumerator |
||
123 | * @return mixed |
||
124 | * @throws InvalidArgumentException On an invalid given enumerator |
||
125 | */ |
||
126 | 8 | public function offsetGet($enumerator) |
|
139 | |||
140 | /** |
||
141 | * Attach a new enumerator or overwrite an existing one |
||
142 | * @param Enum|null|boolean|int|float|string $enumerator |
||
143 | * @param mixed $data |
||
144 | * @return void |
||
145 | * @throws InvalidArgumentException On an invalid given enumerator |
||
146 | * @see attach() |
||
147 | */ |
||
148 | 14 | public function offsetSet($enumerator, $data = null) |
|
158 | |||
159 | /** |
||
160 | * Detach an existing enumerator |
||
161 | * @param Enum|null|boolean|int|float|string $enumerator |
||
162 | * @return void |
||
163 | * @throws InvalidArgumentException On an invalid given enumerator |
||
164 | * @see detach() |
||
165 | */ |
||
166 | 8 | public function offsetUnset($enumerator) |
|
176 | |||
177 | /** |
||
178 | * Get the current value |
||
179 | * @return mixed |
||
180 | */ |
||
181 | 2 | public function current() |
|
189 | |||
190 | /** |
||
191 | * Get the current key |
||
192 | * @return Enum|null |
||
193 | */ |
||
194 | 4 | public function key() |
|
203 | |||
204 | /** |
||
205 | * Reset the iterator position to zero. |
||
206 | * @return void |
||
207 | */ |
||
208 | 2 | public function rewind() |
|
212 | |||
213 | /** |
||
214 | * Increment the iterator position by one. |
||
215 | * @return void |
||
216 | */ |
||
217 | 2 | public function next() |
|
221 | |||
222 | /** |
||
223 | * Test if the iterator is in a valid state |
||
224 | * @return boolean |
||
225 | */ |
||
226 | 2 | public function valid() |
|
230 | |||
231 | /** |
||
232 | * Count the number of elements |
||
233 | * |
||
234 | * @return int |
||
235 | */ |
||
236 | 2 | public function count() |
|
240 | } |
||
241 |