diff --git a/CHANGELOG.md b/CHANGELOG.md index 568c5b7..9ca335a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -* Nothing +### Changed + +* Change signature of `RepositoryInterface::save()`. It should now receive an `ItemInterface` instead of a `ItemDocumentInterface`. ## [0.19.0] - 2019-07-10 diff --git a/README.MD b/README.MD index 13df901..23d7119 100644 --- a/README.MD +++ b/README.MD @@ -253,13 +253,6 @@ $repository->all(['include' => 'author', 'page' => ['limit' => 15, 'offset' => 0 ``` -## DocumentFactory - -The `Repository` and `DocumentClient` both require `ItemDocumentInterface` instances when creating or updating resources. -Such documents can easily be created using the `DocumentFactory` by giving it a `DataInterface` instance. -This can be an `ItemInterface`, usually created by the [ItemHydrator](#itemhydrator), or a `Collection`. - - ## ItemHydrator The `ItemHydrator` can be used to fill/hydrate an item and its relations using an associative array with attributes. @@ -267,15 +260,15 @@ This is useful if you would like to hydrate an item with POST data from your req ``` php $typeMapper = app(Swis\JsonApi\Client\TypeMapper::class); -$itemDocumentBuilder = app(Swis\JsonApi\Client\ItemDocumentBuilder::class); +$itemHydrator = app(Swis\JsonApi\Client\ItemHydrator::class); $blogRepository = app(App\Repositiories\BlogRepository::class); -$itemDocument = $itemDocumentBuilder->build( +$item = $itemHydrator->hydrate( $typeMapper->getMapping('blog'), request()->all(['title', 'author', 'date', 'content', 'tags']), request()->id ); -$blogRepository->save($itemDocument); +$blogRepository->save($item); ``` ### Relations @@ -300,7 +293,7 @@ $attributes = [ 56, ], ]; -$itemDocument = $itemDocumentBuilder->build($typeMapper->getMapping('blog'), $attributes); +$itemDocument = $itemHydrator->hydrate($typeMapper->getMapping('blog'), $attributes); echo json_encode($itemDocument, JSON_PRETTY_PRINT); @@ -376,6 +369,13 @@ It can take everything your request factory takes as input data and returns the It does not parse or validate the response or hydrate items! +## DocumentFactory + +The `DocumentClient` requires `ItemDocumentInterface` instances when creating or updating resources. +Such documents can easily be created using the `DocumentFactory` by giving it a `DataInterface` instance. +This can be an `ItemInterface`, usually created by the [ItemHydrator](#itemhydrator), or a `Collection`. + + ## Service Provider The `\Swis\JsonApi\Client\Providers\ServiceProvider` registers the `TypeMapper`, `JsonApi\Parser` and both clients; `Client` and `DocumentClient`. diff --git a/src/Interfaces/RepositoryInterface.php b/src/Interfaces/RepositoryInterface.php index ee592b2..5d8ffdf 100644 --- a/src/Interfaces/RepositoryInterface.php +++ b/src/Interfaces/RepositoryInterface.php @@ -17,11 +17,11 @@ public function all(); public function find(string $id); /** - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document + * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item * * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ - public function save(ItemDocumentInterface $document); + public function save(ItemInterface $item); /** * @param string $id diff --git a/src/Repository.php b/src/Repository.php index 38f43a7..573dd67 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -3,7 +3,7 @@ namespace Swis\JsonApi\Client; use Swis\JsonApi\Client\Interfaces\DocumentClientInterface; -use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface; +use Swis\JsonApi\Client\Interfaces\ItemInterface; use Swis\JsonApi\Client\Interfaces\RepositoryInterface; class Repository implements RepositoryInterface @@ -13,6 +13,11 @@ class Repository implements RepositoryInterface */ protected $client; + /** + * @var \Swis\JsonApi\Client\DocumentFactory + */ + protected $documentFactory; + /** * @var string */ @@ -20,10 +25,12 @@ class Repository implements RepositoryInterface /** * @param \Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client + * @param \Swis\JsonApi\Client\DocumentFactory $documentFactory */ - public function __construct(DocumentClientInterface $client) + public function __construct(DocumentClientInterface $client, DocumentFactory $documentFactory) { $this->client = $client; + $this->documentFactory = $documentFactory; } /** @@ -74,42 +81,45 @@ public function find(string $id, array $parameters = []) } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document - * @param array $parameters + * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item + * @param array $parameters * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function save(ItemDocumentInterface $document, array $parameters = []) + public function save(ItemInterface $item, array $parameters = []) { - if ($document->getData()->isNew()) { - return $this->saveNew($document, $parameters); + if ($item->isNew()) { + return $this->saveNew($item, $parameters); } - return $this->saveExisting($document, $parameters); + return $this->saveExisting($item, $parameters); } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document - * @param array $parameters + * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item + * @param array $parameters * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - protected function saveNew(ItemDocumentInterface $document, array $parameters = []) + protected function saveNew(ItemInterface $item, array $parameters = []) { - return $this->getClient()->post($this->getEndpoint().'?'.http_build_query($parameters), $document); + return $this->getClient()->post( + $this->getEndpoint().'?'.http_build_query($parameters), + $this->documentFactory->make($item) + ); } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document - * @param array $parameters + * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item + * @param array $parameters * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - protected function saveExisting(ItemDocumentInterface $document, array $parameters = []) + protected function saveExisting(ItemInterface $item, array $parameters = []) { return $this->getClient()->patch( - $this->getEndpoint().'/'.urlencode($document->getData()->getId()).'?'.http_build_query($parameters), - $document + $this->getEndpoint().'/'.urlencode($item->getId()).'?'.http_build_query($parameters), + $this->documentFactory->make($item) ); } diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index 63ceef9..6f8e649 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Swis\JsonApi\Client\Document; +use Swis\JsonApi\Client\DocumentFactory; use Swis\JsonApi\Client\Interfaces\DocumentClientInterface; use Swis\JsonApi\Client\Item; use Swis\JsonApi\Client\ItemDocument; @@ -18,7 +19,7 @@ public function it_can_get_the_client() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame($client, $repository->getClient()); } @@ -30,7 +31,7 @@ public function it_can_get_the_endpoint() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame('mocks', $repository->getEndpoint()); } @@ -50,7 +51,7 @@ public function it_can_get_all() ->with('mocks?foo=bar') ->willReturn($document); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame($document, $repository->all(['foo' => 'bar'])); } @@ -70,7 +71,7 @@ public function it_can_take_one() ->with('mocks?foo=bar') ->willReturn($document); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame($document, $repository->take(['foo' => 'bar'])); } @@ -90,7 +91,7 @@ public function it_can_find_one() ->with('mocks/1?foo=bar') ->willReturn($document); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame($document, $repository->find(1, ['foo' => 'bar'])); } @@ -111,9 +112,9 @@ public function it_can_save_new() ->with('mocks?foo=bar') ->willReturn($document); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->save($document, ['foo' => 'bar'])); + $this->assertSame($document, $repository->save(new Item(), ['foo' => 'bar'])); } /** @@ -132,9 +133,9 @@ public function it_can_save_existing() ->with('mocks/1?foo=bar') ->willReturn($document); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); - $this->assertSame($document, $repository->save($document, ['foo' => 'bar'])); + $this->assertSame($document, $repository->save((new Item())->setId(1), ['foo' => 'bar'])); } /** @@ -152,7 +153,7 @@ public function it_can_delete() ->with('mocks/1?foo=bar') ->willReturn($document); - $repository = new MockRepository($client); + $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame($document, $repository->delete(1, ['foo' => 'bar'])); }