Skip to content

Commit

Permalink
Merge pull request #83 from KartaviK/feature/envelope-enum-construct
Browse files Browse the repository at this point in the history
 Create instance of enum from same enum in constructor
  • Loading branch information
mnapoli authored Feb 4, 2019
2 parents 550d233 + 5147e69 commit 5b9c57d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
9 changes: 9 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5b9c57d

Please sign in to comment.