1111 *
1212 * Create an enum by implementing this class and adding class constants.
1313 *
14+ * @package MyCLabs\Enum
1415 * @author Matthieu Napoli <matthieu@mnapoli.fr>
1516 */
1617abstract class Enum
1718{
1819 /**
1920 * Enum value
21+ *
2022 * @var mixed
2123 */
2224 protected $ value ;
2325
2426 /**
2527 * Store existing constants in a static cache per object.
28+ *
2629 * @var array
2730 */
28- private static $ constantsCache = array ();
31+ private static $ cache = array ();
2932
3033 /**
3134 * Creates a new value of some type
35+ *
3236 * @param mixed $value
37+ *
3338 * @throws \UnexpectedValueException if incompatible type is given.
3439 */
3540 public function __construct ($ value )
3641 {
37- $ possibleValues = self ::toArray ();
38- if (! in_array ($ value , $ possibleValues )) {
42+ if (!in_array ($ value , self ::toArray ())) {
3943 throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . get_called_class ());
4044 }
45+
4146 $ this ->value = $ value ;
4247 }
4348
@@ -49,6 +54,16 @@ public function getValue()
4954 return $ this ->value ;
5055 }
5156
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+
5267 /**
5368 * @return string
5469 */
@@ -57,24 +72,80 @@ public function __toString()
5772 return (string ) $ this ->value ;
5873 }
5974
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+
6085 /**
6186 * Returns all possible values as an array
87+ *
6288 * @return array Constant name in key, constant value in value
6389 */
6490 public static function toArray ()
6591 {
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 ();
7096 }
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 ()));
72141 }
73142
74143 /**
75144 * Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant
145+ *
76146 * @param string $name
77147 * @param array $arguments
148+ *
78149 * @return static
79150 * @throws \BadMethodCallException
80151 */
@@ -83,6 +154,7 @@ public static function __callStatic($name, $arguments)
83154 if (defined ("static:: $ name " )) {
84155 return new static (constant ("static:: $ name " ));
85156 }
157+
86158 throw new \BadMethodCallException ("No static method or enum constant ' $ name' in class " . get_called_class ());
87159 }
88160}
0 commit comments