Skip to content

Commit 5b9c57d

Browse files
authored
Merge pull request #83 from KartaviK/feature/envelope-enum-construct
Create instance of enum from same enum in constructor
2 parents 550d233 + 5147e69 commit 5b9c57d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/Enum.php

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ abstract class Enum implements \JsonSerializable
4040
*/
4141
public function __construct($value)
4242
{
43+
if ($value instanceof static) {
44+
$this->value = $value->getValue();
45+
46+
return;
47+
}
48+
4349
if (!$this->isValid($value)) {
4450
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class());
4551
}

tests/EnumTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ public function testBooleanEnum()
281281
$this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize());
282282
}
283283

284+
public function testConstructWithSameEnumArgument()
285+
{
286+
$enum = new EnumFixture(EnumFixture::FOO);
287+
288+
$enveloped = new EnumFixture($enum);
289+
290+
$this->assertEquals($enum, $enveloped);
291+
}
292+
284293
private function assertJsonEqualsJson($json1, $json2)
285294
{
286295
$this->assertJsonStringEqualsJsonString($json1, $json2);

0 commit comments

Comments
 (0)