|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EasyHttp\LayerContracts\Common; |
|
4
|
|
|
|
|
5
|
|
|
use EasyHttp\LayerContracts\Contracts\HttpClientRequest; |
|
6
|
|
|
use EasyHttp\LayerContracts\Contracts\Request\HttpSecurityContext; |
|
7
|
|
|
|
|
8
|
|
|
class ClientRequest implements HttpClientRequest |
|
9
|
|
|
{ |
|
10
|
|
|
protected string $method; |
|
11
|
|
|
protected string $uri; |
|
12
|
|
|
protected array $headers = []; |
|
13
|
|
|
protected string $body = ''; |
|
14
|
|
|
protected array $json = []; |
|
15
|
|
|
protected array $query = []; |
|
16
|
|
|
protected ?HttpSecurityContext $securityContext = null; |
|
17
|
|
|
protected int $timeout = 10; |
|
18
|
|
|
protected array $basicAuth = []; |
|
19
|
|
|
protected bool $ssl = false; |
|
20
|
|
|
|
|
21
|
11 |
|
public function __construct(string $method, string $uri) |
|
22
|
|
|
{ |
|
23
|
11 |
|
$this->method = $method; |
|
24
|
11 |
|
$this->uri = $uri; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
10 |
|
public function getMethod(): string |
|
28
|
|
|
{ |
|
29
|
10 |
|
return $this->method; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
10 |
|
public function getUri(): string |
|
33
|
|
|
{ |
|
34
|
10 |
|
return $this->uri; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function getHeader(string $key) |
|
38
|
|
|
{ |
|
39
|
1 |
|
return $this->headers[$key] ?? null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public function getHeaders(): array |
|
43
|
|
|
{ |
|
44
|
1 |
|
return $this->headers; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function getBody(): string |
|
48
|
|
|
{ |
|
49
|
2 |
|
if (!$this->hasJson()) { |
|
50
|
1 |
|
return $this->body; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
$body = json_encode($this->json); |
|
54
|
|
|
|
|
55
|
1 |
|
if (!$body) { |
|
56
|
|
|
return ''; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $body; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
public function getJson(): array |
|
63
|
|
|
{ |
|
64
|
2 |
|
return $this->json; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function getQuery(): array |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->query; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function getTimeout(): int |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->timeout; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
public function getSecurityContext(): ?HttpSecurityContext |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->securityContext; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function getBasicAuth(): array |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->basicAuth; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function hasBody(): bool |
|
88
|
|
|
{ |
|
89
|
|
|
if (!$this->hasJson()) { |
|
90
|
|
|
return !empty($this->body); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$body = json_encode($this->json); |
|
94
|
|
|
|
|
95
|
|
|
return (bool) !empty($body); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
public function hasJson(): bool |
|
99
|
|
|
{ |
|
100
|
3 |
|
return !empty($this->json); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
public function hasQuery(): bool |
|
104
|
|
|
{ |
|
105
|
2 |
|
return !empty($this->query); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
public function hasHeaders(): bool |
|
109
|
|
|
{ |
|
110
|
2 |
|
return !empty($this->headers); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
2 |
|
public function hasSecurityContext(): bool |
|
114
|
|
|
{ |
|
115
|
2 |
|
return !is_null($this->getSecurityContext()); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
public function hasBasicAuth(): bool |
|
119
|
|
|
{ |
|
120
|
2 |
|
return !empty($this->basicAuth); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
public function setMethod(string $method): self |
|
124
|
|
|
{ |
|
125
|
1 |
|
$this->method = $method; |
|
126
|
|
|
|
|
127
|
1 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
public function setUri(string $uri): self |
|
131
|
|
|
{ |
|
132
|
1 |
|
$this->uri = $uri; |
|
133
|
|
|
|
|
134
|
1 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
1 |
|
public function setHeader(string $key, string $value): self |
|
138
|
|
|
{ |
|
139
|
1 |
|
$this->headers[$key] = $value; |
|
140
|
|
|
|
|
141
|
1 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
public function setHeaders(array $headers): self |
|
145
|
|
|
{ |
|
146
|
1 |
|
foreach ($headers as $key => $value) { |
|
147
|
1 |
|
$this->setHeader($key, $value); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function setBody(string $body): self |
|
154
|
|
|
{ |
|
155
|
|
|
$this->json = []; |
|
156
|
|
|
$this->body = $body; |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
2 |
|
public function setJson(array $json): self |
|
162
|
|
|
{ |
|
163
|
2 |
|
$this->json = $json; |
|
164
|
|
|
|
|
165
|
2 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
public function setQuery(array $query): self |
|
169
|
|
|
{ |
|
170
|
1 |
|
$this->query = $query; |
|
171
|
|
|
|
|
172
|
1 |
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
1 |
|
public function setTimeout(int $timeout): self |
|
176
|
|
|
{ |
|
177
|
1 |
|
$this->timeout = $timeout; |
|
178
|
|
|
|
|
179
|
1 |
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
1 |
|
public function setSecurityContext(HttpSecurityContext $securityContext): self |
|
183
|
|
|
{ |
|
184
|
1 |
|
$this->securityContext = $securityContext; |
|
185
|
|
|
|
|
186
|
1 |
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
public function setBasicAuth(string $username, string $password): self |
|
190
|
|
|
{ |
|
191
|
1 |
|
$this->basicAuth = [$username, $password]; |
|
192
|
|
|
|
|
193
|
1 |
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
public function ssl(bool $ssl): self |
|
197
|
|
|
{ |
|
198
|
1 |
|
$this->ssl = $ssl; |
|
199
|
|
|
|
|
200
|
1 |
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
2 |
|
public function isSSL(): bool |
|
204
|
|
|
{ |
|
205
|
2 |
|
return $this->ssl; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|