| Total Complexity | 31 |
| Total Lines | 317 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | class EnumMap implements ArrayAccess, Countable, IteratorAggregate |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The classname of the enumeration type |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $enumeration; |
||
|
1 ignored issue
–
show
|
|||
| 28 | |||
| 29 | /** |
||
| 30 | * Internal map of ordinal number and data value |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | private $map = []; |
||
|
1 ignored issue
–
show
|
|||
| 34 | |||
| 35 | /** |
||
| 36 | * Constructor |
||
| 37 | * @param string $enumeration The classname of the enumeration type |
||
|
1 ignored issue
–
show
|
|||
| 38 | * @param null|iterable $map Initialize map |
||
|
1 ignored issue
–
show
|
|||
| 39 | * @throws InvalidArgumentException |
||
| 40 | */ |
||
| 41 | 18 | public function __construct(string $enumeration, iterable $map = null) |
|
|
2 ignored issues
–
show
|
|||
| 42 | { |
||
| 43 | 18 | if (!\is_subclass_of($enumeration, Enum::class)) { |
|
| 44 | 1 | throw new InvalidArgumentException(\sprintf( |
|
| 45 | 1 | '%s can handle subclasses of %s only', |
|
| 46 | 1 | __CLASS__, |
|
| 47 | 1 | Enum::class |
|
| 48 | )); |
||
| 49 | } |
||
| 50 | 17 | $this->enumeration = $enumeration; |
|
| 51 | |||
| 52 | 17 | if ($map) { |
|
| 53 | 1 | $this->addIterable($map); |
|
| 54 | } |
||
| 55 | 17 | } |
|
| 56 | |||
| 57 | /* write access (mutable) */ |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Adds the given enumerator (object or value) mapping to the specified data value. |
||
| 61 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 62 | * @param mixed $value |
||
| 63 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 64 | * @see offsetSet() |
||
| 65 | */ |
||
| 66 | 11 | public function add($enumerator, $value): void |
|
| 70 | 10 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Adds the given iterable, mapping enumerators (objects or values) to data values. |
||
| 74 | * @param iterable $map |
||
| 75 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 76 | */ |
||
| 77 | 3 | public function addIterable(iterable $map): void |
|
| 78 | { |
||
| 79 | 3 | $innerMap = $this->map; |
|
| 80 | 3 | foreach ($map as $enumerator => $value) { |
|
| 81 | 3 | $ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
|
| 82 | 3 | $innerMap[$ord] = $value; |
|
| 83 | } |
||
| 84 | 3 | $this->map = $innerMap; |
|
| 85 | 3 | } |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Removes the given enumerator (object or value) mapping. |
||
| 89 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 90 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 91 | * @see offsetUnset() |
||
| 92 | */ |
||
| 93 | 5 | public function remove($enumerator): void |
|
| 94 | { |
||
| 95 | 5 | $ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
|
| 96 | 5 | unset($this->map[$ord]); |
|
| 97 | 5 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Removes the given iterable enumerator (object or value) mappings. |
||
| 101 | * @param iterable $enumerators |
||
| 102 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 103 | */ |
||
| 104 | 2 | public function removeIterable(iterable $enumerators): void |
|
| 105 | { |
||
| 106 | 2 | $map = $this->map; |
|
| 107 | 2 | foreach ($enumerators as $enumerator) { |
|
| 108 | 2 | $ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
|
| 109 | 2 | unset($map[$ord]); |
|
| 110 | } |
||
| 111 | |||
| 112 | 2 | $this->map = $map; |
|
| 113 | 2 | } |
|
| 114 | |||
| 115 | /* write access (immutable) */ |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Creates a new map with the given enumerator (object or value) mapping to the specified data value added. |
||
|
1 ignored issue
–
show
|
|||
| 119 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 120 | * @param mixed $value |
||
| 121 | * @return static |
||
| 122 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 123 | */ |
||
| 124 | 1 | public function with($enumerator, $value): self |
|
| 125 | { |
||
| 126 | 1 | $clone = clone $this; |
|
| 127 | 1 | $clone->add($enumerator, $value); |
|
| 128 | 1 | return $clone; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Creates a new map with the given iterable mapping enumerators (objects or values) to data values added. |
||
|
1 ignored issue
–
show
|
|||
| 133 | * @param iterable $map |
||
| 134 | * @return static |
||
| 135 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 136 | */ |
||
| 137 | 1 | public function withIterable(iterable $map): self |
|
| 138 | { |
||
| 139 | 1 | $clone = clone $this; |
|
| 140 | 1 | $clone->addIterable($map); |
|
| 141 | 1 | return $clone; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Create a new map with the given enumerator mapping removed. |
||
| 146 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 147 | * @return static |
||
| 148 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 149 | */ |
||
| 150 | 1 | public function without($enumerator): self |
|
| 151 | { |
||
| 152 | 1 | $clone = clone $this; |
|
| 153 | 1 | $clone->remove($enumerator); |
|
| 154 | 1 | return $clone; |
|
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Creates a new map with the given iterable enumerator (object or value) mappings removed. |
||
| 159 | * @param iterable $enumerators |
||
| 160 | * @return static |
||
| 161 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 162 | */ |
||
| 163 | 1 | public function withoutIterable(iterable $enumerators): self |
|
| 168 | } |
||
| 169 | |||
| 170 | /* read access */ |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Get the classname of the enumeration type. |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 1 | public function getEnumeration(): string |
|
| 177 | { |
||
| 178 | 1 | return $this->enumeration; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the mapped data value of the given enumerator (object or value). |
||
| 183 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 184 | * @return mixed |
||
| 185 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 186 | * @throws UnexpectedValueException If the given enumerator does not exist in this map |
||
| 187 | * @see offsetGet() |
||
| 188 | */ |
||
| 189 | 10 | public function get($enumerator) |
|
| 190 | { |
||
| 191 | 10 | $enumerator = ($this->enumeration)::get($enumerator); |
|
| 192 | 10 | $ord = $enumerator->getOrdinal(); |
|
| 193 | 10 | if (!\array_key_exists($ord, $this->map)) { |
|
| 194 | 2 | throw new UnexpectedValueException(sprintf( |
|
| 195 | 2 | 'Enumerator %s could not be found', |
|
| 196 | 2 | \var_export($enumerator->getValue(), true) |
|
| 197 | )); |
||
| 198 | } |
||
| 199 | |||
| 200 | 8 | return $this->map[$ord]; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get a list of enumerator keys. |
||
| 205 | * @return Enum[] |
||
| 206 | */ |
||
| 207 | 7 | public function getKeys(): array |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get a list of mapped data values. |
||
| 214 | * @return mixed[] |
||
| 215 | */ |
||
| 216 | 7 | public function getValues(): array |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Search for the given data value. |
||
| 223 | * @param mixed $value |
||
| 224 | * @param bool $strict Use strict type comparison |
||
|
2 ignored issues
–
show
|
|||
| 225 | * @return Enum|null The enumerator object of the first matching data value or NULL |
||
| 226 | */ |
||
| 227 | 2 | public function search($value, bool $strict = false) |
|
|
2 ignored issues
–
show
|
|||
| 228 | { |
||
| 229 | 2 | $ord = \array_search($value, $this->map, $strict); |
|
| 230 | 2 | if ($ord !== false) { |
|
| 231 | 2 | return ($this->enumeration)::byOrdinal($ord); |
|
| 232 | } |
||
| 233 | |||
| 234 | 2 | return null; |
|
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Test if the given enumerator key (object or value) exists. |
||
| 239 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 240 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 241 | * @see offsetExists |
||
| 242 | */ |
||
| 243 | 7 | public function contains($enumerator): bool |
|
| 251 | } |
||
| 252 | } |
||
| 253 | |||
| 254 | /* ArrayAccess */ |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Test if the given enumerator key (object or value) exists and is not NULL |
||
| 258 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 259 | * @return bool |
||
|
1 ignored issue
–
show
|
|||
| 260 | * @see contains |
||
| 261 | */ |
||
| 262 | 5 | public function offsetExists($enumerator): bool |
|
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the mapped data value of the given enumerator (object or value). |
||
| 274 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 275 | * @return mixed The mapped date value of the given enumerator or NULL |
||
| 276 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 277 | * @see get() |
||
| 278 | */ |
||
| 279 | 4 | public function offsetGet($enumerator) |
|
| 280 | { |
||
| 281 | try { |
||
| 282 | 4 | return $this->get($enumerator); |
|
| 283 | 1 | } catch (UnexpectedValueException $e) { |
|
| 284 | 1 | return null; |
|
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds the given enumerator (object or value) mapping to the specified data value. |
||
| 290 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 291 | * @param mixed $value |
||
| 292 | * @return void |
||
| 293 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 294 | * @see add() |
||
| 295 | */ |
||
| 296 | 7 | public function offsetSet($enumerator, $value = null): void |
|
|
2 ignored issues
–
show
|
|||
| 297 | { |
||
| 298 | 7 | $this->add($enumerator, $value); |
|
| 299 | 6 | } |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Removes the given enumerator (object or value) mapping. |
||
| 303 | * @param Enum|null|bool|int|float|string|array $enumerator |
||
|
1 ignored issue
–
show
|
|||
| 304 | * @return void |
||
| 305 | * @throws InvalidArgumentException On an invalid given enumerator |
||
| 306 | * @see remove() |
||
| 307 | */ |
||
| 308 | 2 | public function offsetUnset($enumerator): void |
|
| 309 | { |
||
| 310 | 2 | $this->remove($enumerator); |
|
| 311 | 2 | } |
|
| 312 | |||
| 313 | /* IteratorAggregate */ |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get a new Iterator. |
||
| 317 | * |
||
| 318 | * @return Iterator Iterator<K extends Enum, V> |
||
| 319 | */ |
||
| 320 | 2 | public function getIterator(): Iterator |
|
| 325 | } |
||
| 326 | 2 | } |
|
| 327 | |||
| 328 | /* Countable */ |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Count the number of elements |
||
| 332 | * |
||
| 333 | * @return int |
||
|
1 ignored issue
–
show
|
|||
| 334 | */ |
||
| 335 | 3 | public function count(): int |
|
| 338 | } |
||
| 339 | } |
||
| 340 |