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