|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Toolkit\Stdlib\Std; |
| 4 | + |
| 5 | +use BadMethodCallException; |
| 6 | +use JsonSerializable; |
| 7 | +use ReflectionClass; |
| 8 | +use UnexpectedValueException; |
| 9 | +use function array_key_exists; |
| 10 | +use function array_keys; |
| 11 | +use function array_search; |
| 12 | +use function get_class; |
| 13 | +use function in_array; |
| 14 | + |
| 15 | +/** |
| 16 | + * class BaseEnum |
| 17 | + * |
| 18 | + * @author inhere |
| 19 | + * @template T |
| 20 | + * @link https://github.com/Elao/PhpEnums |
| 21 | + * @link https://github.com/myclabs/php-enum/blob/master/src/Enum.php |
| 22 | + */ |
| 23 | +abstract class BaseEnum implements JsonSerializable |
| 24 | +{ |
| 25 | + |
| 26 | + /** |
| 27 | + * Store existing constants in a static cache per object. |
| 28 | + * |
| 29 | + * @var array<class-string, array<string, mixed>> |
| 30 | + */ |
| 31 | + protected static array $caches = []; |
| 32 | + |
| 33 | + /** |
| 34 | + * Cache of instances of the Enum class |
| 35 | + * |
| 36 | + * @var array<class-string, array<string, static>> |
| 37 | + */ |
| 38 | + protected static array $instances = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * @param string $name |
| 42 | + * @param array $args |
| 43 | + * |
| 44 | + * @return mixed |
| 45 | + */ |
| 46 | + public static function __callStatic(string $name, array $args): mixed |
| 47 | + { |
| 48 | + $class = static::class; |
| 49 | + |
| 50 | + if (!isset(self::$instances[$class][$name])) { |
| 51 | + $array = static::toArray(); |
| 52 | + if (!isset($array[$name]) && !array_key_exists($name, $array)) { |
| 53 | + throw new BadMethodCallException("No static method or enum constant '$name' in class $class"); |
| 54 | + } |
| 55 | + |
| 56 | + return self::$instances[$class][$name] = new static($array[$name]); |
| 57 | + } |
| 58 | + |
| 59 | + return clone self::$instances[$class][$name]; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param mixed $variable |
| 64 | + * |
| 65 | + * @return bool |
| 66 | + */ |
| 67 | + final public function equals(mixed $variable = null): bool |
| 68 | + { |
| 69 | + return $variable instanceof self |
| 70 | + && $this->getValue() === $variable->getValue() |
| 71 | + && static::class === get_class($variable); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns all possible values as an array |
| 76 | + * |
| 77 | + * @psalm-suppress ImpureStaticProperty |
| 78 | + * |
| 79 | + * @return array<string, mixed> Constant name in key, constant value in value |
| 80 | + */ |
| 81 | + public static function toArray(): array |
| 82 | + { |
| 83 | + $class = static::class; |
| 84 | + |
| 85 | + if (!isset(static::$caches[$class])) { |
| 86 | + /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ |
| 87 | + /** @noinspection PhpUnhandledExceptionInspection */ |
| 88 | + $reflection = new ReflectionClass($class); |
| 89 | + |
| 90 | + /** @psalm-suppress ImpureMethodCall this reflection API usage has no side-effects here */ |
| 91 | + static::$caches[$class] = $reflection->getConstants(); |
| 92 | + } |
| 93 | + |
| 94 | + return static::$caches[$class]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Returns the names (keys) of all constants in the Enum class |
| 99 | + * |
| 100 | + * @return list<string> |
| 101 | + */ |
| 102 | + public static function keys(): array |
| 103 | + { |
| 104 | + return array_keys(static::toArray()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Returns instances of the Enum class of all Enum constants |
| 109 | + * |
| 110 | + * @return array<string, static> Constant name in key, Enum instance in value |
| 111 | + */ |
| 112 | + public static function values(): array |
| 113 | + { |
| 114 | + $values = []; |
| 115 | + |
| 116 | + /** @psalm-var T $value */ |
| 117 | + foreach (static::toArray() as $key => $value) { |
| 118 | + $values[$key] = new static($value); |
| 119 | + } |
| 120 | + |
| 121 | + return $values; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Return key for value |
| 126 | + * |
| 127 | + * @param mixed $value |
| 128 | + * |
| 129 | + * @return string|false |
| 130 | + */ |
| 131 | + public static function search(mixed $value): bool|string |
| 132 | + { |
| 133 | + return array_search($value, static::toArray(), true); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Check if is valid enum value |
| 138 | + * |
| 139 | + * @param T $value |
| 140 | + * @return bool |
| 141 | + */ |
| 142 | + public static function isValid(mixed $value): bool |
| 143 | + { |
| 144 | + return in_array($value, static::toArray(), true); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Asserts valid enum value |
| 149 | + * |
| 150 | + * @param T $value |
| 151 | + */ |
| 152 | + public static function assertValidValue(mixed $value): void |
| 153 | + { |
| 154 | + self::mustGetKeyByValue($value); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * @param mixed $value |
| 159 | + * |
| 160 | + * @return string |
| 161 | + */ |
| 162 | + protected static function mustGetKeyByValue(mixed $value): string |
| 163 | + { |
| 164 | + if (false === ($key = static::search($value))) { |
| 165 | + throw new UnexpectedValueException("Value '$value' is not item of the enum " . static::class); |
| 166 | + } |
| 167 | + |
| 168 | + return $key; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Enum key, the constant name |
| 173 | + * |
| 174 | + * @var string |
| 175 | + */ |
| 176 | + private string $key; |
| 177 | + |
| 178 | + /** |
| 179 | + * Enum value |
| 180 | + * |
| 181 | + * @var T |
| 182 | + */ |
| 183 | + protected mixed $value; |
| 184 | + |
| 185 | + /** |
| 186 | + * Creates a new value of some type |
| 187 | + * |
| 188 | + * @psalm-pure |
| 189 | + * @param mixed $value |
| 190 | + * |
| 191 | + * @psalm-param T $value |
| 192 | + * @throws UnexpectedValueException if incompatible type is given. |
| 193 | + */ |
| 194 | + public function __construct(mixed $value) |
| 195 | + { |
| 196 | + if ($value instanceof static) { |
| 197 | + /** @psalm-var T */ |
| 198 | + $value = $value->getValue(); |
| 199 | + } |
| 200 | + |
| 201 | + /** @psalm-suppress ImplicitToStringCast assertValidValueReturningKey returns always a string but psalm has currently an issue here */ |
| 202 | + $this->key = static::assertValidValueReturningKey($value); |
| 203 | + |
| 204 | + /** @psalm-var T */ |
| 205 | + $this->value = $value; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * @psalm-pure |
| 210 | + * @psalm-suppress InvalidCast |
| 211 | + * @return string |
| 212 | + */ |
| 213 | + public function __toString() |
| 214 | + { |
| 215 | + return (string)$this->value; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * Returns the enum key (i.e. the constant name). |
| 220 | + * |
| 221 | + * @return string |
| 222 | + */ |
| 223 | + public function getKey(): string |
| 224 | + { |
| 225 | + return $this->key; |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * @return mixed |
| 230 | + */ |
| 231 | + public function getValue(): mixed |
| 232 | + { |
| 233 | + return $this->value; |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * @return mixed |
| 238 | + */ |
| 239 | + public function jsonSerialize(): mixed |
| 240 | + { |
| 241 | + return $this->getValue(); |
| 242 | + } |
| 243 | +} |
0 commit comments