1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* A PSR7 aware cURL client (https://github.com/juliangut/spiral). |
5
|
|
|
* |
6
|
|
|
* @license BSD-3-Clause |
7
|
|
|
* @link https://github.com/juliangut/spiral |
8
|
|
|
* @author Julián Gutiérrez <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Jgut\Spiral\Transport; |
12
|
|
|
|
13
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
14
|
|
|
use Jgut\Spiral\Exception\OptionException; |
15
|
|
|
use Jgut\Spiral\Option\OptionFactory; |
16
|
|
|
use Jgut\Spiral\Option\OptionInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Common transport trait. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractTransport implements TransportInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* List of cURL options. |
25
|
|
|
* |
26
|
|
|
* @var OptionInterface[] |
27
|
|
|
*/ |
28
|
|
|
protected $options = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Retrieve added cURL options. |
32
|
|
|
* |
33
|
|
|
* @return OptionInterface[] |
34
|
|
|
*/ |
35
|
|
|
public function getOptions() |
36
|
|
|
{ |
37
|
|
|
return $this->options; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set cURL options. |
42
|
|
|
* |
43
|
|
|
* @param array $options |
44
|
|
|
* |
45
|
|
|
* @throws OptionException |
46
|
|
|
*/ |
47
|
|
|
public function setOptions(array $options) |
48
|
|
|
{ |
49
|
|
|
foreach ($options as $name => $value) { |
50
|
|
|
$this->setOption($name, $value); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
* |
57
|
|
|
* @throws OptionException |
58
|
|
|
*/ |
59
|
|
|
public function setOption($option, $value = '', $quiet = false) |
60
|
|
|
{ |
61
|
|
|
if (!$option instanceof OptionInterface) { |
62
|
|
|
try { |
63
|
|
|
$option = OptionFactory::build($option, $value); |
64
|
|
|
} catch (OptionException $exception) { |
65
|
|
|
if ($quiet !== true) { |
66
|
|
|
throw $exception; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$this->removeOption($option->getOption()); |
72
|
|
|
$this->options[] = $option; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
* |
78
|
|
|
* @param int|string|OptionInterface $option |
79
|
|
|
* @param mixed $value |
80
|
|
|
*/ |
81
|
|
|
public function hasOption($option, $value = null) |
82
|
|
|
{ |
83
|
|
|
if ($option instanceof OptionInterface) { |
84
|
|
|
$option = $option->getOption(); |
85
|
|
|
} else { |
86
|
|
|
try { |
87
|
|
|
$option = OptionFactory::getOptionKey($option); |
88
|
|
|
} catch (OptionException $exception) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($this->options as $transportOption) { |
94
|
|
|
if ($transportOption->getOption() === $option) { |
95
|
|
|
return $value === null ?: $transportOption->getValue() === $value; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
* |
105
|
|
|
* @param int|string|OptionInterface $option |
106
|
|
|
*/ |
107
|
|
|
public function removeOption($option) |
108
|
|
|
{ |
109
|
|
|
if ($option instanceof OptionInterface) { |
110
|
|
|
$option = $option->getOption(); |
111
|
|
|
} else { |
112
|
|
|
try { |
113
|
|
|
$option = OptionFactory::getOptionKey($option); |
114
|
|
|
} catch (OptionException $exception) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->options = array_filter( |
120
|
|
|
$this->options, |
121
|
|
|
/** |
122
|
|
|
* @param OptionInterface $transportOption |
123
|
|
|
*/ |
124
|
|
|
function ($transportOption) use ($option) { |
125
|
|
|
/* @var OptionInterface $transportOption */ |
126
|
|
|
return !($transportOption->getOption() === $option); |
127
|
|
|
} |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Shorthand for OPTIONS cURL request. |
133
|
|
|
* |
134
|
|
|
* @param string $uri |
135
|
|
|
* @param array $headers |
136
|
|
|
* @param string|null $requestBody |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function options($uri, array $headers = [], $requestBody = null) |
141
|
|
|
{ |
142
|
|
|
return $this->request(RequestMethodInterface::METHOD_OPTIONS, $uri, $headers, $requestBody); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Shorthand for HEAD cURL request. |
147
|
|
|
* |
148
|
|
|
* @param string $uri |
149
|
|
|
* @param array $headers |
150
|
|
|
* @param string|null $requestBody |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function head($uri, array $headers = [], $requestBody = null) |
155
|
|
|
{ |
156
|
|
|
return $this->request(RequestMethodInterface::METHOD_HEAD, $uri, $headers, $requestBody); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Shorthand for GET cURL request. |
161
|
|
|
* |
162
|
|
|
* @param string $uri |
163
|
|
|
* @param array $headers |
164
|
|
|
* @param string|null $requestBody |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function get($uri, array $headers = [], $requestBody = null) |
169
|
|
|
{ |
170
|
|
|
return $this->request(RequestMethodInterface::METHOD_GET, $uri, $headers, $requestBody); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Shorthand for POST cURL request. |
175
|
|
|
* |
176
|
|
|
* @param string $uri |
177
|
|
|
* @param array $headers |
178
|
|
|
* @param string|null $requestBody |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function post($uri, array $headers = [], $requestBody = null) |
183
|
|
|
{ |
184
|
|
|
return $this->request(RequestMethodInterface::METHOD_POST, $uri, $headers, $requestBody); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Shorthand for PUT cURL request. |
189
|
|
|
* |
190
|
|
|
* @param string $uri |
191
|
|
|
* @param array $headers |
192
|
|
|
* @param string|null $requestBody |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function put($uri, array $headers = [], $requestBody = null) |
197
|
|
|
{ |
198
|
|
|
return $this->request(RequestMethodInterface::METHOD_PUT, $uri, $headers, $requestBody); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Shorthand for DELETE cURL request. |
203
|
|
|
* |
204
|
|
|
* @param string $uri |
205
|
|
|
* @param array $headers |
206
|
|
|
* @param string|null $requestBody |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function delete($uri, array $headers = [], $requestBody = null) |
211
|
|
|
{ |
212
|
|
|
return $this->request(RequestMethodInterface::METHOD_DELETE, $uri, $headers, $requestBody); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Shorthand for PATCH cURL request. |
217
|
|
|
* |
218
|
|
|
* @param string $uri |
219
|
|
|
* @param array $headers |
220
|
|
|
* @param string|null $requestBody |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public function patch($uri, array $headers = [], $requestBody = null) |
225
|
|
|
{ |
226
|
|
|
return $this->request(RequestMethodInterface::METHOD_PATCH, $uri, $headers, $requestBody); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|