From 2fdc81f94ffb26fe6714d161a1df7f44d4abd009 Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 27 Oct 2015 14:59:27 +0300 Subject: [PATCH 1/3] Correct json serialization of Enum value --- composer.json | 2 +- src/Enum.php | 15 ++++++++++++++- tests/EnumTest.php | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 87a6119..5e8cae8 100755 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ } }, "require": { - "php": ">=5.3" + "php": ">=5.4" }, "require-dev": { "phpunit/phpunit": "4.*", diff --git a/src/Enum.php b/src/Enum.php index 2ad5aea..d43db51 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -15,7 +15,7 @@ * @author Daniel Costa * @author Mirosław Filip */ -abstract class Enum +abstract class Enum implements \JsonSerializable { /** * Enum value @@ -73,6 +73,19 @@ public function __toString() return (string)$this->value; } + /** + * (PHP 5 >= 5.4.0)
+ * Specify data which should be serialized to JSON + * @link http://php.net/manual/en/jsonserializable.jsonserialize.php + * @return mixed data which can be serialized by json_encode, + * which is a value of any type other than a resource. + */ + function jsonSerialize() + { + return $this->value; + } + + /** * Returns the names (keys) of all constants in the Enum class * diff --git a/tests/EnumTest.php b/tests/EnumTest.php index 5f16ee0..d2f7c96 100755 --- a/tests/EnumTest.php +++ b/tests/EnumTest.php @@ -71,6 +71,21 @@ public function testToString($expected, $enumObject) $this->assertSame($expected, (string) $enumObject); } + public function testJsonEncode() + { + $value = new EnumFixture(EnumFixture::FOO); + $encoded = json_encode($value); + $this->assertEquals($encoded, '"'.EnumFixture::FOO.'"'); + + $value = new EnumFixture(EnumFixture::BAR); + $encoded = json_encode($value); + $this->assertEquals($encoded, '"'.EnumFixture::BAR.'"'); + + $value = new EnumFixture(EnumFixture::NUMBER); + $encoded = json_encode($value); + $this->assertEquals($encoded, EnumFixture::NUMBER); + } + public function toStringProvider() { return array( array(EnumFixture::FOO, new EnumFixture(EnumFixture::FOO)), From 794113578fc25847955ea31b95b57e919c8eca4a Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 27 Oct 2015 15:04:05 +0300 Subject: [PATCH 2/3] Correct json serialization of Enum value --- src/Enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enum.php b/src/Enum.php index d43db51..0a3c8bb 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -82,7 +82,7 @@ public function __toString() */ function jsonSerialize() { - return $this->value; + return $this->getValue(); } From 1fca40a2bb761a5e64ed9d47eb9f2a3774dbc62e Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 27 Oct 2015 15:07:27 +0300 Subject: [PATCH 3/3] Correct json serialization of Enum value --- src/Enum.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Enum.php b/src/Enum.php index 0a3c8bb..ef992e2 100755 --- a/src/Enum.php +++ b/src/Enum.php @@ -80,7 +80,7 @@ public function __toString() * @return mixed data which can be serialized by json_encode, * which is a value of any type other than a resource. */ - function jsonSerialize() + public function jsonSerialize() { return $this->getValue(); }