1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Sdk\Core; |
4
|
|
|
|
5
|
|
|
use Ipag\Sdk\Exception\HttpClientException; |
6
|
|
|
use Ipag\Sdk\Http\Response; |
7
|
|
|
use Ipag\Sdk\IO\SerializerInterface; |
8
|
|
|
use Ipag\Sdk\Path\CompositePathInterface; |
9
|
|
|
use Ipag\Sdk\Util\ArrayUtil; |
10
|
|
|
use Ipag\Sdk\Util\PathUtil; |
11
|
|
|
|
12
|
|
|
abstract class Endpoint implements CompositePathInterface |
13
|
|
|
{ |
14
|
|
|
protected Client $client; |
15
|
|
|
protected CompositePathInterface $parent; |
16
|
|
|
protected ?SerializerInterface $serializer; |
17
|
|
|
protected string $location; |
18
|
|
|
|
19
|
|
|
public function __construct(Client $client, CompositePathInterface $parent, ?string $location = null, ?SerializerInterface $serializer = null) |
20
|
|
|
{ |
21
|
|
|
$this->client = $client; |
22
|
|
|
$this->parent = $parent; |
23
|
|
|
$this->location = $this->location ?? $location; |
24
|
|
|
$this->serializer = $serializer; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// |
28
|
|
|
|
29
|
|
|
protected function _GET(array $query = [], array $header = [], ?string $relativeUrl = null): Response |
30
|
|
|
{ |
31
|
|
|
return $this->request(__FUNCTION__, null, $query, $header, $relativeUrl); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function _POST($body, array $query = [], array $header = [], ?string $relativeUrl = null): Response |
35
|
|
|
{ |
36
|
|
|
return $this->request(__FUNCTION__, $body, $query, $header, $relativeUrl); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function _PUT($body, array $query = [], array $header = [], ?string $relativeUrl = null): Response |
40
|
|
|
{ |
41
|
|
|
return $this->request(__FUNCTION__, $body, $query, $header, $relativeUrl); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
protected function _PATCH($body, array $query = [], array $header = [], ?string $relativeUrl = null): Response |
45
|
|
|
{ |
46
|
|
|
return $this->request(__FUNCTION__, $body, $query, $header, $relativeUrl); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function _DELETE(array $query = [], array $header = [], ?string $relativeUrl = null): Response |
50
|
|
|
{ |
51
|
|
|
return $this->request(__FUNCTION__, null, $query, $header, $relativeUrl); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function _HEAD(array $query = [], array $header = [], ?string $relativeUrl = null): Response |
55
|
|
|
{ |
56
|
|
|
return $this->request(__FUNCTION__, null, $query, $header, $relativeUrl); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// |
60
|
|
|
|
61
|
|
|
protected function request(string $method, $body, array $query = [], array $header = [], ?string $relativeUrl = null): Response |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
return $this->client->request( |
65
|
|
|
strtoupper(substr($method, 1)), |
66
|
|
|
$relativeUrl ? $this->joinPath($relativeUrl) : $this->getPath(), |
67
|
|
|
$body, |
68
|
|
|
$query, |
69
|
|
|
$header, |
70
|
|
|
$this->serializer |
71
|
|
|
); |
72
|
|
|
} catch (HttpClientException $e) { |
73
|
|
|
|
74
|
|
|
$errorsSanitized = $this->sanitizeErrorMessage($e->getResponse()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->exceptionThrown( |
77
|
|
|
new HttpClientException( |
78
|
|
|
'response message: ' . json_encode(implode(' | ', $errorsSanitized)) . " (status code: {$e->getCode()})", |
|
|
|
|
79
|
|
|
$e->getCode(), |
80
|
|
|
$e, |
81
|
|
|
$e->getResponse(), |
82
|
|
|
null, |
83
|
|
|
null, |
84
|
|
|
$errorsSanitized |
85
|
|
|
) |
86
|
|
|
); |
|
|
|
|
87
|
|
|
|
88
|
|
|
} catch (\Throwable $th) { |
89
|
|
|
$this->exceptionThrown($th); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// |
94
|
|
|
|
95
|
|
|
public function getParent(): ?CompositePathInterface |
96
|
|
|
{ |
97
|
|
|
return $this->parent; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setParent(?CompositePathInterface $parent): void |
101
|
|
|
{ |
102
|
|
|
$this->parent = $parent; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getPath(): string |
106
|
|
|
{ |
107
|
|
|
return $this->getParent()->joinPath($this->location); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function joinPath(string $relative): string |
111
|
|
|
{ |
112
|
|
|
return implode(PathUtil::PATH_SEPARATOR, [$this->getPath(), ltrim($relative, PathUtil::PATH_SEPARATOR)]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// |
116
|
|
|
|
117
|
|
|
public static function make(Client $client, CompositePathInterface $parent, ?string $location = null, ?SerializerInterface $serializer = null) |
118
|
|
|
{ |
119
|
|
|
return new static($client, $parent, $location, $serializer); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function exceptionThrown(\Throwable $e): void |
123
|
|
|
{ |
124
|
|
|
throw $e; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function sanitizeErrorMessage(Response $response): ?array |
128
|
|
|
{ |
129
|
|
|
$responseData = $response->getParsedPath('message'); |
130
|
|
|
|
131
|
|
|
if (is_string($responseData)) |
|
|
|
|
132
|
|
|
return [$responseData]; |
133
|
|
|
|
134
|
|
|
if (is_array($responseData)) |
135
|
|
|
return ArrayUtil::extractStrings($responseData); |
136
|
|
|
|
137
|
|
|
$responseData = $response->getParsedPath('error'); |
138
|
|
|
|
139
|
|
|
if (is_array($responseData)) |
140
|
|
|
return ArrayUtil::extractStrings($responseData); |
141
|
|
|
|
142
|
|
|
$responseData = $response->getParsedPath('data'); |
143
|
|
|
|
144
|
|
|
if (is_array($responseData)) |
145
|
|
|
return ArrayUtil::extractStrings($responseData); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |