1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client; |
4
|
|
|
|
5
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface; |
6
|
|
|
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface; |
7
|
|
|
use Swis\JsonApi\Client\Interfaces\RepositoryInterface; |
8
|
|
|
|
9
|
|
|
class Repository implements RepositoryInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Swis\JsonApi\Client\Interfaces\DocumentClientInterface |
13
|
|
|
*/ |
14
|
|
|
protected $client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $endpoint = ''; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client |
23
|
|
|
*/ |
24
|
40 |
|
public function __construct(DocumentClientInterface $client) |
25
|
|
|
{ |
26
|
40 |
|
$this->client = $client; |
27
|
40 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentClientInterface |
31
|
|
|
*/ |
32
|
35 |
|
public function getClient() |
33
|
|
|
{ |
34
|
35 |
|
return $this->client; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
35 |
|
public function getEndpoint() |
41
|
|
|
{ |
42
|
35 |
|
return $this->endpoint; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $parameters |
47
|
|
|
* |
48
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
49
|
|
|
*/ |
50
|
5 |
|
public function all(array $parameters = []) |
51
|
|
|
{ |
52
|
5 |
|
return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $id |
57
|
|
|
* @param array $parameters |
58
|
|
|
* |
59
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
60
|
|
|
*/ |
61
|
5 |
|
public function find($id, array $parameters = []) |
62
|
|
|
{ |
63
|
5 |
|
return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document |
68
|
|
|
* @param array $parameters |
69
|
|
|
* |
70
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
71
|
|
|
*/ |
72
|
10 |
|
public function save(ItemDocumentInterface $document, array $parameters = []) |
73
|
|
|
{ |
74
|
10 |
|
if ($document->getData()->isNew()) { |
75
|
5 |
|
return $this->saveNew($document, $parameters); |
76
|
|
|
} |
77
|
|
|
|
78
|
5 |
|
return $this->saveExisting($document, $parameters); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document |
83
|
|
|
* @param array $parameters |
84
|
|
|
* |
85
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
86
|
|
|
*/ |
87
|
5 |
|
protected function saveNew(ItemDocumentInterface $document, array $parameters = []) |
88
|
|
|
{ |
89
|
5 |
|
return $this->getClient()->post($this->getEndpoint().'?'.http_build_query($parameters), $document); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document |
94
|
|
|
* @param array $parameters |
95
|
|
|
* |
96
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
97
|
|
|
*/ |
98
|
5 |
|
protected function saveExisting(ItemDocumentInterface $document, array $parameters = []) |
99
|
|
|
{ |
100
|
5 |
|
return $this->getClient()->patch( |
101
|
5 |
|
$this->getEndpoint().'/'.urlencode($document->getData()->getId()).'?'.http_build_query($parameters), |
102
|
5 |
|
$document |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document |
108
|
|
|
* @param array $parameters |
109
|
|
|
* |
110
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
111
|
|
|
*/ |
112
|
5 |
|
public function delete(ItemDocumentInterface $document, array $parameters = []) |
113
|
|
|
{ |
114
|
5 |
|
return $this->deleteById($document->getData()->getId(), $parameters); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param $id |
119
|
|
|
* @param array $parameters |
120
|
|
|
* |
121
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
122
|
|
|
*/ |
123
|
10 |
|
public function deleteById($id, array $parameters = []) |
124
|
|
|
{ |
125
|
10 |
|
return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|