Skip to content

Commit 29573e9

Browse files
committed
Move jsonSerialize and toArray to Document(Interface)
1 parent f1f2b2f commit 29573e9

File tree

6 files changed

+70
-105
lines changed

6 files changed

+70
-105
lines changed

src/CollectionDocument.php

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,6 @@
44

55
use Swis\JsonApi\Client\Interfaces\CollectionDocumentInterface;
66

7-
class CollectionDocument extends Document implements CollectionDocumentInterface, \JsonSerializable
7+
class CollectionDocument extends Document implements CollectionDocumentInterface
88
{
9-
/**
10-
* Specify data which should be serialized to JSON.
11-
*
12-
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
13-
*
14-
* @return mixed data which can be serialized by <b>json_encode</b>,
15-
* which is a value of any type other than a resource
16-
*
17-
* @since 5.4.0
18-
*/
19-
public function jsonSerialize()
20-
{
21-
return $this->toArray();
22-
}
23-
24-
/**
25-
* @return array
26-
*/
27-
public function toArray(): array
28-
{
29-
$document = [];
30-
31-
if (!empty($this->getLinks())) {
32-
$document['links'] = $this->links;
33-
}
34-
35-
if (!empty($this->getData())) {
36-
$document['data'] = $this->data->toJsonApiArray();
37-
}
38-
39-
if (!empty($this->getMeta())) {
40-
$document['meta'] = $this->meta;
41-
}
42-
43-
if ($this->hasErrors()) {
44-
$document['errors'] = $this->errors->toArray();
45-
}
46-
47-
if (!empty($this->getJsonapi())) {
48-
$document['jsonapi'] = $this->jsonapi;
49-
}
50-
51-
return $document;
52-
}
539
}

src/Document.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Swis\JsonApi\Client\Interfaces\DataInterface;
77
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
88

9-
class Document implements DocumentInterface
9+
class Document implements DocumentInterface, \JsonSerializable
1010
{
1111
/**
1212
* @var \Swis\JsonApi\Client\Interfaces\DataInterface
@@ -155,4 +155,53 @@ public function setData(DataInterface $data)
155155

156156
return $this;
157157
}
158+
159+
/**
160+
* Specify data which should be serialized to JSON.
161+
*
162+
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
163+
*
164+
* @return mixed data which can be serialized by <b>json_encode</b>,
165+
* which is a value of any type other than a resource
166+
*
167+
* @since 5.4.0
168+
*/
169+
public function jsonSerialize()
170+
{
171+
return $this->toArray();
172+
}
173+
174+
/**
175+
* @return array
176+
*/
177+
public function toArray(): array
178+
{
179+
$document = [];
180+
181+
if (!empty($this->getLinks())) {
182+
$document['links'] = $this->links;
183+
}
184+
185+
if (!empty($this->getData())) {
186+
$document['data'] = $this->data->toJsonApiArray();
187+
}
188+
189+
if ($this->getIncluded()->isNotEmpty()) {
190+
$document['included'] = $this->getIncluded()->toJsonApiArray();
191+
}
192+
193+
if (!empty($this->getMeta())) {
194+
$document['meta'] = $this->meta;
195+
}
196+
197+
if ($this->hasErrors()) {
198+
$document['errors'] = $this->errors->toArray();
199+
}
200+
201+
if (!empty($this->getJsonapi())) {
202+
$document['jsonapi'] = $this->jsonapi;
203+
}
204+
205+
return $document;
206+
}
158207
}

src/Interfaces/DocumentInterface.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public function getData();
1717
*/
1818
public function getErrors(): ErrorCollection;
1919

20+
/**
21+
* @return bool
22+
*/
23+
public function hasErrors(): bool;
24+
2025
/**
2126
* @return mixed
2227
*/
@@ -38,7 +43,7 @@ public function getIncluded(): Collection;
3843
public function getJsonapi();
3944

4045
/**
41-
* @return bool
46+
* @return array
4247
*/
43-
public function hasErrors(): bool;
48+
public function toArray(): array;
4449
}

src/Interfaces/ItemDocumentInterface.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,4 @@ interface ItemDocumentInterface extends DocumentInterface
88
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface
99
*/
1010
public function getData();
11-
12-
/**
13-
* @return array
14-
*/
15-
public function toArray();
1611
}

src/InvalidResponseDocument.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public function getErrors(): ErrorCollection
2323
return new ErrorCollection();
2424
}
2525

26+
/**
27+
* @return bool
28+
*/
29+
public function hasErrors(): bool
30+
{
31+
return false;
32+
}
33+
2634
/**
2735
* @return mixed
2836
*/
@@ -56,10 +64,10 @@ public function getJsonapi()
5664
}
5765

5866
/**
59-
* @return bool
67+
* @return array
6068
*/
61-
public function hasErrors(): bool
69+
public function toArray(): array
6270
{
63-
return false;
71+
return [];
6472
}
6573
}

src/ItemDocument.php

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,6 @@
44

55
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface;
66

7-
class ItemDocument extends Document implements ItemDocumentInterface, \JsonSerializable
7+
class ItemDocument extends Document implements ItemDocumentInterface
88
{
9-
/**
10-
* Specify data which should be serialized to JSON.
11-
*
12-
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
13-
*
14-
* @return mixed data which can be serialized by <b>json_encode</b>,
15-
* which is a value of any type other than a resource
16-
*
17-
* @since 5.4.0
18-
*/
19-
public function jsonSerialize()
20-
{
21-
return $this->toArray();
22-
}
23-
24-
/**
25-
* @return array
26-
*/
27-
public function toArray(): array
28-
{
29-
$document = [];
30-
31-
if (!empty($this->getLinks())) {
32-
$document['links'] = $this->links;
33-
}
34-
35-
if (!empty($this->getData())) {
36-
$document['data'] = $this->data->toJsonApiArray();
37-
}
38-
39-
if (!empty($this->getIncluded())) {
40-
$document['included'] = $this->getIncluded()->toJsonApiArray();
41-
}
42-
43-
if (!empty($this->getMeta())) {
44-
$document['meta'] = $this->meta;
45-
}
46-
47-
if ($this->hasErrors()) {
48-
$document['errors'] = $this->errors->toArray();
49-
}
50-
51-
if (!empty($this->getJsonapi())) {
52-
$document['jsonapi'] = $this->jsonapi;
53-
}
54-
55-
return $document;
56-
}
579
}

0 commit comments

Comments
 (0)