Skip to content

Commit aa0a821

Browse files
authored
Merge pull request #79 from swisnl/feature/parser-factories
Add factory methods for DocumentClient, ResponseParser and DocumentParser
2 parents 1357cac + 5d03da0 commit aa0a821

File tree

7 files changed

+105
-57
lines changed

7 files changed

+105
-57
lines changed

README.MD

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,20 @@ php artisan vendor:publish --provider="Swis\JsonApi\Client\Providers\ServiceProv
8686

8787
## Getting started
8888

89-
You can simply require the [DocumentClient](#documentclient) as a dependency and use it in your class.
90-
Alternatively, you can create a repository based on `\Swis\JsonApi\Client\Repository` and use that as a dependency:
89+
You can simply create an instance of [DocumentClient](#documentclient) and use it in your class.
90+
Alternatively, you can create a [repository](#repository).
9191

9292
``` php
93-
use Swis\JsonApi\Client\Repository
93+
use Swis\JsonApi\Client\DocumentClient;
9494

95-
class BlogRepository extends Repository
96-
{
97-
protected $endpoint = 'blogs';
98-
}
95+
$client = DocumentClient::create();
96+
$document = $client->get('https://cms.contentacms.io/api/recipes');
9997

100-
$repository = app(BlogRepository::class);
101-
$document = $repository->all();
102-
if ($document->hasErrors()) {
103-
// do something with errors
104-
} else {
105-
$blogs = $document->getData();
98+
/** @var \Swis\JsonApi\Client\Collection&\Swis\JsonApi\Client\Item[] $collection */
99+
$collection = $document->getData();
100+
101+
foreach ($collection as $item) {
102+
// Do stuff with the items
106103
}
107104
```
108105

src/DocumentClient.php

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

33
namespace Swis\JsonApi\Client;
44

5+
use Psr\Http\Client\ClientInterface as HttpClientInterface;
56
use Psr\Http\Message\ResponseInterface;
67
use Swis\JsonApi\Client\Interfaces\ClientInterface;
78
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface;
89
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
910
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface;
1011
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;
12+
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
13+
use Swis\JsonApi\Client\Parsers\ResponseParser;
1114

1215
class DocumentClient implements DocumentClientInterface
1316
{
@@ -31,6 +34,17 @@ public function __construct(ClientInterface $client, ResponseParserInterface $pa
3134
$this->parser = $parser;
3235
}
3336

37+
/**
38+
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
39+
* @param \Psr\Http\Client\ClientInterface|null $client
40+
*
41+
* @return static
42+
*/
43+
public static function create(TypeMapperInterface $typeMapper = null, HttpClientInterface $client = null): self
44+
{
45+
return new static(new Client($client), ResponseParser::create($typeMapper));
46+
}
47+
3448
/**
3549
* @return string
3650
*/

src/Parsers/DocumentParser.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Swis\JsonApi\Client\Interfaces\ItemInterface;
1212
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
1313
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
14+
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
1415
use Swis\JsonApi\Client\ItemDocument;
16+
use Swis\JsonApi\Client\TypeMapper;
1517

1618
class DocumentParser implements DocumentParserInterface
1719
{
@@ -69,6 +71,29 @@ public function __construct(
6971
$this->metaParser = $metaParser;
7072
}
7173

74+
/**
75+
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
76+
*
77+
* @return static
78+
*/
79+
public static function create(TypeMapperInterface $typeMapper = null): self
80+
{
81+
$metaParser = new MetaParser();
82+
$linksParser = new LinksParser($metaParser);
83+
$itemParser = new ItemParser($typeMapper ?? new TypeMapper(), $linksParser, $metaParser);
84+
85+
return new static(
86+
$itemParser,
87+
new CollectionParser($itemParser),
88+
new ErrorCollectionParser(
89+
new ErrorParser($linksParser, $metaParser)
90+
),
91+
$linksParser,
92+
new JsonapiParser($metaParser),
93+
$metaParser
94+
);
95+
}
96+
7297
/**
7398
* @param string $json
7499
*

src/Parsers/ResponseParser.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
88
use Swis\JsonApi\Client\Interfaces\DocumentParserInterface;
99
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;
10+
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
1011
use Swis\JsonApi\Client\InvalidResponseDocument;
1112

1213
class ResponseParser implements ResponseParserInterface
@@ -24,6 +25,16 @@ public function __construct(DocumentParserInterface $parser)
2425
$this->parser = $parser;
2526
}
2627

28+
/**
29+
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
30+
*
31+
* @return static
32+
*/
33+
public static function create(TypeMapperInterface $typeMapper = null): self
34+
{
35+
return new static(DocumentParser::create($typeMapper));
36+
}
37+
2738
/**
2839
* @param \Psr\Http\Message\ResponseInterface $response
2940
*

tests/DocumentClientTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
class DocumentClientTest extends AbstractTest
1414
{
15+
/**
16+
* @test
17+
*/
18+
public function it_can_create_an_instance_using_a_factory_method()
19+
{
20+
$this->assertInstanceOf(DocumentClient::class, DocumentClient::create());
21+
}
22+
1523
/**
1624
* @test
1725
*/

tests/Parsers/DocumentParserTest.php

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
use Swis\JsonApi\Client\Link;
1515
use Swis\JsonApi\Client\Links;
1616
use Swis\JsonApi\Client\Meta;
17-
use Swis\JsonApi\Client\Parsers\CollectionParser;
1817
use Swis\JsonApi\Client\Parsers\DocumentParser;
19-
use Swis\JsonApi\Client\Parsers\ErrorCollectionParser;
20-
use Swis\JsonApi\Client\Parsers\ErrorParser;
21-
use Swis\JsonApi\Client\Parsers\ItemParser;
22-
use Swis\JsonApi\Client\Parsers\JsonapiParser;
23-
use Swis\JsonApi\Client\Parsers\LinksParser;
24-
use Swis\JsonApi\Client\Parsers\MetaParser;
2518
use Swis\JsonApi\Client\Tests\AbstractTest;
2619
use Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem;
2720
use Swis\JsonApi\Client\Tests\Mocks\Items\MasterItem;
2821
use Swis\JsonApi\Client\TypeMapper;
2922

3023
class DocumentParserTest extends AbstractTest
3124
{
25+
/**
26+
* @test
27+
*/
28+
public function it_can_create_an_instance_using_a_factory_method()
29+
{
30+
$this->assertInstanceOf(DocumentParser::class, DocumentParser::create());
31+
}
32+
3233
/**
3334
* @test
3435
*/
3536
public function it_converts_jsondocument_to_document()
3637
{
37-
$parser = $this->getDocumentParser();
38+
$parser = DocumentParser::create();
3839
$document = $parser->parse(
3940
json_encode(
4041
[
@@ -51,7 +52,7 @@ public function it_converts_jsondocument_to_document()
5152
*/
5253
public function it_throws_when_json_is_not_valid()
5354
{
54-
$parser = $this->getDocumentParser();
55+
$parser = DocumentParser::create();
5556

5657
$this->expectException(ValidationException::class);
5758
$this->expectExceptionMessage('Unable to parse JSON data: Malformed UTF-8 characters, possibly incorrectly encoded');
@@ -67,7 +68,7 @@ public function it_throws_when_json_is_not_valid()
6768
*/
6869
public function it_throws_when_json_is_not_a_jsonapi_document(string $invalidJson)
6970
{
70-
$parser = $this->getDocumentParser();
71+
$parser = DocumentParser::create();
7172

7273
$this->expectException(ValidationException::class);
7374
$this->expectExceptionMessage(sprintf('Document MUST be an object, "%s" given.', gettype(json_decode($invalidJson, false))));
@@ -92,7 +93,7 @@ public function provideInvalidJson(): array
9293
*/
9394
public function it_throws_when_data_errors_and_meta_are_missing()
9495
{
95-
$parser = $this->getDocumentParser();
96+
$parser = DocumentParser::create();
9697

9798
$this->expectException(ValidationException::class);
9899
$this->expectExceptionMessage('Document MUST contain at least one of the following properties: `data`, `errors`, `meta`.');
@@ -105,7 +106,7 @@ public function it_throws_when_data_errors_and_meta_are_missing()
105106
*/
106107
public function it_throws_when_both_data_and_errors_are_present()
107108
{
108-
$parser = $this->getDocumentParser();
109+
$parser = DocumentParser::create();
109110

110111
$this->expectException(ValidationException::class);
111112
$this->expectExceptionMessage('The properties `data` and `errors` MUST NOT coexist in Document.');
@@ -118,7 +119,7 @@ public function it_throws_when_both_data_and_errors_are_present()
118119
*/
119120
public function it_throws_when_included_is_present_but_data_is_not()
120121
{
121-
$parser = $this->getDocumentParser();
122+
$parser = DocumentParser::create();
122123

123124
$this->expectException(ValidationException::class);
124125
$this->expectExceptionMessage('If Document does not contain a `data` property, the `included` property MUST NOT be present either.');
@@ -134,7 +135,7 @@ public function it_throws_when_included_is_present_but_data_is_not()
134135
*/
135136
public function it_throws_when_data_is_not_an_array_object_or_null($invalidData)
136137
{
137-
$parser = $this->getDocumentParser();
138+
$parser = DocumentParser::create();
138139

139140
$this->expectException(ValidationException::class);
140141
$this->expectExceptionMessage(sprintf('Document property "data" MUST be null, an array or an object, "%s" given.', gettype(json_decode($invalidData, false)->data)));
@@ -160,7 +161,7 @@ public function provideInvalidData(): array
160161
*/
161162
public function it_throws_when_included_is_not_an_array($invalidIncluded)
162163
{
163-
$parser = $this->getDocumentParser();
164+
$parser = DocumentParser::create();
164165

165166
$this->expectException(ValidationException::class);
166167
$this->expectExceptionMessage(sprintf('Document property "included" MUST be an array, "%s" given.', gettype(json_decode($invalidIncluded, false)->included)));
@@ -185,7 +186,7 @@ public function provideInvalidIncluded(): array
185186
*/
186187
public function it_throws_when_it_finds_duplicate_resources()
187188
{
188-
$parser = $this->getDocumentParser();
189+
$parser = DocumentParser::create();
189190

190191
$this->expectException(ValidationException::class);
191192
$this->expectExceptionMessage('Resources MUST be unique based on their `type` and `id`, 1 duplicate(s) found.');
@@ -221,7 +222,7 @@ public function it_throws_when_it_finds_duplicate_resources()
221222
*/
222223
public function it_parses_a_resource_document()
223224
{
224-
$parser = $this->getDocumentParser();
225+
$parser = DocumentParser::create();
225226
$document = $parser->parse(
226227
json_encode(
227228
[
@@ -247,7 +248,7 @@ public function it_parses_a_resource_document()
247248
*/
248249
public function it_parses_a_resource_collection_document()
249250
{
250-
$parser = $this->getDocumentParser();
251+
$parser = DocumentParser::create();
251252
$document = $parser->parse(
252253
json_encode(
253254
[
@@ -276,7 +277,7 @@ public function it_parses_a_resource_collection_document()
276277
*/
277278
public function it_parses_a_document_without_data()
278279
{
279-
$parser = $this->getDocumentParser();
280+
$parser = DocumentParser::create();
280281
$document = $parser->parse(
281282
json_encode(
282283
[
@@ -295,7 +296,7 @@ public function it_parses_a_document_without_data()
295296
*/
296297
public function it_parses_included()
297298
{
298-
$parser = $this->getDocumentParser();
299+
$parser = DocumentParser::create();
299300
$document = $parser->parse(
300301
json_encode(
301302
[
@@ -328,7 +329,7 @@ public function it_links_singular_relations_to_items_from_included()
328329
$typeMapper = new TypeMapper();
329330
$typeMapper->setMapping('master', MasterItem::class);
330331
$typeMapper->setMapping('child', ChildItem::class);
331-
$parser = $this->getDocumentParser($typeMapper);
332+
$parser = DocumentParser::create($typeMapper);
332333

333334
$document = $parser->parse(
334335
json_encode(
@@ -374,7 +375,7 @@ public function it_does_not_link_empty_singular_relations()
374375
$typeMapper = new TypeMapper();
375376
$typeMapper->setMapping('master', MasterItem::class);
376377
$typeMapper->setMapping('child', ChildItem::class);
377-
$parser = $this->getDocumentParser($typeMapper);
378+
$parser = DocumentParser::create($typeMapper);
378379

379380
$document = $parser->parse(
380381
json_encode(
@@ -417,7 +418,7 @@ public function it_links_plural_relations_to_items_from_included()
417418
$typeMapper = new TypeMapper();
418419
$typeMapper->setMapping('master', MasterItem::class);
419420
$typeMapper->setMapping('child', ChildItem::class);
420-
$parser = $this->getDocumentParser($typeMapper);
421+
$parser = DocumentParser::create($typeMapper);
421422

422423
$document = $parser->parse(
423424
json_encode(
@@ -478,7 +479,7 @@ public function it_does_not_link_empty_plural_relations()
478479
$typeMapper = new TypeMapper();
479480
$typeMapper->setMapping('master', MasterItem::class);
480481
$typeMapper->setMapping('child', ChildItem::class);
481-
$parser = $this->getDocumentParser($typeMapper);
482+
$parser = DocumentParser::create($typeMapper);
482483

483484
$document = $parser->parse(
484485
json_encode(
@@ -527,7 +528,7 @@ public function it_does_not_link_empty_plural_relations()
527528
*/
528529
public function it_parses_links()
529530
{
530-
$parser = $this->getDocumentParser();
531+
$parser = DocumentParser::create();
531532

532533
$document = $parser->parse(
533534
json_encode(
@@ -550,7 +551,7 @@ public function it_parses_links()
550551
*/
551552
public function it_parses_errors()
552553
{
553-
$parser = $this->getDocumentParser();
554+
$parser = DocumentParser::create();
554555

555556
$document = $parser->parse(
556557
json_encode(
@@ -575,7 +576,7 @@ public function it_parses_errors()
575576
*/
576577
public function it_parses_meta()
577578
{
578-
$parser = $this->getDocumentParser();
579+
$parser = DocumentParser::create();
579580

580581
$document = $parser->parse(
581582
json_encode(
@@ -598,7 +599,7 @@ public function it_parses_meta()
598599
*/
599600
public function it_parses_jsonapi()
600601
{
601-
$parser = $this->getDocumentParser();
602+
$parser = DocumentParser::create();
602603

603604
$document = $parser->parse(
604605
json_encode(
@@ -615,20 +616,4 @@ public function it_parses_jsonapi()
615616

616617
static::assertEquals(new Jsonapi('1.0'), $document->getJsonapi());
617618
}
618-
619-
private function getDocumentParser(TypeMapper $typeMapper = null): DocumentParser
620-
{
621-
$metaParser = new MetaParser();
622-
$linksParser = new LinksParser($metaParser);
623-
$itemParser = new ItemParser($typeMapper ?? new TypeMapper(), $linksParser, $metaParser);
624-
625-
return new DocumentParser(
626-
$itemParser,
627-
new CollectionParser($itemParser),
628-
new ErrorCollectionParser(new ErrorParser($linksParser, $metaParser)),
629-
$linksParser,
630-
new JsonapiParser($metaParser),
631-
$metaParser
632-
);
633-
}
634619
}

0 commit comments

Comments
 (0)