Skip to content

Commit e7017e8

Browse files
authored
Merge pull request #54 from swisnl/feature/refactor-parser-hydrator
Refactor parser and hydrator
2 parents 00c0810 + 9719ff9 commit e7017e8

34 files changed

+1854
-1256
lines changed

CHANGELOG.md

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

1111
* Added `DocumentFactory`.
12-
* Added facades for `DocumentFactory`, `ItemHydrator` and `TypeMapper`.
12+
* Added facades for `DocumentFactory`, `DocumentParser`, `ItemHydrator`, `ResponseParser` and `TypeMapper`.
13+
* Added `DocumentParserInterface` and `ResponseParserInterface` interfaces and implementations.
1314

1415
### Changed
1516

@@ -20,10 +21,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2021
* Renamed `getRelationship` to `getRelation`, `hasRelationship` to `hasRelation` and `removeRelationship` to `unsetRelation` in `Item`.
2122
* Renamed/aligned some parameters in several relation methods in `Item`.
2223
* Renamed namespace `Swis\JsonApi\Client\Traits` to `Swis\JsonApi\Client\Concerns`.
24+
* Renamed namespace `Swis\JsonApi\Client\JsonApi` to `Swis\JsonApi\Client\Parsers`.
25+
* Renamed `ServiceProvider::registerParser` (singular) to `ServiceProvider::registerParsers` (plural).
2326

2427
### Removed
2528

2629
* Removed `CollectionDocumentBuilder` and `ItemDocumentBuilder` in favor of `DocumentFactory`.
30+
* Removed `JsonApi\Parser` in favor of `JsonApi\DocumentParser` and `JsonApi\ResponseParser`.
2731

2832
## [0.18.0] - 2019-07-01
2933

README.MD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ If you register your own service provider and use package auto discover, don't f
427427
```
428428

429429

430+
## Advanced usage
431+
432+
If you don't like to use the supplied repository or clients, you can also parse a 'raw' `\Psr\Http\Message\ResponseInterface` or a simple json string using the `Parsers\ResponseParser` or `Parser\DocumentParser` respectively.
433+
434+
430435
## Change log
431436

432437
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
],
6161
"aliases":{
6262
"DocumentFactory": "Swis\\JsonApi\\Client\\Facades\\DocumentFactoryFacade",
63+
"DocumentParser": "Swis\\JsonApi\\Client\\Facades\\DocumentParserFacade",
6364
"ItemHydrator": "Swis\\JsonApi\\Client\\Facades\\ItemHydratorFacade",
65+
"ResponseParser": "Swis\\JsonApi\\Client\\Facades\\ResponseParserFacade",
6466
"TypeMapper": "Swis\\JsonApi\\Client\\Facades\\TypeMapperFacade"
6567
}
6668
}

src/Document.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ public function getResponse(): ? ResponseInterface
5454

5555
/**
5656
* @param \Psr\Http\Message\ResponseInterface|null $response
57+
*
58+
* @return $this
5759
*/
58-
public function setResponse(ResponseInterface $response = null)
60+
public function setResponse(? ResponseInterface $response)
5961
{
6062
$this->response = $response;
63+
64+
return $this;
6165
}
6266

6367
/**
@@ -70,10 +74,14 @@ public function getErrors(): ErrorCollection
7074

7175
/**
7276
* @param \Swis\JsonApi\Client\ErrorCollection $errors
77+
*
78+
* @return $this
7379
*/
7480
public function setErrors(ErrorCollection $errors)
7581
{
7682
$this->errors = $errors;
83+
84+
return $this;
7785
}
7886

7987
/**
@@ -103,7 +111,7 @@ public function getIncluded(): Collection
103111
/**
104112
* @param \Swis\JsonApi\Client\Collection $included
105113
*
106-
* @return static
114+
* @return $this
107115
*/
108116
public function setIncluded(Collection $included)
109117
{
@@ -122,10 +130,14 @@ public function getJsonapi(): ? Jsonapi
122130

123131
/**
124132
* @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi
133+
*
134+
* @return $this
125135
*/
126-
public function setJsonapi(Jsonapi $jsonapi = null)
136+
public function setJsonapi(? Jsonapi $jsonapi)
127137
{
128138
$this->jsonapi = $jsonapi;
139+
140+
return $this;
129141
}
130142

131143
/**
@@ -139,7 +151,7 @@ public function getData()
139151
/**
140152
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
141153
*
142-
* @return static
154+
* @return $this
143155
*/
144156
public function setData(DataInterface $data)
145157
{

src/DocumentClient.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface;
88
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
99
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface;
10-
use Swis\JsonApi\Client\Interfaces\ParserInterface;
10+
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;
1111

1212
class DocumentClient implements DocumentClientInterface
1313
{
@@ -17,15 +17,15 @@ class DocumentClient implements DocumentClientInterface
1717
private $client;
1818

1919
/**
20-
* @var \Swis\JsonApi\Client\Interfaces\ParserInterface
20+
* @var \Swis\JsonApi\Client\Interfaces\ResponseParserInterface
2121
*/
2222
private $parser;
2323

2424
/**
25-
* @param \Swis\JsonApi\Client\Interfaces\ClientInterface $client
26-
* @param \Swis\JsonApi\Client\Interfaces\ParserInterface $parser
25+
* @param \Swis\JsonApi\Client\Interfaces\ClientInterface $client
26+
* @param \Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser
2727
*/
28-
public function __construct(ClientInterface $client, ParserInterface $parser)
28+
public function __construct(ClientInterface $client, ResponseParserInterface $parser)
2929
{
3030
$this->client = $client;
3131
$this->parser = $parser;
@@ -120,6 +120,6 @@ protected function sanitizeJson(string $json): string
120120
*/
121121
protected function parseResponse(ResponseInterface $response): DocumentInterface
122122
{
123-
return $this->parser->deserializeResponse($response);
123+
return $this->parser->parse($response);
124124
}
125125
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use Swis\JsonApi\Client\Interfaces\DocumentParserInterface;
7+
8+
/**
9+
* @method static \Swis\JsonApi\Client\Interfaces\DocumentInterface parse(string $json)
10+
*
11+
* @see \Swis\JsonApi\Client\Interfaces\DocumentParserInterface
12+
*/
13+
class DocumentParserFacade extends Facade
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected static function getFacadeAccessor()
19+
{
20+
return DocumentParserInterface::class;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;
7+
8+
/**
9+
* @method static \Swis\JsonApi\Client\Interfaces\DocumentInterface parse(\Psr\Http\Message\ResponseInterface $response)
10+
*
11+
* @see \Swis\JsonApi\Client\Interfaces\ResponseParserInterface
12+
*/
13+
class ResponseParserFacade extends Facade
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
protected static function getFacadeAccessor()
19+
{
20+
return ResponseParserInterface::class;
21+
}
22+
}

src/Interfaces/DocumentInterface.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,37 @@ interface DocumentInterface extends \JsonSerializable
1616
*/
1717
public function getResponse(): ? ResponseInterface;
1818

19+
/**
20+
* @param \Psr\Http\Message\ResponseInterface|null $response
21+
*
22+
* @return $this
23+
*/
24+
public function setResponse(? ResponseInterface $response);
25+
1926
/**
2027
* @return \Swis\JsonApi\Client\Interfaces\DataInterface
2128
*/
2229
public function getData();
2330

31+
/**
32+
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
33+
*
34+
* @return $this
35+
*/
36+
public function setData(DataInterface $data);
37+
2438
/**
2539
* @return \Swis\JsonApi\Client\ErrorCollection
2640
*/
2741
public function getErrors(): ErrorCollection;
2842

43+
/**
44+
* @param \Swis\JsonApi\Client\ErrorCollection $errors
45+
*
46+
* @return $this
47+
*/
48+
public function setErrors(ErrorCollection $errors);
49+
2950
/**
3051
* @return bool
3152
*/
@@ -36,21 +57,49 @@ public function hasErrors(): bool;
3657
*/
3758
public function getMeta(): ? Meta;
3859

60+
/**
61+
* @param \Swis\JsonApi\Client\Meta|null $meta
62+
*
63+
* @return $this
64+
*/
65+
public function setMeta(? Meta $meta);
66+
3967
/**
4068
* @return \Swis\JsonApi\Client\Links|null
4169
*/
4270
public function getLinks(): ? Links;
4371

72+
/**
73+
* @param \Swis\JsonApi\Client\Links|null $links
74+
*
75+
* @return $this
76+
*/
77+
public function setLinks(? Links $links);
78+
4479
/**
4580
* @return mixed
4681
*/
4782
public function getIncluded(): Collection;
4883

84+
/**
85+
* @param \Swis\JsonApi\Client\Collection $included
86+
*
87+
* @return $this
88+
*/
89+
public function setIncluded(Collection $included);
90+
4991
/**
5092
* @return \Swis\JsonApi\Client\Jsonapi|null
5193
*/
5294
public function getJsonapi(): ? Jsonapi;
5395

96+
/**
97+
* @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi
98+
*
99+
* @return $this
100+
*/
101+
public function setJsonapi(? Jsonapi $jsonapi);
102+
54103
/**
55104
* @return array
56105
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Swis\JsonApi\Client\Interfaces;
4+
5+
interface DocumentParserInterface
6+
{
7+
/**
8+
* @param string $json
9+
*
10+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
11+
*/
12+
public function parse(string $json): DocumentInterface;
13+
}

src/Interfaces/ParserInterface.php

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

0 commit comments

Comments
 (0)