Skip to content

Commit 6972d7f

Browse files
authored
Merge pull request #47 from swisnl/feature/meta-and-links
Meta and links
2 parents f2d77a1 + b7e130a commit 6972d7f

33 files changed

+1361
-256
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99
### Added
1010

1111
* Added `take` method to `Repository` to allow fetching resources without id.
12+
* Added links and meta to `ItemInterface`.
13+
N.B. This is a breaking change if you implement the `ItemInterface` yourself instead of using the supplied `Item`.
14+
* Added `Jsonapi` class.
15+
* Added links and meta to `OneRelationInterface` and `ManyRelationInterface`.
16+
N.B. This is a breaking change if you implement (one of) these interfaces yourself instead of using the supplied relations.
17+
* Added `Link` and `Links` classes.
18+
* Added links to `Error`.
19+
20+
### Changed
21+
22+
* `Error::getMeta()` now returns a `Meta` instance instead of an `ErrorMeta` instance. The `Meta` class does not have the `has` and `get` methods, but uses magic overloading methods (e.g. `__get` and `__set`) just like `Item`.
23+
N.B. This is a breaking change if you use meta on errors.
24+
* `DocumentInterface::getLinks()` now returns a `Links` instance instead of a plain array. If no links are present, it returns `null`. All implementations have been updated to reflect these changes.
25+
N.B. This is a minor breaking change if you use links on documents.
26+
* `DocumentInterface::getMeta()` now returns a `Meta` instance instead of a plain array. If no meta is present, it returns `null`. All implementations have been updated to reflect these changes.
27+
N.B. This is a minor breaking change if you use meta on documents.
28+
* `DocumentInterface::getJsonapi()` now returns a `Jsonapi` instance instead of a plain array. If no jsonapi is present, it returns `null`. All implementations have been updated to reflect these changes.
29+
* Parameters for `ItemInterface::setRelation()` have changed to include optional `Links` and `Meta` objects.
30+
* `JsonApi\ErrorsParser`, `JsonApi\Hydrator` and `JsonApi\Parser` have an extra dependency in their constructor.
31+
N.B. Make sure to add this dependency if you've overwritten `ServiceProvider::registerParser` or construct the `JsonApi\Parser` yourself.
32+
33+
### Removed
34+
35+
* Removed `ErrorMeta` class in favor of generic `Meta` class.
36+
37+
### Fixed
38+
39+
* Fixed parsing of [JSON:API object](https://jsonapi.org/format/#document-jsonapi-object) in document.
1240

1341
## [0.14.0] - 2019-01-23
1442

README.MD

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,56 @@ class BlogItem extends Item
149149
This package uses [Laravel Collections](https://laravel.com/docs/collections) as a wrapper for item arrays.
150150

151151

152+
## Links
153+
154+
All objects that can have links use an instance of `Links` to store their links.
155+
These are in turn an instance of `Link`.
156+
157+
### Example
158+
159+
Given the following JSON:
160+
``` json
161+
{
162+
"data": [],
163+
"links": {
164+
"self": "http://example.com/test"
165+
}
166+
}
167+
```
168+
169+
You can get the links this way:
170+
``` php
171+
/** @var $document \Swis\JsonApi\Client\Document */
172+
$links = $document->getLinks();
173+
echo $links->self->getHref(); // http://example.com/test
174+
```
175+
176+
177+
## Meta
178+
179+
All objects that can have meta information use an instance of `Meta` to store their metadata.
180+
This is a simple array-like object with key-value pairs.
181+
182+
### Example
183+
184+
Given the following JSON:
185+
``` json
186+
{
187+
"data": [],
188+
"meta": {
189+
"foo": "bar"
190+
}
191+
}
192+
```
193+
194+
You can get the meta this way:
195+
``` php
196+
/** @var $document \Swis\JsonApi\Client\Document */
197+
$meta = $document->getMeta();
198+
echo $meta->foo; // bar
199+
```
200+
201+
152202
## TypeMapper
153203

154204
All custom models must be registered with the `TypeMapper`.

src/Document.php

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,18 @@
55
use Swis\JsonApi\Client\Errors\ErrorCollection;
66
use Swis\JsonApi\Client\Interfaces\DataInterface;
77
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
8+
use Swis\JsonApi\Client\Traits\HasLinks;
9+
use Swis\JsonApi\Client\Traits\HasMeta;
810

911
class Document implements DocumentInterface
1012
{
13+
use HasLinks, HasMeta;
14+
1115
/**
1216
* @var \Swis\JsonApi\Client\Interfaces\DataInterface
1317
*/
1418
protected $data;
1519

16-
/**
17-
* @var array
18-
*/
19-
protected $meta = [];
20-
21-
/**
22-
* @var array
23-
*/
24-
protected $links = [];
25-
2620
/**
2721
* @var \Swis\JsonApi\Client\Errors\ErrorCollection
2822
*/
@@ -34,48 +28,16 @@ class Document implements DocumentInterface
3428
protected $included;
3529

3630
/**
37-
* @var array
31+
* @var \Swis\JsonApi\Client\Jsonapi|null
3832
*/
39-
protected $jsonapi = [];
33+
protected $jsonapi;
4034

4135
public function __construct()
4236
{
4337
$this->errors = new ErrorCollection();
4438
$this->included = new Collection();
4539
}
4640

47-
/**
48-
* @return array
49-
*/
50-
public function getMeta(): array
51-
{
52-
return $this->meta;
53-
}
54-
55-
/**
56-
* @param array $meta
57-
*/
58-
public function setMeta(array $meta)
59-
{
60-
$this->meta = $meta;
61-
}
62-
63-
/**
64-
* @return array
65-
*/
66-
public function getLinks(): array
67-
{
68-
return $this->links;
69-
}
70-
71-
/**
72-
* @param array $links
73-
*/
74-
public function setLinks(array $links)
75-
{
76-
$this->links = $links;
77-
}
78-
7941
/**
8042
* @return \Swis\JsonApi\Client\Errors\ErrorCollection
8143
*/
@@ -129,17 +91,17 @@ public function setIncluded(Collection $included)
12991
}
13092

13193
/**
132-
* @return array
94+
* @return \Swis\JsonApi\Client\Jsonapi|null
13395
*/
134-
public function getJsonapi(): array
96+
public function getJsonapi()
13597
{
13698
return $this->jsonapi;
13799
}
138100

139101
/**
140-
* @param array $jsonapi
102+
* @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi
141103
*/
142-
public function setJsonapi(array $jsonapi)
104+
public function setJsonapi(Jsonapi $jsonapi = null)
143105
{
144106
$this->jsonapi = $jsonapi;
145107
}
@@ -186,8 +148,8 @@ public function toArray(): array
186148
{
187149
$document = [];
188150

189-
if (!empty($this->getLinks())) {
190-
$document['links'] = $this->links;
151+
if ($this->getLinks() !== null) {
152+
$document['links'] = $this->getLinks()->toArray();
191153
}
192154

193155
if (!empty($this->getData())) {
@@ -198,16 +160,16 @@ public function toArray(): array
198160
$document['included'] = $this->getIncluded()->toJsonApiArray();
199161
}
200162

201-
if (!empty($this->getMeta())) {
202-
$document['meta'] = $this->meta;
163+
if ($this->getMeta() !== null) {
164+
$document['meta'] = $this->getMeta()->toArray();
203165
}
204166

205167
if ($this->hasErrors()) {
206168
$document['errors'] = $this->errors->toArray();
207169
}
208170

209-
if (!empty($this->getJsonapi())) {
210-
$document['jsonapi'] = $this->jsonapi;
171+
if ($this->getJsonapi() !== null) {
172+
$document['jsonapi'] = $this->getJsonapi()->toArray();
211173
}
212174

213175
return $document;

src/Errors/Error.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
22

33
namespace Swis\JsonApi\Client\Errors;
44

5+
use Swis\JsonApi\Client\Links;
6+
use Swis\JsonApi\Client\Meta;
7+
use Swis\JsonApi\Client\Traits\HasLinks;
8+
use Swis\JsonApi\Client\Traits\HasMeta;
9+
510
class Error
611
{
12+
use HasLinks, HasMeta;
13+
714
/**
815
* @var string|null
916
*/
@@ -34,30 +41,28 @@ class Error
3441
*/
3542
protected $source;
3643

37-
/**
38-
* @var \Swis\JsonApi\Client\Errors\ErrorMeta|null
39-
*/
40-
protected $meta;
41-
4244
/**
4345
* @param string|null $id
46+
* @param \Swis\JsonApi\Client\Links|null $links
4447
* @param string|null $status
4548
* @param string|null $code
4649
* @param string|null $title
4750
* @param string|null $detail
4851
* @param \Swis\JsonApi\Client\Errors\ErrorSource|null $source
49-
* @param \Swis\JsonApi\Client\Errors\ErrorMeta|null $meta
52+
* @param \Swis\JsonApi\Client\Meta|null $meta
5053
*/
5154
public function __construct(
5255
string $id = null,
56+
Links $links = null,
5357
string $status = null,
5458
string $code = null,
5559
string $title = null,
5660
string $detail = null,
5761
ErrorSource $source = null,
58-
ErrorMeta $meta = null
62+
Meta $meta = null
5963
) {
6064
$this->id = $id;
65+
$this->links = $links;
6166
$this->status = $status;
6267
$this->code = $code;
6368
$this->title = $title;
@@ -113,12 +118,4 @@ public function getSource()
113118
{
114119
return $this->source;
115120
}
116-
117-
/**
118-
* @return \Swis\JsonApi\Client\Errors\ErrorMeta|null
119-
*/
120-
public function getMeta()
121-
{
122-
return $this->meta;
123-
}
124121
}

src/Errors/ErrorMeta.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/Interfaces/DocumentInterface.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ public function getErrors(): ErrorCollection;
2323
public function hasErrors(): bool;
2424

2525
/**
26-
* @return mixed
26+
* @return \Swis\JsonApi\Client\Meta|null
2727
*/
28-
public function getMeta(): array;
28+
public function getMeta();
2929

3030
/**
31-
* @return mixed
31+
* @return \Swis\JsonApi\Client\Links|null
3232
*/
33-
public function getLinks(): array;
33+
public function getLinks();
3434

3535
/**
3636
* @return mixed
3737
*/
3838
public function getIncluded(): Collection;
3939

4040
/**
41-
* @return mixed
41+
* @return \Swis\JsonApi\Client\Jsonapi|null
4242
*/
4343
public function getJsonapi();
4444

0 commit comments

Comments
 (0)