Exception
11 months ago
AbstractEnum.php
11 months ago
EnumMap.php
11 months ago
NullValue.php
11 months ago
AbstractEnum.php
203 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WP2FA_Vendor\DASPRiD\Enum; |
| 5 | |
| 6 | use WP2FA_Vendor\DASPRiD\Enum\Exception\CloneNotSupportedException; |
| 7 | use WP2FA_Vendor\DASPRiD\Enum\Exception\IllegalArgumentException; |
| 8 | use WP2FA_Vendor\DASPRiD\Enum\Exception\MismatchException; |
| 9 | use WP2FA_Vendor\DASPRiD\Enum\Exception\SerializeNotSupportedException; |
| 10 | use WP2FA_Vendor\DASPRiD\Enum\Exception\UnserializeNotSupportedException; |
| 11 | use ReflectionClass; |
| 12 | abstract class AbstractEnum |
| 13 | { |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | private $name; |
| 18 | /** |
| 19 | * @var int |
| 20 | */ |
| 21 | private $ordinal; |
| 22 | /** |
| 23 | * @var array<string, array<string, static>> |
| 24 | */ |
| 25 | private static $values = []; |
| 26 | /** |
| 27 | * @var array<string, bool> |
| 28 | */ |
| 29 | private static $allValuesLoaded = []; |
| 30 | /** |
| 31 | * @var array<string, array> |
| 32 | */ |
| 33 | private static $constants = []; |
| 34 | /** |
| 35 | * The constructor is private by default to avoid arbitrary enum creation. |
| 36 | * |
| 37 | * When creating your own constructor for a parameterized enum, make sure to declare it as protected, so that |
| 38 | * the static methods are able to construct it. Avoid making it public, as that would allow creation of |
| 39 | * non-singleton enum instances. |
| 40 | */ |
| 41 | private function __construct() |
| 42 | { |
| 43 | } |
| 44 | /** |
| 45 | * Magic getter which forwards all calls to {@see self::valueOf()}. |
| 46 | * |
| 47 | * @return static |
| 48 | */ |
| 49 | public static final function __callStatic(string $name, array $arguments) : self |
| 50 | { |
| 51 | return static::valueOf($name); |
| 52 | } |
| 53 | /** |
| 54 | * Returns an enum with the specified name. |
| 55 | * |
| 56 | * The name must match exactly an identifier used to declare an enum in this type (extraneous whitespace characters |
| 57 | * are not permitted). |
| 58 | * |
| 59 | * @return static |
| 60 | * @throws IllegalArgumentException if the enum has no constant with the specified name |
| 61 | */ |
| 62 | public static final function valueOf(string $name) : self |
| 63 | { |
| 64 | if (isset(self::$values[static::class][$name])) { |
| 65 | return self::$values[static::class][$name]; |
| 66 | } |
| 67 | $constants = self::constants(); |
| 68 | if (\array_key_exists($name, $constants)) { |
| 69 | return self::createValue($name, $constants[$name][0], $constants[$name][1]); |
| 70 | } |
| 71 | throw new IllegalArgumentException(\sprintf('No enum constant %s::%s', static::class, $name)); |
| 72 | } |
| 73 | /** |
| 74 | * @return static |
| 75 | */ |
| 76 | private static function createValue(string $name, int $ordinal, array $arguments) : self |
| 77 | { |
| 78 | $instance = new static(...$arguments); |
| 79 | $instance->name = $name; |
| 80 | $instance->ordinal = $ordinal; |
| 81 | self::$values[static::class][$name] = $instance; |
| 82 | return $instance; |
| 83 | } |
| 84 | /** |
| 85 | * Obtains all possible types defined by this enum. |
| 86 | * |
| 87 | * @return static[] |
| 88 | */ |
| 89 | public static final function values() : array |
| 90 | { |
| 91 | if (isset(self::$allValuesLoaded[static::class])) { |
| 92 | return self::$values[static::class]; |
| 93 | } |
| 94 | if (!isset(self::$values[static::class])) { |
| 95 | self::$values[static::class] = []; |
| 96 | } |
| 97 | foreach (self::constants() as $name => $constant) { |
| 98 | if (\array_key_exists($name, self::$values[static::class])) { |
| 99 | continue; |
| 100 | } |
| 101 | static::createValue($name, $constant[0], $constant[1]); |
| 102 | } |
| 103 | \uasort(self::$values[static::class], function (self $a, self $b) { |
| 104 | return $a->ordinal() <=> $b->ordinal(); |
| 105 | }); |
| 106 | self::$allValuesLoaded[static::class] = \true; |
| 107 | return self::$values[static::class]; |
| 108 | } |
| 109 | private static function constants() : array |
| 110 | { |
| 111 | if (isset(self::$constants[static::class])) { |
| 112 | return self::$constants[static::class]; |
| 113 | } |
| 114 | self::$constants[static::class] = []; |
| 115 | $reflectionClass = new ReflectionClass(static::class); |
| 116 | $ordinal = -1; |
| 117 | foreach ($reflectionClass->getReflectionConstants() as $reflectionConstant) { |
| 118 | if (!$reflectionConstant->isProtected()) { |
| 119 | continue; |
| 120 | } |
| 121 | $value = $reflectionConstant->getValue(); |
| 122 | self::$constants[static::class][$reflectionConstant->name] = [++$ordinal, \is_array($value) ? $value : []]; |
| 123 | } |
| 124 | return self::$constants[static::class]; |
| 125 | } |
| 126 | /** |
| 127 | * Returns the name of this enum constant, exactly as declared in its enum declaration. |
| 128 | * |
| 129 | * Most programmers should use the {@see self::__toString()} method in preference to this one, as the toString |
| 130 | * method may return a more user-friendly name. This method is designed primarily for use in specialized situations |
| 131 | * where correctness depends on getting the exact name, which will not vary from release to release. |
| 132 | */ |
| 133 | public final function name() : string |
| 134 | { |
| 135 | return $this->name; |
| 136 | } |
| 137 | /** |
| 138 | * Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial |
| 139 | * constant is assigned an ordinal of zero). |
| 140 | * |
| 141 | * Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data |
| 142 | * structures. |
| 143 | */ |
| 144 | public final function ordinal() : int |
| 145 | { |
| 146 | return $this->ordinal; |
| 147 | } |
| 148 | /** |
| 149 | * Compares this enum with the specified object for order. |
| 150 | * |
| 151 | * Returns negative integer, zero or positive integer as this object is less than, equal to or greater than the |
| 152 | * specified object. |
| 153 | * |
| 154 | * Enums are only comparable to other enums of the same type. The natural order implemented by this method is the |
| 155 | * order in which the constants are declared. |
| 156 | * |
| 157 | * @throws MismatchException if the passed enum is not of the same type |
| 158 | */ |
| 159 | public final function compareTo(self $other) : int |
| 160 | { |
| 161 | if (!$other instanceof static) { |
| 162 | throw new MismatchException(\sprintf('The passed enum %s is not of the same type as %s', \get_class($other), static::class)); |
| 163 | } |
| 164 | return $this->ordinal - $other->ordinal; |
| 165 | } |
| 166 | /** |
| 167 | * Forbid cloning enums. |
| 168 | * |
| 169 | * @throws CloneNotSupportedException |
| 170 | */ |
| 171 | public final function __clone() |
| 172 | { |
| 173 | throw new CloneNotSupportedException(); |
| 174 | } |
| 175 | /** |
| 176 | * Forbid serializing enums. |
| 177 | * |
| 178 | * @throws SerializeNotSupportedException |
| 179 | */ |
| 180 | public final function __sleep() : array |
| 181 | { |
| 182 | throw new SerializeNotSupportedException(); |
| 183 | } |
| 184 | /** |
| 185 | * Forbid unserializing enums. |
| 186 | * |
| 187 | * @throws UnserializeNotSupportedException |
| 188 | */ |
| 189 | public final function __wakeup() : void |
| 190 | { |
| 191 | throw new UnserializeNotSupportedException(); |
| 192 | } |
| 193 | /** |
| 194 | * Turns the enum into a string representation. |
| 195 | * |
| 196 | * You may override this method to give a more user-friendly version. |
| 197 | */ |
| 198 | public function __toString() : string |
| 199 | { |
| 200 | return $this->name; |
| 201 | } |
| 202 | } |
| 203 |