|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Vadim Bova <[email protected]> |
|
5
|
|
|
* @link https://bova.io |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Folour\Oxide\Components; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Abstract configuration class for Oxide http client |
|
12
|
|
|
* |
|
13
|
|
|
* @package Folour\Oxide |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Configurator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array Options array buffer |
|
19
|
|
|
*/ |
|
20
|
|
|
private $options = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Set file for read and write cookies |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $cookies_file |
|
26
|
|
|
* @return self |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setCookiesFile(string $cookies_file): self |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->setOptions([ |
|
31
|
|
|
CURLOPT_COOKIEFILE => $cookies_file, |
|
32
|
|
|
CURLOPT_COOKIEJAR => $cookies_file |
|
33
|
|
|
]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Set request cookies |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $cookies |
|
40
|
|
|
* @return self |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setCookies(array $cookies): self |
|
43
|
|
|
{ |
|
44
|
|
|
$cookie_string = ''; |
|
45
|
|
|
array_walk($cookies, function($value, $key) use(&$cookie_string) { |
|
46
|
|
|
$cookie_string .= "$key=$value;"; |
|
47
|
|
|
}); |
|
48
|
|
|
|
|
49
|
|
|
return $this->setOptions([ |
|
50
|
|
|
CURLOPT_COOKIE => rtrim($cookie_string, ';') |
|
51
|
|
|
]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set request headers |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $headers |
|
58
|
|
|
* @return self |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setHeaders(array $headers): self |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->setOptions([ |
|
63
|
|
|
CURLOPT_HTTPHEADER => $headers |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set proxy server |
|
69
|
|
|
* If proxy server required authorization - provide proxy in format: |
|
70
|
|
|
* user:[email protected]:port |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $proxy |
|
73
|
|
|
* @return self |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setProxy(string $proxy): self |
|
76
|
|
|
{ |
|
77
|
|
|
if(strstr($proxy, '@')) { |
|
78
|
|
|
[$auth, $proxy] = explode('@', $proxy); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->setOptions([ |
|
82
|
|
|
CURLOPT_PROXYUSERPWD => $auth ?? null, |
|
83
|
|
|
CURLOPT_PROXY => $proxy |
|
84
|
|
|
]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Add cURL options |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $options |
|
91
|
|
|
* @return self |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function setOptions(array $options): self |
|
94
|
|
|
{ |
|
95
|
|
|
$this->options = array_replace($this->options, $options); |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get options array |
|
102
|
|
|
* |
|
103
|
|
|
* @return array |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getOptions(): array |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->options; |
|
108
|
|
|
} |
|
109
|
|
|
} |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.