1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyHttp\Model; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Http Options |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/shahradelahi/easy-http |
9
|
|
|
* @author Shahrad Elahi (https://github.com/shahradelahi) |
10
|
|
|
* @license https://github.com/shahradelahi/easy-http/blob/master/LICENSE (MIT License) |
11
|
|
|
*/ |
12
|
|
|
class HttpOptions |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ?array |
17
|
|
|
*/ |
18
|
|
|
public ?array $headers = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ?array |
22
|
|
|
*/ |
23
|
|
|
public ?array $queries = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ?string |
27
|
|
|
*/ |
28
|
|
|
public ?string $body = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The proxy server to use |
32
|
|
|
* |
33
|
|
|
* @var ?ProxyServer |
34
|
|
|
*/ |
35
|
|
|
public ?ProxyServer $proxy = null; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Add specific opt to curl |
39
|
|
|
* |
40
|
|
|
* @var ?array |
41
|
|
|
*/ |
42
|
|
|
public ?array $curlOptions = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The timeout of the request |
46
|
|
|
* |
47
|
|
|
* @var ?int |
48
|
|
|
*/ |
49
|
|
|
public ?int $timeout = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The multipart data |
53
|
|
|
* |
54
|
|
|
* @var ?array |
55
|
|
|
*/ |
56
|
|
|
public ?array $multipart = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Http Options constructor. |
60
|
|
|
* |
61
|
|
|
* @param array $options |
62
|
|
|
*/ |
63
|
|
|
public function __construct(array $options = []) |
64
|
|
|
{ |
65
|
|
|
$this->setOptions($options); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the class as an array |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function toArray(): array |
74
|
|
|
{ |
75
|
|
|
$result = []; |
76
|
|
|
foreach (get_object_vars($this) as $key => $value) { |
77
|
|
|
$result[$key] = $value; |
78
|
|
|
} |
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set Options |
84
|
|
|
* |
85
|
|
|
* @param array $options |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function setOptions(array $options): void |
89
|
|
|
{ |
90
|
|
|
foreach ($options as $key => $value) { |
91
|
|
|
if (method_exists($this, 'set' . ucfirst($key))) { |
92
|
|
|
if ($value !== null) { |
93
|
|
|
$this->{'set' . ucfirst($key)}($value); |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
if (property_exists($this, $key)) { |
97
|
|
|
$this->{$key} = $value; |
98
|
|
|
} else { |
99
|
|
|
throw new \InvalidArgumentException("Invalid option: $key"); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set Body of Http request |
107
|
|
|
* |
108
|
|
|
* @param ?string|array $body The body of the request - On array it will be converted to json |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function setBody(string|array|null $body): void |
112
|
|
|
{ |
113
|
|
|
if (is_array($body)) { |
|
|
|
|
114
|
|
|
$this->body = json_encode($body); |
115
|
|
|
$this->headers['Content-Type'] = 'application/json'; |
116
|
|
|
} else { |
117
|
|
|
$this->body = $body; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Set proxy server |
123
|
|
|
* |
124
|
|
|
* @param array $proxy ["host", "port", "user", "pass"] |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function setProxy(array $proxy): void |
128
|
|
|
{ |
129
|
|
|
$this->proxy = (new ProxyServer())->setProxy($proxy); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get Query String |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
public function getQueryString(): string |
138
|
|
|
{ |
139
|
|
|
return http_build_query($this->queries); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set Curl Options |
144
|
|
|
* |
145
|
|
|
* @param array $options [{"CURLOPT_*": "value"}, ...] |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function setCurlOptions(array $options): void |
149
|
|
|
{ |
150
|
|
|
if (count($options) > 0) { |
151
|
|
|
foreach ($options as $option => $value) { |
152
|
|
|
$this->curlOptions[$option] = $value; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $string |
159
|
|
|
* @param array $array |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public function addMultiPart(string $string, array $array): void |
163
|
|
|
{ |
164
|
|
|
$this->multipart[] = [ |
165
|
|
|
'name' => $string, |
166
|
|
|
'contents' => $array |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |