67 if (
$value instanceof
static) {
72 $this->key = static::assertValidValueReturningKey(
$value);
80 if ($this->key === null) {
81 $this->key = static::search($this->value);
92 $key = static::assertValidValueReturningKey(
$value);
94 return self::__callStatic(
$key, []);
138 final public function equals($variable = null): bool
140 return $variable instanceof
self 141 && $this->
getValue() === $variable->getValue()
142 && static::class === \get_class($variable);
187 $class = static::class;
189 if (!isset(static::$cache[$class])) {
191 $reflection = new \ReflectionClass($class);
193 static::$cache[$class] = $reflection->getConstants();
196 return static::$cache[$class];
221 self::assertValidValueReturningKey(
$value);
232 if (
false === (
$key = static::search(
$value))) {
233 throw new \UnexpectedValueException(
"Value '$value' is not part of the enum " . static::class);
251 return isset($array[
$key]) || \array_key_exists($key, $array);
281 $class = static::class;
282 if (!isset(self::$instances[$class][
$name])) {
284 if (!isset($array[$name]) && !\array_key_exists($name, $array)) {
285 $message =
"No static method or enum constant '$name' in class " . static::class;
286 throw new \BadMethodCallException(
$message);
288 return self::$instances[$class][
$name] =
new static($array[
$name]);
290 return clone self::$instances[$class][
$name];
static toArray()
Returns all possible values as an array.
static assertValidValueReturningKey($value)
Asserts valid enum value.
__construct($value)
Creates a new value of some type.
http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
static __callStatic($name, $arguments)
Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class cons...
static keys()
Returns the names (keys) of all constants in the Enum class.
static isValidKey($key)
Check if is valid enum key.
catch(Exception $e) $message
equals($variable=null)
Determines if Enum should be considered equal with the variable passed as a parameter.
jsonSerialize()
Specify data which should be serialized to JSON.
static values()
Returns instances of the Enum class of all Enum constants.
toArray($value)
Wrap the given value in an array if it is no array.
static search($value)
Return key for value.
getKey()
Returns the enum key (i.e.
static assertValidValue($value)
Asserts valid enum value.
static isValid($value)
Check if is valid enum value.
__toString()
-pure -suppress InvalidCast