Skip to content

Implement JsonSerializable, fixes #67 #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- nightly
- hhvm

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
"require": {
"php": ">=5.3"
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.7|^6.0",
Expand Down
14 changes: 13 additions & 1 deletion src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Daniel Costa <[email protected]>
* @author Mirosław Filip <[email protected]>
*/
abstract class Enum
abstract class Enum implements \JsonSerializable
{
/**
* Enum value
Expand Down Expand Up @@ -183,4 +183,16 @@ public static function __callStatic($name, $arguments)

throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class());
}

/**
* Specify data which should be serialized to JSON. This method returns data that can be serialized by json_encode()
* natively.
*
* @return mixed
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
*/
public function jsonSerialize()
{
return $this->getValue();
}
}
16 changes: 15 additions & 1 deletion tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testGetKey()

/**
* @dataProvider invalidValueProvider
* @expectedException UnexpectedValueException
* @expectedException \UnexpectedValueException
* @expectedExceptionMessage is not part of the enum MyCLabs\Tests\Enum\EnumFixture
*/
public function testCreatingEnumWithInvalidValue($value)
Expand Down Expand Up @@ -247,4 +247,18 @@ public function testEqualsConflictValues()
{
$this->assertFalse(EnumFixture::FOO()->equals(EnumConflict::FOO()));
}

/**
* jsonSerialize()
*/
public function testJsonSerialize()
{
$this->assertJsonStringEqualsJsonString('"foo"', json_encode(new EnumFixture(EnumFixture::FOO)));
$this->assertJsonStringEqualsJsonString('"bar"', json_encode(new EnumFixture(EnumFixture::BAR)));
$this->assertJsonStringEqualsJsonString('42', json_encode(new EnumFixture(EnumFixture::NUMBER)));
$this->assertJsonStringEqualsJsonString('0', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NUMBER)));
$this->assertJsonStringEqualsJsonString('null', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_NULL)));
$this->assertJsonStringEqualsJsonString('""', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_EMPTY_STRING)));
$this->assertJsonStringEqualsJsonString('false', json_encode(new EnumFixture(EnumFixture::PROBLEMATIC_BOOLEAN_FALSE)));
}
}