@@ -18,7 +18,7 @@ abstract class Enum
18
18
/**
19
19
* The selected value
20
20
*
21
- * @var null|boolean |int|float|string
21
+ * @var null|bool |int|float|string
22
22
*/
23
23
private $ value ;
24
24
@@ -46,8 +46,8 @@ abstract class Enum
46
46
/**
47
47
* Constructor
48
48
*
49
- * @param null|boolean |int|float|string $value The value to select
50
- * @param int|null $ordinal The ordinal number of the value
49
+ * @param null|bool |int|float|string $value The value to select
50
+ * @param int|null $ordinal The ordinal number of the value
51
51
*/
52
52
final private function __construct ($ value , $ ordinal = null )
53
53
{
@@ -96,7 +96,7 @@ final public function __wakeup()
96
96
/**
97
97
* Get the current selected value
98
98
*
99
- * @return null|boolean |int|float|string
99
+ * @return null|bool |int|float|string
100
100
*/
101
101
final public function getValue ()
102
102
{
@@ -142,7 +142,7 @@ final public function getOrdinal()
142
142
* Compare this enum against another enum and check if it's the same value
143
143
*
144
144
* @param mixed $value
145
- * @return boolean
145
+ * @return bool
146
146
*/
147
147
final public function is ($ enum )
148
148
{
@@ -151,17 +151,30 @@ final public function is($enum)
151
151
}
152
152
153
153
/**
154
- * Get an enum of the given value
154
+ * Get an enum of the given value or instance
155
155
*
156
- * @param static|null|boolean|int|float|string $value
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.
159
+ *
160
+ * @param static|null|bool|int|float|string $value
161
+ * @param bool $tradeExtendedAsUnknown
157
162
* @return static
158
163
* @throws InvalidArgumentException On an unknwon or invalid value
159
164
* @throws LogicException On ambiguous constant values
160
165
*/
161
- final public static function get ($ value )
166
+ final public static function get ($ value, $ tradeExtendedAsUnknown = true )
162
167
{
163
168
if ($ value instanceof static) {
164
- $ value = $ value ->getValue ();
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
+ }
177
+ return $ value ;
165
178
}
166
179
167
180
$ class = get_called_class ();
0 commit comments