1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\JsonApi\Client; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Client\ClientInterface as HttpClientInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Swis\JsonApi\Client\Interfaces\ClientInterface; |
10
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface; |
11
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentInterface; |
12
|
|
|
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface; |
13
|
|
|
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface; |
14
|
|
|
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface; |
15
|
|
|
use Swis\JsonApi\Client\Parsers\ResponseParser; |
16
|
|
|
|
17
|
|
|
class DocumentClient implements DocumentClientInterface |
18
|
|
|
{ |
19
|
|
|
private ClientInterface $client; |
20
|
|
|
|
21
|
|
|
private ResponseParserInterface $parser; |
22
|
|
|
|
23
|
24 |
|
public function __construct(ClientInterface $client, ResponseParserInterface $parser) |
24
|
|
|
{ |
25
|
24 |
|
$this->client = $client; |
26
|
24 |
|
$this->parser = $parser; |
27
|
12 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
4 |
|
public static function create(?TypeMapperInterface $typeMapper = null, ?HttpClientInterface $client = null): self |
33
|
|
|
{ |
34
|
4 |
|
return new static(new Client($client), ResponseParser::create($typeMapper)); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
public function getBaseUri(): string |
38
|
|
|
{ |
39
|
4 |
|
return $this->client->getBaseUri(); |
40
|
|
|
} |
41
|
|
|
|
42
|
4 |
|
public function setBaseUri(string $baseUri): void |
43
|
|
|
{ |
44
|
4 |
|
$this->client->setBaseUri($baseUri); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
4 |
|
public function get(string $endpoint, array $headers = []): DocumentInterface |
48
|
|
|
{ |
49
|
4 |
|
return $this->parseResponse($this->client->get($endpoint, $headers)); |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
public function post(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface |
53
|
|
|
{ |
54
|
4 |
|
return $this->parseResponse($this->client->post($endpoint, $this->prepareBody($body), $headers)); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function patch(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface |
58
|
|
|
{ |
59
|
4 |
|
return $this->parseResponse($this->client->patch($endpoint, $this->prepareBody($body), $headers)); |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
public function delete(string $endpoint, array $headers = []): DocumentInterface |
63
|
|
|
{ |
64
|
4 |
|
return $this->parseResponse($this->client->delete($endpoint, $headers)); |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
protected function prepareBody(ItemDocumentInterface $body): string |
68
|
|
|
{ |
69
|
8 |
|
return $this->sanitizeJson(json_encode($body, JSON_THROW_ON_ERROR)); |
70
|
|
|
} |
71
|
|
|
|
72
|
8 |
|
protected function sanitizeJson(string $json): string |
73
|
|
|
{ |
74
|
8 |
|
return str_replace('\r\n', '\\n', $json); |
75
|
|
|
} |
76
|
|
|
|
77
|
16 |
|
protected function parseResponse(ResponseInterface $response): DocumentInterface |
78
|
|
|
{ |
79
|
16 |
|
return $this->parser->parse($response); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|