|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client; |
|
4
|
|
|
|
|
5
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
|
6
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
|
7
|
|
|
use Psr\Http\Client\ClientInterface as HttpClientInterface; |
|
8
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
12
|
|
|
use Psr\Http\Message\StreamInterface; |
|
13
|
|
|
use Swis\JsonApi\Client\Interfaces\ClientInterface; |
|
14
|
|
|
|
|
15
|
|
|
class Client implements ClientInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Psr\Http\Client\ClientInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var \Psr\Http\Message\RequestFactoryInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $requestFactory; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Psr\Http\Message\StreamFactoryInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $streamFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $baseUri = ''; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $defaultHeaders = [ |
|
41
|
|
|
'Accept' => 'application/vnd.api+json', |
|
42
|
|
|
'Content-Type' => 'application/vnd.api+json', |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \Psr\Http\Client\ClientInterface|null $client |
|
47
|
|
|
* @param \Psr\Http\Message\RequestFactoryInterface|null $requestFactory |
|
48
|
|
|
* @param \Psr\Http\Message\StreamFactoryInterface|null $streamFactory |
|
49
|
|
|
*/ |
|
50
|
77 |
|
public function __construct( |
|
51
|
|
|
HttpClientInterface $client = null, |
|
52
|
|
|
RequestFactoryInterface $requestFactory = null, |
|
53
|
|
|
StreamFactoryInterface $streamFactory = null |
|
54
|
|
|
) { |
|
55
|
77 |
|
$this->client = $client ?: Psr18ClientDiscovery::find(); |
|
56
|
77 |
|
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); |
|
57
|
77 |
|
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); |
|
58
|
77 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
7 |
|
public function getBaseUri(): string |
|
64
|
|
|
{ |
|
65
|
7 |
|
return $this->baseUri; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $baseUri |
|
70
|
|
|
*/ |
|
71
|
7 |
|
public function setBaseUri(string $baseUri): void |
|
72
|
|
|
{ |
|
73
|
7 |
|
$this->baseUri = $baseUri; |
|
74
|
7 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function getDefaultHeaders(): array |
|
80
|
|
|
{ |
|
81
|
7 |
|
return $this->defaultHeaders; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $defaultHeaders |
|
86
|
|
|
*/ |
|
87
|
7 |
|
public function setDefaultHeaders(array $defaultHeaders): void |
|
88
|
|
|
{ |
|
89
|
7 |
|
$this->defaultHeaders = $defaultHeaders; |
|
90
|
7 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $endpoint |
|
94
|
|
|
* @param array $headers |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
99
|
|
|
*/ |
|
100
|
7 |
|
public function get(string $endpoint, array $headers = []): ResponseInterface |
|
101
|
|
|
{ |
|
102
|
7 |
|
return $this->request('GET', $endpoint, null, $headers); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $endpoint |
|
107
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
|
108
|
|
|
* @param array $headers |
|
109
|
|
|
* |
|
110
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
|
111
|
|
|
* |
|
112
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
113
|
|
|
*/ |
|
114
|
7 |
|
public function post(string $endpoint, $body, array $headers = []): ResponseInterface |
|
115
|
|
|
{ |
|
116
|
7 |
|
return $this->request('POST', $endpoint, $body, $headers); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $endpoint |
|
121
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
|
122
|
|
|
* @param array $headers |
|
123
|
|
|
* |
|
124
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
|
125
|
|
|
* |
|
126
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
127
|
|
|
*/ |
|
128
|
7 |
|
public function patch(string $endpoint, $body, array $headers = []): ResponseInterface |
|
129
|
|
|
{ |
|
130
|
7 |
|
return $this->request('PATCH', $endpoint, $body, $headers); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $endpoint |
|
135
|
|
|
* @param array $headers |
|
136
|
|
|
* |
|
137
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
|
138
|
|
|
* |
|
139
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
140
|
|
|
*/ |
|
141
|
7 |
|
public function delete(string $endpoint, array $headers = []): ResponseInterface |
|
142
|
|
|
{ |
|
143
|
7 |
|
return $this->request('DELETE', $endpoint, null, $headers); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $method |
|
148
|
|
|
* @param string $endpoint |
|
149
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
|
150
|
|
|
* @param array $headers |
|
151
|
|
|
* |
|
152
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
|
153
|
|
|
* |
|
154
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
155
|
|
|
*/ |
|
156
|
63 |
|
public function request(string $method, string $endpoint, $body = null, array $headers = []): ResponseInterface |
|
157
|
|
|
{ |
|
158
|
63 |
|
return $this->client->sendRequest($this->buildRequest($method, $endpoint, $body, $headers)); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $method |
|
163
|
|
|
* @param string $endpoint |
|
164
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
|
165
|
|
|
* @param array $headers |
|
166
|
|
|
* |
|
167
|
|
|
* @return \Psr\Http\Message\RequestInterface |
|
168
|
|
|
*/ |
|
169
|
63 |
|
protected function buildRequest(string $method, string $endpoint, $body = null, array $headers = []): RequestInterface |
|
170
|
|
|
{ |
|
171
|
63 |
|
$request = $this->requestFactory->createRequest($method, $this->getEndpoint($endpoint)); |
|
172
|
|
|
|
|
173
|
63 |
|
if ($body !== null) { |
|
174
|
35 |
|
if (is_resource($body)) { |
|
175
|
7 |
|
$body = $this->streamFactory->createStreamFromResource($body); |
|
176
|
|
|
} |
|
177
|
35 |
|
if (!($body instanceof StreamInterface)) { |
|
178
|
21 |
|
$body = $this->streamFactory->createStream($body); |
|
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
35 |
|
$request = $request->withBody($body); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
63 |
|
foreach ($this->mergeHeaders($headers) as $name => $value) { |
|
185
|
63 |
|
$request = $request->withHeader($name, $value); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
63 |
|
return $request; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @param string $endpoint |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
63 |
|
protected function getEndpoint(string $endpoint): string |
|
197
|
|
|
{ |
|
198
|
63 |
|
return $this->baseUri.$endpoint; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param array $headers |
|
203
|
|
|
* |
|
204
|
|
|
* @return array |
|
205
|
|
|
*/ |
|
206
|
63 |
|
protected function mergeHeaders(array $headers): array |
|
207
|
|
|
{ |
|
208
|
63 |
|
return array_merge($this->defaultHeaders, $headers); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|