1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swis\JsonApi\Client; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Client\ClientInterface as HttpClientInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Swis\JsonApi\Client\Interfaces\ClientInterface; |
8
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentClientInterface; |
9
|
|
|
use Swis\JsonApi\Client\Interfaces\DocumentInterface; |
10
|
|
|
use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface; |
11
|
|
|
use Swis\JsonApi\Client\Interfaces\ResponseParserInterface; |
12
|
|
|
use Swis\JsonApi\Client\Interfaces\TypeMapperInterface; |
13
|
|
|
use Swis\JsonApi\Client\Parsers\ResponseParser; |
14
|
|
|
|
15
|
|
|
class DocumentClient implements DocumentClientInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var \Swis\JsonApi\Client\Interfaces\ClientInterface |
19
|
|
|
*/ |
20
|
|
|
private $client; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var \Swis\JsonApi\Client\Interfaces\ResponseParserInterface |
24
|
|
|
*/ |
25
|
|
|
private $parser; |
26
|
|
|
|
27
|
|
|
/** |
28
|
45 |
|
* @param \Swis\JsonApi\Client\Interfaces\ClientInterface $client |
29
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser |
30
|
45 |
|
*/ |
31
|
45 |
|
public function __construct(ClientInterface $client, ResponseParserInterface $parser) |
32
|
45 |
|
{ |
33
|
|
|
$this->client = $client; |
34
|
|
|
$this->parser = $parser; |
35
|
|
|
} |
36
|
|
|
|
37
|
9 |
|
/** |
38
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper |
39
|
9 |
|
* @param \Psr\Http\Client\ClientInterface|null $client |
40
|
|
|
* |
41
|
|
|
* @return static |
42
|
|
|
*/ |
43
|
|
|
public static function create(TypeMapperInterface $typeMapper = null, HttpClientInterface $client = null): self |
44
|
|
|
{ |
45
|
9 |
|
return new static(new Client($client), ResponseParser::create($typeMapper)); |
46
|
|
|
} |
47
|
9 |
|
|
48
|
9 |
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getBaseUri(): string |
52
|
|
|
{ |
53
|
|
|
return $this->client->getBaseUri(); |
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
/** |
57
|
|
|
* @param string $baseUri |
58
|
9 |
|
*/ |
59
|
|
|
public function setBaseUri(string $baseUri): void |
60
|
|
|
{ |
61
|
|
|
$this->client->setBaseUri($baseUri); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $endpoint |
66
|
|
|
* @param array $headers |
67
|
|
|
* |
68
|
9 |
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
69
|
|
|
*/ |
70
|
9 |
|
public function get(string $endpoint, array $headers = []): DocumentInterface |
71
|
|
|
{ |
72
|
|
|
return $this->parseResponse($this->client->get($endpoint, $headers)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $endpoint |
77
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body |
78
|
|
|
* @param array $headers |
79
|
|
|
* |
80
|
9 |
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
81
|
|
|
*/ |
82
|
9 |
|
public function post(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface |
83
|
|
|
{ |
84
|
|
|
return $this->parseResponse($this->client->post($endpoint, $this->prepareBody($body), $headers)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $endpoint |
89
|
|
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body |
90
|
|
|
* @param array $headers |
91
|
9 |
|
* |
92
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
93
|
9 |
|
*/ |
94
|
|
|
public function patch(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface |
95
|
|
|
{ |
96
|
|
|
return $this->parseResponse($this->client->patch($endpoint, $this->prepareBody($body), $headers)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $endpoint |
101
|
18 |
|
* @param array $headers |
102
|
|
|
* |
103
|
18 |
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
104
|
|
|
*/ |
105
|
|
|
public function delete(string $endpoint, array $headers = []): DocumentInterface |
106
|
|
|
{ |
107
|
|
|
return $this->parseResponse($this->client->delete($endpoint, $headers)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
18 |
|
* @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body |
112
|
|
|
* |
113
|
18 |
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
protected function prepareBody(ItemDocumentInterface $body): string |
116
|
|
|
{ |
117
|
|
|
return $this->sanitizeJson(json_encode($body)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
36 |
|
* @param string $json |
122
|
|
|
* |
123
|
36 |
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
protected function sanitizeJson(string $json): string |
126
|
|
|
{ |
127
|
|
|
return str_replace('\r\n', '\\n', $json); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
132
|
|
|
* |
133
|
|
|
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface |
134
|
|
|
*/ |
135
|
|
|
protected function parseResponse(ResponseInterface $response): DocumentInterface |
136
|
|
|
{ |
137
|
|
|
return $this->parser->parse($response); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|