Skip to content

Commit

Permalink
Serialize classes as objects
Browse files Browse the repository at this point in the history
  • Loading branch information
JaZo committed May 18, 2020
1 parent ce8fe8f commit a3b3fc9
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 23 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
55 changes: 38 additions & 17 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <b>json_encode</b>,
* which is a value of any type other than a resource
*
* @since 5.4.0
*/
public function jsonSerialize()
{
return $this->toArray();
}

/**
* @return array
*/
Expand All @@ -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();
}

Expand All @@ -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) {
Expand All @@ -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;
}
}
29 changes: 28 additions & 1 deletion src/Jsonapi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -32,6 +35,8 @@ public function getVersion()
}

/**
* {@inheritdoc}
*
* @return array
*/
public function toArray(): array
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
19 changes: 19 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit a3b3fc9

Please sign in to comment.