diff --git a/src/Enum.php b/src/Enum.php index d1bc516..aa36c42 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -40,6 +40,12 @@ abstract class Enum implements \JsonSerializable */ public function __construct($value) { + if ($value instanceof static) { + $this->value = $value->getValue(); + + return; + } + if (!$this->isValid($value)) { throw new \UnexpectedValueException("Value '$value' is not part of the enum " . \get_called_class()); } diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 57f65a3..9591d73 100644 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -281,6 +281,15 @@ public function testBooleanEnum() $this->assertFalse((new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE))->jsonSerialize()); } + public function testConstructWithSameEnumArgument() + { + $enum = new EnumFixture(EnumFixture::FOO); + + $enveloped = new EnumFixture($enum); + + $this->assertEquals($enum, $enveloped); + } + private function assertJsonEqualsJson($json1, $json2) { $this->assertJsonStringEqualsJsonString($json1, $json2);