Skip to content

Commit 3db172a

Browse files
committed
Split repository into traits
Closes #91
1 parent d20c349 commit 3db172a

File tree

11 files changed

+231
-128
lines changed

11 files changed

+231
-128
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
* The `Repository` is split up into one trait per action [#91](https://github.com/swisnl/json-api-client/issues/91). This allows you to build your own repository by extending the `BaseRepository` and including just the actions/traits you need. See the (updated) readme for usage instructions. The `Repository` now uses these new traits and should be fully backwards compatible.
12+
913
### Fixed
1014

1115
* Fix ParamNameMismatch issue [#96](https://github.com/swisnl/json-api-client/issues/96).

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,21 @@ class BlogRepository extends \Swis\JsonApi\Client\Repository
255255
}
256256
```
257257

258+
The above repository will have a method for all CRUD-actions. If you work with a read-only API and don't want to have all actions, you can build your own repository by extending `\Swis\JsonApi\Client\BaseRepository` and including just the actions/traits you need.
259+
260+
``` php
261+
use Swis\JsonApi\Client\Actions\FetchMany;
262+
use Swis\JsonApi\Client\Actions\FetchOne;
263+
264+
class BlogRepository extends \Swis\JsonApi\Client\BaseRepository
265+
{
266+
use FetchMany;
267+
use FetchOne;
268+
269+
protected $endpoint = 'blogs';
270+
}
271+
```
272+
258273
If this repository (pattern) doesn't fit your needs, you can create your own implementation using the [clients](#clients) provided by this package.
259274

260275
### Request parameters

src/Actions/Create.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
use Swis\JsonApi\Client\Interfaces\ItemInterface;
8+
9+
trait Create
10+
{
11+
/**
12+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
13+
* @param array $parameters
14+
*
15+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
16+
*/
17+
public function create(ItemInterface $item, array $parameters = [])
18+
{
19+
return $this->getClient()->post(
20+
$this->getEndpoint().'?'.http_build_query($parameters),
21+
$this->documentFactory->make($item)
22+
);
23+
}
24+
}

src/Actions/Delete.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
trait Delete
8+
{
9+
/**
10+
* @param string $id
11+
* @param array $parameters
12+
*
13+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
14+
*/
15+
public function delete(string $id, array $parameters = [])
16+
{
17+
return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
18+
}
19+
}

src/Actions/FetchMany.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
trait FetchMany
8+
{
9+
/**
10+
* @param array $parameters
11+
*
12+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
13+
*/
14+
public function all(array $parameters = [])
15+
{
16+
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
17+
}
18+
}

src/Actions/FetchOne.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
trait FetchOne
8+
{
9+
/**
10+
* @param string $id
11+
* @param array $parameters
12+
*
13+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
14+
*/
15+
public function find(string $id, array $parameters = [])
16+
{
17+
return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters));
18+
}
19+
}

src/Actions/Save.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
use Swis\JsonApi\Client\Interfaces\ItemInterface;
8+
9+
trait Save
10+
{
11+
use Create { create as protected saveNew; }
12+
use Update { update as protected saveExisting; }
13+
14+
/**
15+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
16+
* @param array $parameters
17+
*
18+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
19+
*/
20+
public function save(ItemInterface $item, array $parameters = [])
21+
{
22+
if ($item->isNew()) {
23+
return $this->saveNew($item, $parameters);
24+
}
25+
26+
return $this->saveExisting($item, $parameters);
27+
}
28+
}

src/Actions/TakeOne.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
trait TakeOne
8+
{
9+
/**
10+
* @param array $parameters
11+
*
12+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
13+
*/
14+
public function take(array $parameters = [])
15+
{
16+
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters));
17+
}
18+
}

src/Actions/Update.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client\Actions;
6+
7+
use Swis\JsonApi\Client\Interfaces\ItemInterface;
8+
9+
trait Update
10+
{
11+
/**
12+
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
13+
* @param array $parameters
14+
*
15+
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
16+
*/
17+
public function update(ItemInterface $item, array $parameters = [])
18+
{
19+
return $this->getClient()->patch(
20+
$this->getEndpoint().'/'.urlencode($item->getId()).'?'.http_build_query($parameters),
21+
$this->documentFactory->make($item)
22+
);
23+
}
24+
}

src/BaseRepository.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Swis\JsonApi\Client;
6+
7+
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface;
8+
9+
abstract class BaseRepository
10+
{
11+
/**
12+
* @var \Swis\JsonApi\Client\Interfaces\DocumentClientInterface
13+
*/
14+
protected $client;
15+
16+
/**
17+
* @var \Swis\JsonApi\Client\DocumentFactory
18+
*/
19+
protected $documentFactory;
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $endpoint = '';
25+
26+
/**
27+
* @param \Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client
28+
* @param \Swis\JsonApi\Client\DocumentFactory $documentFactory
29+
*/
30+
public function __construct(DocumentClientInterface $client, DocumentFactory $documentFactory)
31+
{
32+
$this->client = $client;
33+
$this->documentFactory = $documentFactory;
34+
}
35+
36+
/**
37+
* @return \Swis\JsonApi\Client\Interfaces\DocumentClientInterface
38+
*/
39+
public function getClient()
40+
{
41+
return $this->client;
42+
}
43+
44+
/**
45+
* @return string
46+
*/
47+
public function getEndpoint()
48+
{
49+
return $this->endpoint;
50+
}
51+
}

0 commit comments

Comments
 (0)