|
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 array $json = []; |
|
14
|
|
|
protected array $query = []; |
|
15
|
|
|
protected ?HttpSecurityContext $securityContext = null; |
|
16
|
|
|
protected int $timeout = 10; |
|
17
|
|
|
protected array $basicAuth = []; |
|
18
|
|
|
protected bool $ssl = false; |
|
19
|
|
|
|
|
20
|
10 |
|
public function __construct(string $method, string $uri) |
|
21
|
|
|
{ |
|
22
|
10 |
|
$this->method = $method; |
|
23
|
10 |
|
$this->uri = $uri; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
10 |
|
public function getMethod(): string |
|
27
|
|
|
{ |
|
28
|
10 |
|
return $this->method; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
10 |
|
public function getUri(): string |
|
32
|
|
|
{ |
|
33
|
10 |
|
return $this->uri; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getHeader(string $key) |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->headers[$key] ?? null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function getHeaders(): array |
|
42
|
|
|
{ |
|
43
|
1 |
|
return $this->headers; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function getJson(): array |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->json; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function getQuery(): array |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->query; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
public function getTimeout(): int |
|
57
|
|
|
{ |
|
58
|
1 |
|
return $this->timeout; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function getSecurityContext(): ?HttpSecurityContext |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->securityContext; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
public function getBasicAuth(): array |
|
67
|
|
|
{ |
|
68
|
1 |
|
return $this->basicAuth; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function hasJson(): bool |
|
72
|
|
|
{ |
|
73
|
2 |
|
return !empty($this->json); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function hasQuery(): bool |
|
77
|
|
|
{ |
|
78
|
2 |
|
return !empty($this->query); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
public function hasHeaders(): bool |
|
82
|
|
|
{ |
|
83
|
2 |
|
return !empty($this->headers); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
public function hasSecurityContext(): bool |
|
87
|
|
|
{ |
|
88
|
2 |
|
return !is_null($this->getSecurityContext()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
public function hasBasicAuth(): bool |
|
92
|
|
|
{ |
|
93
|
2 |
|
return !empty($this->basicAuth); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
public function setMethod(string $method): self |
|
97
|
|
|
{ |
|
98
|
1 |
|
$this->method = $method; |
|
99
|
|
|
|
|
100
|
1 |
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
public function setUri(string $uri): self |
|
104
|
|
|
{ |
|
105
|
1 |
|
$this->uri = $uri; |
|
106
|
|
|
|
|
107
|
1 |
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
1 |
|
public function setHeader(string $key, string $value): self |
|
111
|
|
|
{ |
|
112
|
1 |
|
$this->headers[$key] = $value; |
|
113
|
|
|
|
|
114
|
1 |
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
public function setHeaders(array $headers): self |
|
118
|
|
|
{ |
|
119
|
1 |
|
foreach ($headers as $key => $value) { |
|
120
|
1 |
|
$this->setHeader($key, $value); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function setJson(array $json): self |
|
127
|
|
|
{ |
|
128
|
1 |
|
$this->json = $json; |
|
129
|
|
|
|
|
130
|
1 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
public function setQuery(array $query): self |
|
134
|
|
|
{ |
|
135
|
1 |
|
$this->query = $query; |
|
136
|
|
|
|
|
137
|
1 |
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
public function setTimeout(int $timeout): self |
|
141
|
|
|
{ |
|
142
|
1 |
|
$this->timeout = $timeout; |
|
143
|
|
|
|
|
144
|
1 |
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
public function setSecurityContext(HttpSecurityContext $securityContext): self |
|
148
|
|
|
{ |
|
149
|
1 |
|
$this->securityContext = $securityContext; |
|
150
|
|
|
|
|
151
|
1 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
public function setBasicAuth(string $username, string $password): self |
|
155
|
|
|
{ |
|
156
|
1 |
|
$this->basicAuth = [$username, $password]; |
|
157
|
|
|
|
|
158
|
1 |
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
public function ssl(bool $ssl): self |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->ssl = $ssl; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
2 |
|
public function isSSL(): bool |
|
169
|
|
|
{ |
|
170
|
2 |
|
return $this->ssl; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|