@@ -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
{
@@ -78,7 +78,7 @@ final private function __clone()
78
78
/**
79
79
* Get the current selected value
80
80
*
81
- * @return null|boolean |int|float|string
81
+ * @return null|bool |int|float|string
82
82
*/
83
83
final public function getValue ()
84
84
{
@@ -124,25 +124,38 @@ final public function getOrdinal()
124
124
* Compare this enum against another enum and check if it's the same value
125
125
*
126
126
* @param mixed $value
127
- * @return boolean
127
+ * @return bool
128
128
*/
129
129
final public function is ($ enum )
130
130
{
131
131
return $ this ->value === $ enum || ($ enum instanceof static && $ this ->value === $ enum ->getValue ());
132
132
}
133
133
134
134
/**
135
- * Get an enum of the given value
135
+ * Get an enum of the given value or instance
136
136
*
137
- * @param static|null|boolean|int|float|string $value
137
+ * On passing an extended instance the instance will be returned if the value
138
+ * is inherited by the called class or if $tradeExtendedAsUnknown is disabled
139
+ * else an InvalidArgumentException will be thrown.
140
+ *
141
+ * @param static|null|bool|int|float|string $value
142
+ * @param bool $tradeExtendedAsUnknown
138
143
* @return static
139
144
* @throws InvalidArgumentException On an unknwon or invalid value
140
145
* @throws LogicException On ambiguous constant values
141
146
*/
142
- final public static function get ($ value )
147
+ final public static function get ($ value, $ tradeExtendedAsUnknown = true )
143
148
{
144
149
if ($ value instanceof static) {
145
- $ value = $ value ->getValue ();
150
+ if ($ tradeExtendedAsUnknown && !defined (get_called_class () . ':: ' . $ value ->getName ())) {
151
+ throw new InvalidArgumentException (sprintf (
152
+ "%s::%s is not inherited from %s " ,
153
+ get_class ($ value ),
154
+ $ value ->getName (),
155
+ get_called_class ()
156
+ ));
157
+ }
158
+ return $ value ;
146
159
}
147
160
148
161
$ class = get_called_class ();
0 commit comments