@@ -128,7 +128,7 @@ final public function getOrdinal()
128
128
* @throws InvalidArgumentException On an unknwon or invalid value
129
129
* @throws LogicException On ambiguous constant values
130
130
*/
131
- static public function get ($ value )
131
+ final static public function get ($ value )
132
132
{
133
133
$ class = get_called_class ();
134
134
$ id = $ class . '. ' . $ value ;
@@ -141,6 +141,45 @@ static public function get($value)
141
141
return $ instance ;
142
142
}
143
143
144
+ /**
145
+ * Get an enum by the given name
146
+ *
147
+ * @param string $name The name to instantiate the enum by
148
+ * @return Enum
149
+ * @throws InvalidArgumentException On an invalid or unknown name
150
+ * @throws LogicException On ambiguous constant values
151
+ */
152
+ final public static function getByName ($ name )
153
+ {
154
+ $ classConst = 'static:: ' . $ name ;
155
+ if (!defined ($ classConst )) {
156
+ $ class = get_called_class ();
157
+ throw new InvalidArgumentException ($ class . ':: ' . $ name . ' not defined ' );
158
+ }
159
+ return static ::get (constant ($ classConst ));
160
+ }
161
+
162
+ /**
163
+ * Get an enum by the given ordinal number
164
+ *
165
+ * @param int $ordinal The ordinal number to instantiate the enum by
166
+ * @return Enum
167
+ * @throws InvalidArgumentException On an invalid ordinal number
168
+ * @throws LogicException On ambiguous constant values
169
+ */
170
+ final public static function getByOrdinal ($ ordinal )
171
+ {
172
+ $ constants = static ::getConstants ();
173
+ $ item = array_slice ($ constants , $ ordinal , 1 , false );
174
+ if (!$ item ) {
175
+ throw new InvalidArgumentException (sprintf (
176
+ 'Invalid ordinal number, must between 0 and %s ' ,
177
+ count ($ constants ) - 1
178
+ ));
179
+ }
180
+ return static ::get (current ($ item ));
181
+ }
182
+
144
183
/**
145
184
* Clears all instantiated enums
146
185
*
@@ -149,7 +188,7 @@ static public function get($value)
149
188
* @param null|string $class
150
189
* @return void
151
190
*/
152
- final static function clear ()
191
+ final static public function clear ()
153
192
{
154
193
$ class = get_called_class ();
155
194
@@ -212,18 +251,14 @@ final static public function getConstants()
212
251
* This will be called automatically on calling a method
213
252
* with the same name of a defined constant.
214
253
*
215
- * @param string $const The name of the constant to instantiate the enum with
216
- * @param array $args There should be no arguments
217
- * @throws BadMethodCallException On an unknown constant name (method name)
218
- * @throws LogicException On ambiguous constant values
254
+ * @param string $method The name to instantiate the enum by (called as method)
255
+ * @param array $args There should be no arguments
256
+ * @return Enum
257
+ * @throws InvalidArgumentException On an invalid or unknown name
258
+ * @throws LogicException On ambiguous constant values
219
259
*/
220
- final public static function __callStatic ($ const , array $ args )
260
+ final public static function __callStatic ($ method , array $ args )
221
261
{
222
- $ classConst = 'static:: ' . $ const ;
223
- if (!defined ($ classConst )) {
224
- $ class = get_called_class ();
225
- throw new BadMethodCallException ($ class . ':: ' . $ const . ' not defined ' );
226
- }
227
- return static ::get (constant ($ classConst ));
262
+ return static ::getByName ($ method );
228
263
}
229
264
}
0 commit comments