File tree 2 files changed +38
-3
lines changed
2 files changed +38
-3
lines changed Original file line number Diff line number Diff line change @@ -61,14 +61,20 @@ public function __construct($value)
61
61
$ value = $ value ->getValue ();
62
62
}
63
63
64
- if (!$ this ->isValid ($ value )) {
65
- throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . static ::class);
66
- }
64
+ self ::assertValidValue ($ value );
67
65
68
66
/** @psalm-var T */
69
67
$ this ->value = $ value ;
70
68
}
71
69
70
+ /** @param mixed $value */
71
+ public static function fromMixed ($ value ): self
72
+ {
73
+ self ::assertValidValue ($ value );
74
+
75
+ return new static ($ value );
76
+ }
77
+
72
78
/**
73
79
* @psalm-pure
74
80
* @return mixed
@@ -175,13 +181,27 @@ public static function toArray()
175
181
* @param $value
176
182
* @psalm-param mixed $value
177
183
* @psalm-pure
184
+ * @psalm-assert-if-true T $value
178
185
* @return bool
179
186
*/
180
187
public static function isValid ($ value )
181
188
{
182
189
return \in_array ($ value , static ::toArray (), true );
183
190
}
184
191
192
+ /**
193
+ * Asserts valid enum value
194
+ *
195
+ * @psalm-pure
196
+ * @psalm-assert T $value
197
+ */
198
+ public static function assertValidValue ($ value ): void
199
+ {
200
+ if (!self ::isValid ($ value )) {
201
+ throw new \UnexpectedValueException ("Value ' $ value' is not part of the enum " . static ::class);
202
+ }
203
+ }
204
+
185
205
/**
186
206
* Check if is valid enum key
187
207
*
Original file line number Diff line number Diff line change @@ -332,4 +332,19 @@ public function testEnumValuesInheritance()
332
332
$ inheritedEnumFixture = InheritedEnumFixture::VALUE ();
333
333
new EnumFixture ($ inheritedEnumFixture );
334
334
}
335
+
336
+ /**
337
+ * @dataProvider isValidProvider
338
+ */
339
+ public function testAssertValidValue ($ value , $ isValid ): void
340
+ {
341
+ if (!$ isValid ) {
342
+ $ this ->expectException (\UnexpectedValueException::class);
343
+ $ this ->expectExceptionMessage ("Value ' $ value' is not part of the enum " . EnumFixture::class);
344
+ }
345
+
346
+ EnumFixture::assertValidValue ($ value );
347
+
348
+ self ::assertTrue (EnumFixture::isValid ($ value ));
349
+ }
335
350
}
You can’t perform that action at this time.
0 commit comments