From cfb1cdd272f634d9bc014080bdbf9b3fd15131e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Guti=C3=A9rrez?= Date: Wed, 19 Sep 2018 00:34:40 +0200 Subject: [PATCH] make json serializable --- composer.json | 1 + src/AbstractIdentity.php | 8 ++++++++ src/Identity.php | 2 +- tests/Identity/AbstractIdentityTest.php | 1 + tests/Identity/UuidIdentityTest.php | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 27a4254..1fabf12 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "prefer-stable": true, "require": { "php": "^7.1", + "ext-json": "*", "phpgears/immutability": "0.1", "ramsey/uuid": "^3.7" }, diff --git a/src/AbstractIdentity.php b/src/AbstractIdentity.php index cc192dd..176a8f8 100644 --- a/src/AbstractIdentity.php +++ b/src/AbstractIdentity.php @@ -67,6 +67,14 @@ final public function __toString(): string return $this->value; } + /** + * {@inheritdoc} + */ + final public function jsonSerialize(): string + { + return $this->value; + } + /** * {@inheritdoc} * diff --git a/src/Identity.php b/src/Identity.php index f70c6fb..179ccc0 100644 --- a/src/Identity.php +++ b/src/Identity.php @@ -16,7 +16,7 @@ /** * Identity value object interface. */ -interface Identity +interface Identity extends \JsonSerializable { /** * Get identity from string. diff --git a/tests/Identity/AbstractIdentityTest.php b/tests/Identity/AbstractIdentityTest.php index 13d86e6..f8670fd 100644 --- a/tests/Identity/AbstractIdentityTest.php +++ b/tests/Identity/AbstractIdentityTest.php @@ -27,6 +27,7 @@ public function testCreation(): void $this->assertSame('thisIsMyId', $stub->getValue()); $this->assertSame('thisIsMyId', (string) $stub); + $this->assertSame('"thisIsMyId"', \json_encode($stub)); } public function testEquality(): void diff --git a/tests/Identity/UuidIdentityTest.php b/tests/Identity/UuidIdentityTest.php index 0d96401..109bda3 100644 --- a/tests/Identity/UuidIdentityTest.php +++ b/tests/Identity/UuidIdentityTest.php @@ -28,6 +28,8 @@ public function testCreation(): void $stub = UuidIdentity::fromString($uuid); $this->assertSame($uuid, $stub->getValue()); + $this->assertSame('4c4316cb-b48b-44fb-a034-90d789966bac', (string) $stub); + $this->assertSame('"4c4316cb-b48b-44fb-a034-90d789966bac"', \json_encode($stub)); } /**