1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSSC\Components; |
4
|
|
|
|
5
|
|
|
use WSSC\Contracts\WscCommonsContract; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ClientConfig |
9
|
|
|
* @package WSSC\Components |
10
|
|
|
*/ |
11
|
|
|
class ClientConfig |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private string $scheme; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private string $host; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private string $user; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private string $password; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private string $port; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private int $timeout = WscCommonsContract::DEFAULT_TIMEOUT; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private array $headers = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private int $fragmentSize = WscCommonsContract::DEFAULT_FRAGMENT_SIZE; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var null|resource |
55
|
|
|
*/ |
56
|
|
|
private $context; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var bool |
60
|
|
|
*/ |
61
|
|
|
private bool $hasProxy = false; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private string $proxyIp; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private string $proxyPort; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var string|null |
75
|
|
|
*/ |
76
|
|
|
private ?string $proxyAuth; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var array |
80
|
|
|
*/ |
81
|
|
|
private array $contextOptions = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
|
|
public function getTimeout(): int |
87
|
|
|
{ |
88
|
|
|
return $this->timeout; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $timeout |
93
|
|
|
* @return ClientConfig |
94
|
|
|
*/ |
95
|
|
|
public function setTimeout(int $timeout): ClientConfig |
96
|
|
|
{ |
97
|
|
|
$this->timeout = $timeout; |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getHeaders(): array |
105
|
|
|
{ |
106
|
|
|
return $this->headers; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array $headers |
111
|
|
|
* @return ClientConfig |
112
|
|
|
*/ |
113
|
|
|
public function setHeaders(array $headers): ClientConfig |
114
|
|
|
{ |
115
|
|
|
$this->headers = $headers; |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function getFragmentSize(): int |
123
|
|
|
{ |
124
|
|
|
return $this->fragmentSize; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $fragmentSize |
129
|
|
|
* @return ClientConfig |
130
|
|
|
*/ |
131
|
|
|
public function setFragmentSize(int $fragmentSize): ClientConfig |
132
|
|
|
{ |
133
|
|
|
$this->fragmentSize = $fragmentSize; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function getContext() |
141
|
|
|
{ |
142
|
|
|
return $this->context; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $context |
147
|
|
|
* @return ClientConfig |
148
|
|
|
*/ |
149
|
|
|
public function setContext($context): ClientConfig |
150
|
|
|
{ |
151
|
|
|
$this->context = $context; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return mixed |
157
|
|
|
*/ |
158
|
|
|
public function getScheme(): string |
159
|
|
|
{ |
160
|
|
|
return $this->scheme; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $scheme |
165
|
|
|
* @return ClientConfig |
166
|
|
|
*/ |
167
|
|
|
public function setScheme(string $scheme): ClientConfig |
168
|
|
|
{ |
169
|
|
|
$this->scheme = $scheme; |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
|
|
public function getHost(): string |
177
|
|
|
{ |
178
|
|
|
return $this->host; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $host |
183
|
|
|
* @return ClientConfig |
184
|
|
|
*/ |
185
|
|
|
public function setHost(string $host): ClientConfig |
186
|
|
|
{ |
187
|
|
|
$this->host = $host; |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function getUser(): string |
195
|
|
|
{ |
196
|
|
|
return $this->user; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $urlParts |
201
|
|
|
* @return ClientConfig |
202
|
|
|
*/ |
203
|
|
|
public function setUser(array $urlParts): ClientConfig |
204
|
|
|
{ |
205
|
|
|
$this->user = $urlParts['user'] ?? ''; |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
|
|
public function getPassword(): string |
213
|
|
|
{ |
214
|
|
|
return $this->password; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param array $urlParts |
219
|
|
|
* @return ClientConfig |
220
|
|
|
*/ |
221
|
|
|
public function setPassword(array $urlParts): ClientConfig |
222
|
|
|
{ |
223
|
|
|
$this->password = $urlParts['pass'] ?? ''; |
224
|
|
|
return $this; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getPort(): string |
231
|
|
|
{ |
232
|
|
|
return $this->port; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param array $urlParts |
237
|
|
|
* @return ClientConfig |
238
|
|
|
*/ |
239
|
|
|
public function setPort(array $urlParts): ClientConfig |
240
|
|
|
{ |
241
|
|
|
$this->port = $urlParts['port'] ?? ($this->scheme === 'wss' ? '443' : '80'); |
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @return array |
247
|
|
|
*/ |
248
|
|
|
public function getContextOptions(): array |
249
|
|
|
{ |
250
|
|
|
return $this->contextOptions; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param array $contextOptions |
255
|
|
|
* @return ClientConfig |
256
|
|
|
*/ |
257
|
|
|
public function setContextOptions(array $contextOptions): ClientConfig |
258
|
|
|
{ |
259
|
|
|
$this->contextOptions = $contextOptions; |
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param string $ip |
265
|
|
|
* @param string $port |
266
|
|
|
* @return ClientConfig |
267
|
|
|
*/ |
268
|
|
|
public function setProxy(string $ip, string $port): ClientConfig |
269
|
|
|
{ |
270
|
|
|
$this->hasProxy = true; |
271
|
|
|
$this->proxyIp = $ip; |
272
|
|
|
$this->proxyPort = $port; |
273
|
|
|
|
274
|
|
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Sets auth for proxy |
279
|
|
|
* |
280
|
|
|
* @param string $userName |
281
|
|
|
* @param string $password |
282
|
|
|
*/ |
283
|
|
|
public function setProxyAuth(string $userName, string $password): ClientConfig |
284
|
|
|
{ |
285
|
|
|
$this->proxyAuth = (empty($userName) === false && empty($password) === false) ? base64_encode($userName.':'.$password) : null; |
286
|
|
|
return $this; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return bool |
291
|
|
|
*/ |
292
|
|
|
public function hasProxy() : bool |
293
|
|
|
{ |
294
|
|
|
return $this->hasProxy; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string|null |
299
|
|
|
*/ |
300
|
|
|
public function getProxyIp() : ?string |
301
|
|
|
{ |
302
|
|
|
return $this->proxyIp; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @return string|null |
307
|
|
|
*/ |
308
|
|
|
public function getProxyPort() : ?string |
309
|
|
|
{ |
310
|
|
|
return $this->proxyPort; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return string|null |
315
|
|
|
*/ |
316
|
|
|
public function getProxyAuth() : ?string |
317
|
|
|
{ |
318
|
|
|
return $this->proxyAuth; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|