Skip to content

Commit

Permalink
Merge pull request #17 from nasyrov/mrhn-feature/json-encode-enum
Browse files Browse the repository at this point in the history
Mrhn feature/json encode enum
  • Loading branch information
Evgenii Nasyrov authored Apr 4, 2018
2 parents e44ed6a + a8849b0 commit 0fd7086
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use BadMethodCallException;
use ReflectionClass;
use UnexpectedValueException;
use JsonSerializable;

abstract class Enum
abstract class Enum implements JsonSerializable
{
/**
* The enum value.
Expand Down Expand Up @@ -48,6 +49,16 @@ public function __toString()
return (string)$this->value;
}

/**
* Get the serialized value.
*
* @return mixed
*/
public function jsonSerialize()
{
return $this->value;
}

/**
* Get the enum key.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public function it_converts_enum_value_to_string()
);
}

/** @test */
public function it_gets_serialized_value()
{
$this->assertEquals(123, json_encode(new EnumFixture(123)));
$this->assertEquals('"test"', json_encode(new EnumFixture('test')));
}

/** @test */
public function it_gets_enum_key()
{
Expand Down

0 comments on commit 0fd7086

Please sign in to comment.