diff --git a/src/Enum.php b/src/Enum.php index 1d4f7ec..f7ef159 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -28,6 +28,13 @@ abstract class Enum implements \JsonSerializable */ protected $value; + /** + * Enum key, the constant name + * + * @var string + */ + protected $key; + /** * Store existing constants in a static cache per object. * @@ -61,12 +68,13 @@ public function __construct($value) $value = $value->getValue(); } - if (!$this->isValid($value)) { + if (false === ($key = static::search($value))) { throw new \UnexpectedValueException("Value '$value' is not part of the enum " . static::class); } /** @psalm-var T */ $this->value = $value; + $this->key = $key; } /** @@ -86,7 +94,7 @@ public function getValue() */ public function getKey(): string { - return static::search($this->value); + return $this->key; } /**