File tree 1 file changed +10
-2
lines changed
1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,13 @@ abstract class Enum implements \JsonSerializable
28
28
*/
29
29
protected $ value ;
30
30
31
+ /**
32
+ * Enum key, the constant name
33
+ *
34
+ * @var string
35
+ */
36
+ protected $ key ;
37
+
31
38
/**
32
39
* Store existing constants in a static cache per object.
33
40
*
@@ -61,12 +68,13 @@ public function __construct($value)
61
68
$ value = $ value ->getValue ();
62
69
}
63
70
64
- if (! $ this -> isValid ($ value )) {
71
+ if (false === ( $ key = static :: search ($ value) )) {
65
72
throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . static ::class);
66
73
}
67
74
68
75
/** @psalm-var T */
69
76
$ this ->value = $ value ;
77
+ $ this ->key = $ key ;
70
78
}
71
79
72
80
/**
@@ -86,7 +94,7 @@ public function getValue()
86
94
*/
87
95
public function getKey (): string
88
96
{
89
- return static :: search ( $ this ->value ) ;
97
+ return $ this ->key ;
90
98
}
91
99
92
100
/**
You can’t perform that action at this time.
0 commit comments