Total Complexity | 19 |
Total Lines | 176 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
24 | class EnumMap implements ArrayAccess, Countable, IteratorAggregate |
||
25 | { |
||
26 | /** |
||
27 | * The classname of the enumeration type |
||
28 | * @var string |
||
29 | */ |
||
30 | private $enumeration; |
||
1 ignored issue
–
show
|
|||
31 | |||
32 | /** |
||
33 | * Internal map of ordinal number and value |
||
34 | * @var array |
||
35 | */ |
||
36 | private $map = []; |
||
1 ignored issue
–
show
|
|||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * @param string $enumeration The classname of the enumeration type |
||
1 ignored issue
–
show
|
|||
41 | * @throws InvalidArgumentException |
||
42 | */ |
||
43 | 14 | public function __construct(string $enumeration) |
|
44 | { |
||
45 | 14 | if (!\is_subclass_of($enumeration, Enum::class)) { |
|
46 | 1 | throw new InvalidArgumentException(\sprintf( |
|
47 | 1 | '%s can handle subclasses of %s only', |
|
48 | 1 | __CLASS__, |
|
49 | 1 | Enum::class |
|
50 | )); |
||
51 | } |
||
52 | 13 | $this->enumeration = $enumeration; |
|
53 | 13 | } |
|
54 | |||
55 | /** |
||
56 | * Get the classname of the enumeration |
||
57 | * @return string |
||
58 | */ |
||
59 | 1 | public function getEnumeration(): string |
|
60 | { |
||
61 | 1 | return $this->enumeration; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get a list of map keys |
||
66 | * @return Enum[] |
||
67 | */ |
||
68 | 3 | public function getKeys(): array |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Get a list of map values |
||
75 | * @return mixed[] |
||
76 | */ |
||
77 | 3 | public function getValues(): array |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Search for the given value |
||
84 | * @param mixed $value |
||
85 | * @param bool $strict Use strict type comparison |
||
2 ignored issues
–
show
|
|||
86 | * @return Enum|null The found key or NULL |
||
87 | */ |
||
88 | 2 | public function search($value, bool $strict = false) |
|
2 ignored issues
–
show
|
|||
89 | { |
||
90 | 2 | $ord = \array_search($value, $this->map, $strict); |
|
91 | 2 | if ($ord !== false) { |
|
92 | 2 | return ($this->enumeration)::byOrdinal($ord); |
|
93 | } |
||
94 | |||
95 | 2 | return null; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Test if the given enumerator exists |
||
100 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
1 ignored issue
–
show
|
|||
101 | * @return bool |
||
1 ignored issue
–
show
|
|||
102 | * @see offsetExists |
||
103 | */ |
||
104 | 4 | public function contains($enumerator): bool |
|
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Test if the given enumerator key exists and is not NULL |
||
117 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
1 ignored issue
–
show
|
|||
118 | * @return bool |
||
1 ignored issue
–
show
|
|||
119 | * @see contains |
||
120 | */ |
||
121 | 5 | public function offsetExists($enumerator): bool |
|
128 | } |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Get mapped data for the given enumerator |
||
133 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
1 ignored issue
–
show
|
|||
134 | * @return mixed |
||
135 | * @throws InvalidArgumentException On an invalid given enumerator |
||
136 | * @throws UnexpectedValueException If the given enumerator does not exist in this map |
||
137 | */ |
||
138 | 7 | public function offsetGet($enumerator) |
|
139 | { |
||
140 | 7 | $enumerator = ($this->enumeration)::get($enumerator); |
|
141 | 7 | $ord = $enumerator->getOrdinal(); |
|
142 | 7 | if (!isset($this->map[$ord]) && !\array_key_exists($ord, $this->map)) { |
|
143 | 2 | throw new UnexpectedValueException(sprintf( |
|
144 | 2 | 'Enumerator %s could not be found', |
|
145 | 2 | \var_export($enumerator->getValue(), true) |
|
146 | )); |
||
147 | } |
||
148 | |||
149 | 5 | return $this->map[$ord]; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Attach a new enumerator or overwrite an existing one |
||
154 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
1 ignored issue
–
show
|
|||
155 | * @param mixed $value |
||
156 | * @return void |
||
157 | * @throws InvalidArgumentException On an invalid given enumerator |
||
158 | * @see attach() |
||
159 | */ |
||
160 | 10 | public function offsetSet($enumerator, $value = null): void |
|
2 ignored issues
–
show
|
|||
161 | { |
||
162 | 10 | $ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
|
163 | 9 | $this->map[$ord] = $value; |
|
164 | 9 | } |
|
165 | |||
166 | /** |
||
167 | * Detach an existing enumerator |
||
168 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
1 ignored issue
–
show
|
|||
169 | * @return void |
||
170 | * @throws InvalidArgumentException On an invalid given enumerator |
||
171 | * @see detach() |
||
172 | */ |
||
173 | 4 | public function offsetUnset($enumerator): void |
|
174 | { |
||
175 | 4 | $ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
|
176 | 4 | unset($this->map[$ord]); |
|
177 | 4 | } |
|
178 | |||
179 | /** |
||
180 | * Get a new Iterator. |
||
181 | * |
||
182 | * @return Iterator |
||
183 | */ |
||
184 | 2 | public function getIterator(): Iterator |
|
189 | } |
||
190 | 2 | } |
|
191 | |||
192 | /** |
||
193 | * Count the number of elements |
||
194 | * |
||
195 | * @return int |
||
1 ignored issue
–
show
|
|||
196 | */ |
||
197 | 2 | public function count(): int |
|
200 | } |
||
201 | } |
||
202 |