Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add factory methods for DocumentClient, ResponseParser and DocumentParser #79

Merged
merged 4 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Parsers/DocumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Swis\JsonApi\Client\Interfaces\ItemInterface;
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
use Swis\JsonApi\Client\ItemDocument;
use Swis\JsonApi\Client\TypeMapper;

class DocumentParser implements DocumentParserInterface
{
Expand Down Expand Up @@ -69,6 +71,29 @@ public function __construct(
$this->metaParser = $metaParser;
}

/**
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
*
* @return static
*/
public static function create(TypeMapperInterface $typeMapper = null): self
{
$metaParser = new MetaParser();
$linksParser = new LinksParser($metaParser);
$itemParser = new ItemParser($typeMapper ?? new TypeMapper(), $linksParser, $metaParser);

return new static(
$itemParser,
new CollectionParser($itemParser),
new ErrorCollectionParser(
new ErrorParser($linksParser, $metaParser)
),
$linksParser,
new JsonapiParser($metaParser),
$metaParser
);
}

/**
* @param string $json
*
Expand Down
11 changes: 11 additions & 0 deletions src/Parsers/ResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
use Swis\JsonApi\Client\Interfaces\DocumentParserInterface;
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface;
use Swis\JsonApi\Client\InvalidResponseDocument;

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

/**
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper
*
* @return static
*/
public static function create(TypeMapperInterface $typeMapper = null): self
{
return new static(DocumentParser::create($typeMapper));
}

/**
* @param \Psr\Http\Message\ResponseInterface $response
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Parsers/DocumentParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@

class DocumentParserTest extends AbstractTest
{
/**
* @test
*/
public function it_can_create_an_instance_using_a_factory_method()
{
$this->assertInstanceOf(DocumentParser::class, DocumentParser::create());
}

/**
* @test
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Parsers/ResponseParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

class ResponseParserTest extends AbstractTest
{
/**
* @test
*/
public function it_can_create_an_instance_using_a_factory_method()
{
$this->assertInstanceOf(ResponseParser::class, ResponseParser::create());
}

/**
* @test
*/
Expand Down