11
11
*
12
12
* Create an enum by implementing this class and adding class constants.
13
13
*
14
+ * @package MyCLabs\Enum
14
15
* @author Matthieu Napoli <[email protected] >
15
16
*/
16
17
abstract class Enum
17
18
{
18
19
/**
19
20
* Enum value
21
+ *
20
22
* @var mixed
21
23
*/
22
24
protected $ value ;
23
25
24
26
/**
25
27
* Store existing constants in a static cache per object.
28
+ *
26
29
* @var array
27
30
*/
28
- private static $ constantsCache = array ();
31
+ private static $ cache = array ();
29
32
30
33
/**
31
34
* Creates a new value of some type
35
+ *
32
36
* @param mixed $value
37
+ *
33
38
* @throws \UnexpectedValueException if incompatible type is given.
34
39
*/
35
40
public function __construct ($ value )
36
41
{
37
- $ possibleValues = self ::toArray ();
38
- if (! in_array ($ value , $ possibleValues )) {
42
+ if (!in_array ($ value , self ::toArray ())) {
39
43
throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . get_called_class ());
40
44
}
45
+
41
46
$ this ->value = $ value ;
42
47
}
43
48
@@ -49,6 +54,16 @@ public function getValue()
49
54
return $ this ->value ;
50
55
}
51
56
57
+ /**
58
+ * Returns the key of the current value on Enum
59
+ *
60
+ * @return mixed
61
+ */
62
+ public function getKey ()
63
+ {
64
+ return self ::search ($ this ->value );
65
+ }
66
+
52
67
/**
53
68
* @return string
54
69
*/
@@ -57,24 +72,80 @@ public function __toString()
57
72
return (string ) $ this ->value ;
58
73
}
59
74
75
+ /**
76
+ * Returns the names (keys) of all constants in the Enum class
77
+ *
78
+ * @return array
79
+ */
80
+ public static function keys ()
81
+ {
82
+ return array_keys (static ::toArray ());
83
+ }
84
+
60
85
/**
61
86
* Returns all possible values as an array
87
+ *
62
88
* @return array Constant name in key, constant value in value
63
89
*/
64
90
public static function toArray ()
65
91
{
66
- $ calledClass = get_called_class ();
67
- if (!array_key_exists ($ calledClass , self ::$ constantsCache )) {
68
- $ reflection = new \ReflectionClass ($ calledClass );
69
- self ::$ constantsCache [ $ calledClass ] = $ reflection ->getConstants ();
92
+ $ class = get_called_class ();
93
+ if (!array_key_exists ($ class , self ::$ cache )) {
94
+ $ reflection = new \ReflectionClass ($ class );
95
+ self ::$ cache [ $ class ] = $ reflection ->getConstants ();
70
96
}
71
- return self ::$ constantsCache [$ calledClass ];
97
+
98
+ return self ::$ cache [$ class ];
99
+ }
100
+
101
+ /**
102
+ * Check if is valid enum value
103
+ *
104
+ * @static
105
+ *
106
+ * @param $value
107
+ *
108
+ * @return bool
109
+ */
110
+ public static function isValid ($ value )
111
+ {
112
+ return in_array ($ value , self ::toArray ());
113
+ }
114
+
115
+ /**
116
+ * Check if is valid enum key
117
+ *
118
+ * @static
119
+ *
120
+ * @param $key
121
+ *
122
+ * @return bool
123
+ */
124
+ public static function isValidKey ($ key )
125
+ {
126
+ return in_array ($ key , self ::keys ());
127
+ }
128
+
129
+ /**
130
+ * Return key for value
131
+ *
132
+ * @static
133
+ *
134
+ * @param $value
135
+ *
136
+ * @return mixed
137
+ */
138
+ public static function search ($ value )
139
+ {
140
+ return array_search ($ value , array_combine (self ::keys (), self ::toArray ()));
72
141
}
73
142
74
143
/**
75
144
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
145
+ *
76
146
* @param string $name
77
147
* @param array $arguments
148
+ *
78
149
* @return static
79
150
* @throws \BadMethodCallException
80
151
*/
@@ -83,6 +154,7 @@ public static function __callStatic($name, $arguments)
83
154
if (defined ("static:: $ name " )) {
84
155
return new static (constant ("static:: $ name " ));
85
156
}
157
+
86
158
throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . get_called_class ());
87
159
}
88
160
}
0 commit comments