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

Refactor parser and hydrator #54

Merged
merged 8 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

* Added `DocumentFactory`.
* Added facades for `DocumentFactory`, `ItemHydrator` and `TypeMapper`.
* Added facades for `DocumentFactory`, `DocumentParser`, `ItemHydrator`, `ResponseParser` and `TypeMapper`.
* Added `DocumentParserInterface` and `ResponseParserInterface` interfaces and implementations.

### Changed

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

### Removed

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

## [0.18.0] - 2019-07-01

Expand Down
5 changes: 5 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,11 @@ If you register your own service provider and use package auto discover, don't f
```


## Advanced usage

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.


## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
],
"aliases":{
"DocumentFactory": "Swis\\JsonApi\\Client\\Facades\\DocumentFactoryFacade",
"DocumentParser": "Swis\\JsonApi\\Client\\Facades\\DocumentParserFacade",
"ItemHydrator": "Swis\\JsonApi\\Client\\Facades\\ItemHydratorFacade",
"ResponseParser": "Swis\\JsonApi\\Client\\Facades\\ResponseParserFacade",
"TypeMapper": "Swis\\JsonApi\\Client\\Facades\\TypeMapperFacade"
}
}
Expand Down
20 changes: 16 additions & 4 deletions src/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public function getResponse(): ? ResponseInterface

/**
* @param \Psr\Http\Message\ResponseInterface|null $response
*
* @return $this
*/
public function setResponse(ResponseInterface $response = null)
public function setResponse(? ResponseInterface $response)
{
$this->response = $response;

return $this;
}

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

/**
* @param \Swis\JsonApi\Client\ErrorCollection $errors
*
* @return $this
*/
public function setErrors(ErrorCollection $errors)
{
$this->errors = $errors;

return $this;
}

/**
Expand Down Expand Up @@ -103,7 +111,7 @@ public function getIncluded(): Collection
/**
* @param \Swis\JsonApi\Client\Collection $included
*
* @return static
* @return $this
*/
public function setIncluded(Collection $included)
{
Expand All @@ -122,10 +130,14 @@ public function getJsonapi(): ? Jsonapi

/**
* @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi
*
* @return $this
*/
public function setJsonapi(Jsonapi $jsonapi = null)
public function setJsonapi(? Jsonapi $jsonapi)
JaZo marked this conversation as resolved.
Show resolved Hide resolved
{
$this->jsonapi = $jsonapi;

return $this;
}

/**
Expand All @@ -139,7 +151,7 @@ public function getData()
/**
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
*
* @return static
* @return $this
*/
public function setData(DataInterface $data)
{
Expand Down
12 changes: 6 additions & 6 deletions src/DocumentClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface;
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface;
use Swis\JsonApi\Client\Interfaces\ParserInterface;
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;

class DocumentClient implements DocumentClientInterface
{
Expand All @@ -17,15 +17,15 @@ class DocumentClient implements DocumentClientInterface
private $client;

/**
* @var \Swis\JsonApi\Client\Interfaces\ParserInterface
* @var \Swis\JsonApi\Client\Interfaces\ResponseParserInterface
*/
private $parser;

/**
* @param \Swis\JsonApi\Client\Interfaces\ClientInterface $client
* @param \Swis\JsonApi\Client\Interfaces\ParserInterface $parser
* @param \Swis\JsonApi\Client\Interfaces\ClientInterface $client
* @param \Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser
*/
public function __construct(ClientInterface $client, ParserInterface $parser)
public function __construct(ClientInterface $client, ResponseParserInterface $parser)
{
$this->client = $client;
$this->parser = $parser;
Expand Down Expand Up @@ -120,6 +120,6 @@ protected function sanitizeJson(string $json): string
*/
protected function parseResponse(ResponseInterface $response): DocumentInterface
{
return $this->parser->deserializeResponse($response);
return $this->parser->parse($response);
}
}
22 changes: 22 additions & 0 deletions src/Facades/DocumentParserFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Swis\JsonApi\Client\Facades;

use Illuminate\Support\Facades\Facade;
use Swis\JsonApi\Client\Interfaces\DocumentParserInterface;

/**
* @method static \Swis\JsonApi\Client\Interfaces\DocumentInterface parse(string $json)
*
* @see \Swis\JsonApi\Client\Interfaces\DocumentParserInterface
*/
class DocumentParserFacade extends Facade
{
/**
* {@inheritdoc}
*/
protected static function getFacadeAccessor()
{
return DocumentParserInterface::class;
}
}
22 changes: 22 additions & 0 deletions src/Facades/ResponseParserFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Swis\JsonApi\Client\Facades;

use Illuminate\Support\Facades\Facade;
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface;

/**
* @method static \Swis\JsonApi\Client\Interfaces\DocumentInterface parse(\Psr\Http\Message\ResponseInterface $response)
*
* @see \Swis\JsonApi\Client\Interfaces\ResponseParserInterface
*/
class ResponseParserFacade extends Facade
{
/**
* {@inheritdoc}
*/
protected static function getFacadeAccessor()
{
return ResponseParserInterface::class;
}
}
49 changes: 49 additions & 0 deletions src/Interfaces/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,37 @@ interface DocumentInterface extends \JsonSerializable
*/
public function getResponse(): ? ResponseInterface;

/**
* @param \Psr\Http\Message\ResponseInterface|null $response
*
* @return $this
*/
public function setResponse(? ResponseInterface $response);

/**
* @return \Swis\JsonApi\Client\Interfaces\DataInterface
*/
public function getData();

/**
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
*
* @return $this
*/
public function setData(DataInterface $data);

/**
* @return \Swis\JsonApi\Client\ErrorCollection
*/
public function getErrors(): ErrorCollection;

/**
* @param \Swis\JsonApi\Client\ErrorCollection $errors
*
* @return $this
*/
public function setErrors(ErrorCollection $errors);

/**
* @return bool
*/
Expand All @@ -36,21 +57,49 @@ public function hasErrors(): bool;
*/
public function getMeta(): ? Meta;

/**
* @param \Swis\JsonApi\Client\Meta|null $meta
*
* @return $this
*/
public function setMeta(? Meta $meta);

/**
* @return \Swis\JsonApi\Client\Links|null
*/
public function getLinks(): ? Links;

/**
* @param \Swis\JsonApi\Client\Links|null $links
*
* @return $this
*/
public function setLinks(? Links $links);

/**
* @return mixed
*/
public function getIncluded(): Collection;

/**
* @param \Swis\JsonApi\Client\Collection $included
*
* @return $this
*/
public function setIncluded(Collection $included);

/**
* @return \Swis\JsonApi\Client\Jsonapi|null
*/
public function getJsonapi(): ? Jsonapi;

/**
* @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi
*
* @return $this
*/
public function setJsonapi(? Jsonapi $jsonapi);

/**
* @return array
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Interfaces/DocumentParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Swis\JsonApi\Client\Interfaces;

interface DocumentParserInterface
{
/**
* @param string $json
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function parse(string $json): DocumentInterface;
}
35 changes: 0 additions & 35 deletions src/Interfaces/ParserInterface.php

This file was deleted.

15 changes: 15 additions & 0 deletions src/Interfaces/ResponseParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Swis\JsonApi\Client\Interfaces;

use Psr\Http\Message\ResponseInterface;

interface ResponseParserInterface
{
/**
* @param \Psr\Http\Message\ResponseInterface $response
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function parse(ResponseInterface $response): DocumentInterface;
}
Loading