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