Skip to content

Commit 87643e9

Browse files
committed
Add factory methods for ResponseParser and DocumentParser
1 parent cc4a641 commit 87643e9

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

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/Parsers/DocumentParserTest.php

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

3030
class DocumentParserTest extends AbstractTest
3131
{
32+
/**
33+
* @test
34+
*/
35+
public function it_can_create_an_instance_using_a_factory_method()
36+
{
37+
$this->assertInstanceOf(DocumentParser::class, DocumentParser::create());
38+
}
39+
3240
/**
3341
* @test
3442
*/

tests/Parsers/ResponseParserTest.php

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

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

0 commit comments

Comments
 (0)