Skip to content

Commit 13cccd5

Browse files
authored
Merge pull request #32 from swisnl/issue/31
Align CollectionDocument(Interface) with ItemDocument(Interface)
2 parents f1f2b2f + e6441c5 commit 13cccd5

File tree

7 files changed

+163
-105
lines changed

7 files changed

+163
-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
}

tests/DocumentTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Swis\JsonApi\Client\Collection;
7+
use Swis\JsonApi\Client\Document;
8+
use Swis\JsonApi\Client\Errors\ErrorCollection;
9+
use Swis\JsonApi\Client\Item;
10+
11+
class DocumentTest extends TestCase
12+
{
13+
/**
14+
* @test
15+
*/
16+
public function it_returns_only_filled_properties_in_toArray()
17+
{
18+
$document = new Document();
19+
20+
$this->assertEquals([], $document->toArray());
21+
22+
$document->setLinks(
23+
[
24+
'self' => 'http://example.com/articles',
25+
'next' => 'http://example.com/articles?page[offset]=2',
26+
'last' => 'http://example.com/articles?page[offset]=10',
27+
]
28+
);
29+
$document->setData(
30+
(new Item(['title' => 'JSON:API paints my bikeshed!']))
31+
->setType('articles')
32+
->setId(1)
33+
);
34+
$document->setIncluded(
35+
new Collection(
36+
[
37+
(new Item(['firstName' => 'Dan', 'lastName' => 'Gebhardt', 'twitter' => 'dgeb']))
38+
->setType('people')
39+
->setId(9),
40+
]
41+
)
42+
);
43+
$document->setMeta(['copyright' => 'Copyright 2015 Example Corp.']);
44+
$document->setErrors(
45+
new ErrorCollection(
46+
[
47+
['id' => 'error-1'],
48+
]
49+
)
50+
);
51+
$document->setJsonapi(['version' => '1.0']);
52+
53+
$this->assertEquals(
54+
[
55+
'links' => [
56+
'self' => 'http://example.com/articles',
57+
'next' => 'http://example.com/articles?page[offset]=2',
58+
'last' => 'http://example.com/articles?page[offset]=10',
59+
],
60+
'data' => [
61+
'type' => 'articles',
62+
'id' => 1,
63+
'attributes' => [
64+
'title' => 'JSON:API paints my bikeshed!',
65+
],
66+
],
67+
'included' => [
68+
[
69+
'type' => 'people',
70+
'id' => 9,
71+
'attributes' => [
72+
'firstName' => 'Dan',
73+
'lastName' => 'Gebhardt',
74+
'twitter' => 'dgeb',
75+
],
76+
],
77+
],
78+
'meta' => [
79+
'copyright' => 'Copyright 2015 Example Corp.',
80+
],
81+
'errors' => [
82+
[
83+
'id' => 'error-1',
84+
],
85+
],
86+
'jsonapi' => [
87+
'version' => '1.0',
88+
],
89+
],
90+
$document->toArray()
91+
);
92+
}
93+
}

0 commit comments

Comments
 (0)