From a3b3fc92069e08f5eb6fff1e8140596e2b7738d0 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Mon, 18 May 2020 11:56:06 +0200 Subject: [PATCH] Serialize classes as objects --- CHANGELOG.md | 4 ++- src/Document.php | 55 +++++++++++++++++++++++++++++------------- src/Jsonapi.php | 29 +++++++++++++++++++++- src/Links.php | 4 +-- src/Meta.php | 4 +-- tests/DocumentTest.php | 19 +++++++++++++++ 6 files changed, 92 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26987eb..666a13e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -* Nothing +### Fixed + +* `Document`, `Jsonapi`, `Links` and `Meta` classes will be serialized as object when empty. ## [1.1.0] - 2020-03-06 diff --git a/src/Document.php b/src/Document.php index 281f5d4..a9f4c68 100644 --- a/src/Document.php +++ b/src/Document.php @@ -160,21 +160,6 @@ public function setData(DataInterface $data) return $this; } - /** - * Specify data which should be serialized to JSON. - * - * @see 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 - * - * @since 5.4.0 - */ - public function jsonSerialize() - { - return $this->toArray(); - } - /** * @return array */ @@ -186,7 +171,7 @@ public function toArray(): array $document['links'] = $this->getLinks()->toArray(); } - if (!empty($this->getData())) { + if ($this->getData() !== null) { $document['data'] = $this->data->toJsonApiArray(); } @@ -199,7 +184,7 @@ public function toArray(): array } if ($this->hasErrors()) { - $document['errors'] = $this->errors->toArray(); + $document['errors'] = $this->getErrors()->toArray(); } if ($this->getJsonapi() !== null) { @@ -208,4 +193,40 @@ public function toArray(): array return $document; } + + /** + * {@inheritdoc} + * + * @return object + */ + public function jsonSerialize() + { + $document = []; + + if ($this->getLinks() !== null) { + $document['links'] = $this->getLinks(); + } + + if ($this->getData() !== null) { + $document['data'] = $this->data->toJsonApiArray(); + } + + if ($this->getIncluded()->isNotEmpty()) { + $document['included'] = $this->getIncluded()->toJsonApiArray(); + } + + if ($this->getMeta() !== null) { + $document['meta'] = $this->getMeta(); + } + + if ($this->hasErrors()) { + $document['errors'] = $this->getErrors(); + } + + if ($this->getJsonapi() !== null) { + $document['jsonapi'] = $this->getJsonapi(); + } + + return (object) $document; + } } diff --git a/src/Jsonapi.php b/src/Jsonapi.php index 649c2ba..055e587 100644 --- a/src/Jsonapi.php +++ b/src/Jsonapi.php @@ -2,9 +2,12 @@ namespace Swis\JsonApi\Client; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; +use JsonSerializable; use Swis\JsonApi\Client\Concerns\HasMeta; -class Jsonapi +class Jsonapi implements Arrayable, Jsonable, JsonSerializable { use HasMeta; @@ -32,6 +35,8 @@ public function getVersion() } /** + * {@inheritdoc} + * * @return array */ public function toArray(): array @@ -48,4 +53,26 @@ public function toArray(): array return $array; } + + /** + * {@inheritdoc} + * + * @param int $options + * + * @return false|string + */ + public function toJson($options = 0) + { + return json_encode($this->jsonSerialize(), $options); + } + + /** + * {@inheritdoc} + * + * @return object + */ + public function jsonSerialize() + { + return (object) $this->toArray(); + } } diff --git a/src/Links.php b/src/Links.php index acce020..2983aaa 100644 --- a/src/Links.php +++ b/src/Links.php @@ -134,10 +134,10 @@ public function toJson($options = 0) /** * {@inheritdoc} * - * @return array|mixed + * @return object */ public function jsonSerialize() { - return $this->toArray(); + return (object) $this->toArray(); } } diff --git a/src/Meta.php b/src/Meta.php index 2bfcfbc..991ebc2 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -129,10 +129,10 @@ public function toJson($options = 0) /** * {@inheritdoc} * - * @return array|mixed + * @return object */ public function jsonSerialize() { - return $this->toArray(); + return (object) $this->toArray(); } } diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 3a2f1e5..4ce5a0b 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -229,4 +229,23 @@ public function it_returns_only_filled_properties_in_toArray() $document->toArray() ); } + + /** + * @test + */ + public function it_serializes_empty_members_as_empty_objects() + { + $document = new Document(); + + $this->assertEquals('{}', json_encode($document)); + + $document->setData(new Collection()); + $document->setErrors(new ErrorCollection()); + $document->setIncluded(new Collection()); + $document->setJsonapi(new Jsonapi()); + $document->setLinks(new Links([])); + $document->setMeta(new Meta([])); + + $this->assertEquals('{"links":{},"data":[],"meta":{},"jsonapi":{}}', json_encode($document)); + } }