@@ -32,14 +32,14 @@ abstract class Enum
32
32
/**
33
33
* An array of available constants by class
34
34
*
35
- * @var array ["$class" => ["$const " => $value, ...], ...]
35
+ * @var array ["$class" => ["$name " => $value, ...], ...]
36
36
*/
37
37
private static $ constants = array ();
38
38
39
39
/**
40
40
* Already instantiated enumerators
41
41
*
42
- * @var array ["$class" => ["$const " => $instance, ...], ...]
42
+ * @var array ["$class" => ["$name " => $instance, ...], ...]
43
43
*/
44
44
private static $ instances = array ();
45
45
@@ -139,41 +139,27 @@ final public function getOrdinal()
139
139
}
140
140
141
141
/**
142
- * Compare this enumerator against another enumerator and check if it's the same
142
+ * Compare this enumerator against another and check if it's the same.
143
143
*
144
144
* @param mixed $enum
145
145
* @return bool
146
146
*/
147
147
final public function is ($ enum )
148
148
{
149
- return $ this ->value === $ enum
150
- || (($ enum instanceof static || $ this instanceof $ enum ) && $ this ->value === $ enum ->getValue ());
149
+ return $ this === $ enum || $ this ->value === $ enum ;
151
150
}
152
151
153
152
/**
154
- * Instantiate an enumerator of the given value or instance
155
- *
156
- * On passing an extended instance the instance will be returned if the value
157
- * is inherited by the called class or if $tradeExtendedAsUnknown is disabled
158
- * else an InvalidArgumentException will be thrown.
153
+ * Get an enumerator instance of the given value or instance
159
154
*
160
155
* @param static|null|bool|int|float|string $value
161
- * @param bool $tradeExtendedAsUnknown
162
156
* @return static
163
157
* @throws InvalidArgumentException On an unknwon or invalid value
164
158
* @throws LogicException On ambiguous constant values
165
159
*/
166
160
final public static function get ($ value , $ tradeExtendedAsUnknown = true )
167
161
{
168
- if ($ value instanceof static) {
169
- if ($ tradeExtendedAsUnknown && !defined ('static:: ' . $ value ->getName ())) {
170
- throw new InvalidArgumentException (sprintf (
171
- "%s::%s is not inherited from %s " ,
172
- get_class ($ value ),
173
- $ value ->getName (),
174
- get_called_class ()
175
- ));
176
- }
162
+ if ($ value instanceof static && get_class ($ value ) === get_called_class ()) {
177
163
return $ value ;
178
164
}
179
165
@@ -184,19 +170,22 @@ final public static function get($value, $tradeExtendedAsUnknown = true)
184
170
if (is_scalar ($ value )) {
185
171
throw new InvalidArgumentException ('Unknown value ' . var_export ($ value , true ));
186
172
} else {
187
- throw new InvalidArgumentException ('Invalid value of type ' . gettype ($ value ));
173
+ throw new InvalidArgumentException (sprintf (
174
+ 'Invalid value of type %s ' ,
175
+ is_object ($ value ) ? get_class ($ value ) : gettype ($ value )
176
+ ));
188
177
}
189
178
}
190
179
191
- if (isset (self ::$ instances [$ class ][$ name ])) {
192
- return self ::$ instances [$ class ][$ name ];
180
+ if (! isset (self ::$ instances [$ class ][$ name ])) {
181
+ self ::$ instances [$ class ][$ name ] = new $ class ( $ constants [ $ name ]) ;
193
182
}
194
183
195
- return self ::$ instances [$ class ][$ name ] = new $ class ( $ constants [ $ name ]) ;
184
+ return self ::$ instances [$ class ][$ name ];
196
185
}
197
186
198
187
/**
199
- * Instantiate an enumarator by the given name
188
+ * Get an enumarator instance by the given name
200
189
*
201
190
* @param string $name The name of the enumerator
202
191
* @return static
@@ -220,7 +209,7 @@ final public static function getByName($name)
220
209
}
221
210
222
211
/**
223
- * Instantiate an enumeration by the given ordinal number
212
+ * Get an enumeration instance by the given ordinal number
224
213
*
225
214
* @param int $ordinal The ordinal number or the enumerator
226
215
* @return static
@@ -314,7 +303,7 @@ private static function detectConstants($class)
314
303
}
315
304
316
305
/**
317
- * Instantiate an enumarator by the given name.
306
+ * Get an enumarator instance by the given name.
318
307
*
319
308
* This will be called automatically on calling a method
320
309
* with the same name of a defined enumerator.
0 commit comments