1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client; |
6
|
|
|
|
7
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
8
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
9
|
|
|
use Psr\Http\Client\ClientInterface as HttpClientInterface; |
10
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
11
|
|
|
use Psr\Http\Message\RequestInterface; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
14
|
|
|
use Psr\Http\Message\StreamInterface; |
15
|
|
|
use Swis\JsonApi\Client\Interfaces\ClientInterface; |
16
|
|
|
|
17
|
|
|
class Client implements ClientInterface |
18
|
|
|
{ |
19
|
|
|
private HttpClientInterface $client; |
20
|
|
|
|
21
|
|
|
private RequestFactoryInterface $requestFactory; |
22
|
|
|
|
23
|
|
|
private StreamFactoryInterface $streamFactory; |
24
|
|
|
|
25
|
|
|
private string $baseUri = ''; |
26
|
|
|
|
27
|
|
|
private array $defaultHeaders = [ |
28
|
|
|
'Accept' => 'application/vnd.api+json', |
29
|
|
|
'Content-Type' => 'application/vnd.api+json', |
30
|
|
|
]; |
31
|
|
|
|
32
|
56 |
|
public function __construct( |
33
|
|
|
?HttpClientInterface $client = null, |
34
|
|
|
?RequestFactoryInterface $requestFactory = null, |
35
|
|
|
?StreamFactoryInterface $streamFactory = null, |
36
|
|
|
) { |
37
|
56 |
|
$this->client = $client ?: Psr18ClientDiscovery::find(); |
38
|
56 |
|
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); |
39
|
56 |
|
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); |
40
|
28 |
|
} |
41
|
|
|
|
42
|
4 |
|
public function getBaseUri(): string |
43
|
|
|
{ |
44
|
4 |
|
return $this->baseUri; |
45
|
|
|
} |
46
|
|
|
|
47
|
12 |
|
public function setBaseUri(string $baseUri): void |
48
|
|
|
{ |
49
|
12 |
|
$this->baseUri = $baseUri; |
50
|
6 |
|
} |
51
|
|
|
|
52
|
4 |
|
public function getDefaultHeaders(): array |
53
|
|
|
{ |
54
|
4 |
|
return $this->defaultHeaders; |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function setDefaultHeaders(array $defaultHeaders): void |
58
|
|
|
{ |
59
|
4 |
|
$this->defaultHeaders = $defaultHeaders; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
64
|
|
|
*/ |
65
|
12 |
|
public function get(string $endpoint, array $headers = []): ResponseInterface |
66
|
|
|
{ |
67
|
12 |
|
return $this->request('GET', $endpoint, null, $headers); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
72
|
|
|
* |
73
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
74
|
|
|
*/ |
75
|
4 |
|
public function post(string $endpoint, $body, array $headers = []): ResponseInterface |
76
|
|
|
{ |
77
|
4 |
|
return $this->request('POST', $endpoint, $body, $headers); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
82
|
|
|
* |
83
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
84
|
|
|
*/ |
85
|
4 |
|
public function patch(string $endpoint, $body, array $headers = []): ResponseInterface |
86
|
|
|
{ |
87
|
4 |
|
return $this->request('PATCH', $endpoint, $body, $headers); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
92
|
|
|
*/ |
93
|
4 |
|
public function delete(string $endpoint, array $headers = []): ResponseInterface |
94
|
|
|
{ |
95
|
4 |
|
return $this->request('DELETE', $endpoint, null, $headers); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
100
|
|
|
* |
101
|
|
|
* @throws \Psr\Http\Client\ClientExceptionInterface |
102
|
|
|
*/ |
103
|
44 |
|
public function request(string $method, string $endpoint, $body = null, array $headers = []): ResponseInterface |
104
|
|
|
{ |
105
|
44 |
|
return $this->client->sendRequest($this->buildRequest($method, $endpoint, $body, $headers)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string|resource|\Psr\Http\Message\StreamInterface|null $body |
110
|
|
|
*/ |
111
|
44 |
|
protected function buildRequest(string $method, string $endpoint, $body = null, array $headers = []): RequestInterface |
112
|
|
|
{ |
113
|
44 |
|
$request = $this->requestFactory->createRequest($method, $this->getEndpoint($endpoint)); |
114
|
|
|
|
115
|
44 |
|
if ($body !== null) { |
116
|
20 |
|
if (is_resource($body)) { |
117
|
4 |
|
$body = $this->streamFactory->createStreamFromResource($body); |
118
|
|
|
} |
119
|
20 |
|
if (! ($body instanceof StreamInterface)) { |
120
|
12 |
|
$body = $this->streamFactory->createStream($body); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
20 |
|
$request = $request->withBody($body); |
124
|
|
|
} |
125
|
|
|
|
126
|
44 |
|
foreach ($this->mergeHeaders($headers) as $name => $value) { |
127
|
44 |
|
$request = $request->withHeader($name, $value); |
128
|
|
|
} |
129
|
|
|
|
130
|
44 |
|
return $request; |
131
|
|
|
} |
132
|
|
|
|
133
|
44 |
|
protected function getEndpoint(string $endpoint): string |
134
|
|
|
{ |
135
|
44 |
|
if (strpos($endpoint, 'http://') === 0 || strpos($endpoint, 'https://') === 0) { |
136
|
4 |
|
return $endpoint; |
137
|
|
|
} |
138
|
|
|
|
139
|
40 |
|
return $this->baseUri.$endpoint; |
140
|
|
|
} |
141
|
|
|
|
142
|
44 |
|
protected function mergeHeaders(array $headers): array |
143
|
|
|
{ |
144
|
44 |
|
return array_merge($this->defaultHeaders, $headers); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|