Skip to content

Commit a3b3fc9

Browse files
committed
Serialize classes as objects
1 parent ce8fe8f commit a3b3fc9

File tree

6 files changed

+92
-23
lines changed

6 files changed

+92
-23
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9-
* Nothing
9+
### Fixed
10+
11+
* `Document`, `Jsonapi`, `Links` and `Meta` classes will be serialized as object when empty.
1012

1113
## [1.1.0] - 2020-03-06
1214

src/Document.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -160,21 +160,6 @@ public function setData(DataInterface $data)
160160
return $this;
161161
}
162162

163-
/**
164-
* Specify data which should be serialized to JSON.
165-
*
166-
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
167-
*
168-
* @return mixed data which can be serialized by <b>json_encode</b>,
169-
* which is a value of any type other than a resource
170-
*
171-
* @since 5.4.0
172-
*/
173-
public function jsonSerialize()
174-
{
175-
return $this->toArray();
176-
}
177-
178163
/**
179164
* @return array
180165
*/
@@ -186,7 +171,7 @@ public function toArray(): array
186171
$document['links'] = $this->getLinks()->toArray();
187172
}
188173

189-
if (!empty($this->getData())) {
174+
if ($this->getData() !== null) {
190175
$document['data'] = $this->data->toJsonApiArray();
191176
}
192177

@@ -199,7 +184,7 @@ public function toArray(): array
199184
}
200185

201186
if ($this->hasErrors()) {
202-
$document['errors'] = $this->errors->toArray();
187+
$document['errors'] = $this->getErrors()->toArray();
203188
}
204189

205190
if ($this->getJsonapi() !== null) {
@@ -208,4 +193,40 @@ public function toArray(): array
208193

209194
return $document;
210195
}
196+
197+
/**
198+
* {@inheritdoc}
199+
*
200+
* @return object
201+
*/
202+
public function jsonSerialize()
203+
{
204+
$document = [];
205+
206+
if ($this->getLinks() !== null) {
207+
$document['links'] = $this->getLinks();
208+
}
209+
210+
if ($this->getData() !== null) {
211+
$document['data'] = $this->data->toJsonApiArray();
212+
}
213+
214+
if ($this->getIncluded()->isNotEmpty()) {
215+
$document['included'] = $this->getIncluded()->toJsonApiArray();
216+
}
217+
218+
if ($this->getMeta() !== null) {
219+
$document['meta'] = $this->getMeta();
220+
}
221+
222+
if ($this->hasErrors()) {
223+
$document['errors'] = $this->getErrors();
224+
}
225+
226+
if ($this->getJsonapi() !== null) {
227+
$document['jsonapi'] = $this->getJsonapi();
228+
}
229+
230+
return (object) $document;
231+
}
211232
}

src/Jsonapi.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Swis\JsonApi\Client;
44

5+
use Illuminate\Contracts\Support\Arrayable;
6+
use Illuminate\Contracts\Support\Jsonable;
7+
use JsonSerializable;
58
use Swis\JsonApi\Client\Concerns\HasMeta;
69

7-
class Jsonapi
10+
class Jsonapi implements Arrayable, Jsonable, JsonSerializable
811
{
912
use HasMeta;
1013

@@ -32,6 +35,8 @@ public function getVersion()
3235
}
3336

3437
/**
38+
* {@inheritdoc}
39+
*
3540
* @return array
3641
*/
3742
public function toArray(): array
@@ -48,4 +53,26 @@ public function toArray(): array
4853

4954
return $array;
5055
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*
60+
* @param int $options
61+
*
62+
* @return false|string
63+
*/
64+
public function toJson($options = 0)
65+
{
66+
return json_encode($this->jsonSerialize(), $options);
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*
72+
* @return object
73+
*/
74+
public function jsonSerialize()
75+
{
76+
return (object) $this->toArray();
77+
}
5178
}

src/Links.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public function toJson($options = 0)
134134
/**
135135
* {@inheritdoc}
136136
*
137-
* @return array|mixed
137+
* @return object
138138
*/
139139
public function jsonSerialize()
140140
{
141-
return $this->toArray();
141+
return (object) $this->toArray();
142142
}
143143
}

src/Meta.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public function toJson($options = 0)
129129
/**
130130
* {@inheritdoc}
131131
*
132-
* @return array|mixed
132+
* @return object
133133
*/
134134
public function jsonSerialize()
135135
{
136-
return $this->toArray();
136+
return (object) $this->toArray();
137137
}
138138
}

tests/DocumentTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,23 @@ public function it_returns_only_filled_properties_in_toArray()
229229
$document->toArray()
230230
);
231231
}
232+
233+
/**
234+
* @test
235+
*/
236+
public function it_serializes_empty_members_as_empty_objects()
237+
{
238+
$document = new Document();
239+
240+
$this->assertEquals('{}', json_encode($document));
241+
242+
$document->setData(new Collection());
243+
$document->setErrors(new ErrorCollection());
244+
$document->setIncluded(new Collection());
245+
$document->setJsonapi(new Jsonapi());
246+
$document->setLinks(new Links([]));
247+
$document->setMeta(new Meta([]));
248+
249+
$this->assertEquals('{"links":{},"data":[],"meta":{},"jsonapi":{}}', json_encode($document));
250+
}
232251
}

0 commit comments

Comments
 (0)