1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VCR; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Encapsulates a HTTP request. |
7
|
|
|
*/ |
8
|
|
|
class Request |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $method; |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $url; |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $headers = array(); |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $body; |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $postFiles = array(); |
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $postFields = array(); |
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $curlOptions = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $method |
41
|
|
|
* @param string $url |
42
|
|
|
* @param array $headers |
43
|
|
|
*/ |
44
|
664 |
|
public function __construct($method, $url, array $headers = array()) |
45
|
|
|
{ |
46
|
664 |
|
$this->method = $method; |
47
|
664 |
|
$this->headers = $headers; |
48
|
664 |
|
$this->setUrl($url); |
49
|
664 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns true if specified request matches the current one |
53
|
|
|
* with specified request matcher callbacks. |
54
|
|
|
* |
55
|
|
|
* @param Request $request Request to check if it matches the current one. |
56
|
|
|
* @param \callable[] $requestMatchers Request matcher callbacks. |
57
|
|
|
* |
58
|
|
|
* @throws \BadFunctionCallException If one of the specified request matchers is not callable. |
59
|
|
|
* @return boolean True if specified request matches the current one. |
60
|
|
|
*/ |
61
|
84 |
|
public function matches(Request $request, array $requestMatchers) |
62
|
|
|
{ |
63
|
84 |
|
foreach ($requestMatchers as $matcher) { |
64
|
84 |
|
if (!is_callable($matcher)) { |
65
|
7 |
|
throw new \BadFunctionCallException( |
66
|
7 |
|
'Matcher could not be executed. ' . print_r($matcher, true) |
67
|
3 |
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
77 |
|
if (call_user_func_array($matcher, array($this, $request)) === false) { |
71
|
50 |
|
return false; |
72
|
|
|
} |
73
|
27 |
|
} |
74
|
|
|
|
75
|
63 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns an array representation of this request. |
80
|
|
|
* |
81
|
|
|
* @return array Array representation of this request. |
82
|
|
|
*/ |
83
|
98 |
|
public function toArray() |
84
|
|
|
{ |
85
|
98 |
|
return array_filter( |
86
|
|
|
array( |
87
|
98 |
|
'method' => $this->getMethod(), |
88
|
98 |
|
'url' => $this->getUrl(), |
89
|
98 |
|
'headers' => $this->getHeaders(), |
90
|
98 |
|
'body' => $this->getBody(), |
91
|
98 |
|
'post_files' => $this->getPostFiles(), |
92
|
98 |
|
'post_fields' => $this->getPostFields(), |
93
|
|
|
) |
94
|
42 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates a new Request from a specified array. |
99
|
|
|
* |
100
|
|
|
* @param array $request Request represented as an array. |
101
|
|
|
* |
102
|
|
|
* @return Request A new Request from specified array. |
103
|
|
|
*/ |
104
|
98 |
|
public static function fromArray(array $request) |
105
|
|
|
{ |
106
|
98 |
|
$requestObject = new Request( |
107
|
98 |
|
$request['method'], |
108
|
98 |
|
$request['url'], |
109
|
98 |
|
isset($request['headers']) ? $request['headers'] : array() |
110
|
42 |
|
); |
111
|
|
|
|
112
|
98 |
|
if (!empty($request['post_fields']) && is_array($request['post_fields'])) { |
113
|
14 |
|
$requestObject->setPostFields($request['post_fields']); |
114
|
6 |
|
} |
115
|
|
|
|
116
|
98 |
|
if (!empty($request['post_files']) && is_array($request['post_files'])) { |
117
|
7 |
|
foreach ($request['post_files'] as $file) { |
118
|
7 |
|
$requestObject->addPostFile($file); |
119
|
3 |
|
} |
120
|
3 |
|
} |
121
|
|
|
|
122
|
98 |
|
if (!empty($request['body'])) { |
123
|
28 |
|
$requestObject->setBody((string)$request['body']); |
124
|
12 |
|
} |
125
|
|
|
|
126
|
98 |
|
return $requestObject; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $url |
131
|
|
|
*/ |
132
|
664 |
|
public function setUrl($url) |
133
|
|
|
{ |
134
|
664 |
|
$this->url = $url; |
135
|
664 |
|
if ($this->hasHeader('Host') === false || $this->getHeader('Host') === null) { |
136
|
664 |
|
$this->setHeader('Host', $this->getHost()); |
137
|
284 |
|
} |
138
|
664 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
175 |
|
public function getBody() |
144
|
|
|
{ |
145
|
175 |
|
return $this->body; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
237 |
|
public function getMethod() |
152
|
|
|
{ |
153
|
237 |
|
if ($this->getCurlOption(CURLOPT_CUSTOMREQUEST) !== null) { |
154
|
14 |
|
return $this->getCurlOption(CURLOPT_CUSTOMREQUEST); |
155
|
|
|
} |
156
|
|
|
|
157
|
230 |
|
return $this->method; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
189 |
|
public function getHeaders() |
164
|
|
|
{ |
165
|
189 |
|
return $this->headers; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param $key |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
154 |
|
public function getHeader($key) |
173
|
|
|
{ |
174
|
154 |
|
return $this->headers[$key]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param $key |
179
|
|
|
* @return boolean |
180
|
|
|
*/ |
181
|
664 |
|
public function hasHeader($key) |
182
|
|
|
{ |
183
|
664 |
|
return array_key_exists($key, $this->headers); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
154 |
|
public function getPostFields() |
190
|
|
|
{ |
191
|
154 |
|
return $this->postFields; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
98 |
|
public function getPostFiles() |
198
|
|
|
{ |
199
|
98 |
|
return $this->postFiles; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
664 |
|
public function getUrl() |
206
|
|
|
{ |
207
|
664 |
|
return $this->url; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return mixed |
212
|
|
|
*/ |
213
|
664 |
|
public function getHost() |
214
|
|
|
{ |
215
|
664 |
|
$host = parse_url($this->getUrl(), PHP_URL_HOST); |
216
|
|
|
|
217
|
664 |
|
if ($port = parse_url($this->getUrl(), PHP_URL_PORT)) { |
218
|
7 |
|
$host .= ':' . $port; |
219
|
3 |
|
} |
220
|
|
|
|
221
|
664 |
|
return $host; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @return mixed |
226
|
|
|
*/ |
227
|
56 |
|
public function getPath() |
228
|
|
|
{ |
229
|
56 |
|
return parse_url($this->getUrl(), PHP_URL_PATH); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return mixed |
234
|
|
|
*/ |
235
|
56 |
|
public function getQuery() |
236
|
|
|
{ |
237
|
56 |
|
return parse_url($this->getUrl(), PHP_URL_QUERY); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return array |
242
|
|
|
*/ |
243
|
7 |
|
public function getCurlOptions() |
244
|
|
|
{ |
245
|
7 |
|
return $this->curlOptions; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param $key |
250
|
|
|
* @return mixed |
251
|
|
|
*/ |
252
|
349 |
|
public function getCurlOption($key) |
253
|
|
|
{ |
254
|
349 |
|
if (empty($this->curlOptions[$key])) { |
255
|
314 |
|
return null; |
256
|
|
|
} |
257
|
|
|
|
258
|
49 |
|
return $this->curlOptions[$key]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Sets the request method. |
263
|
|
|
* |
264
|
|
|
* @param string $method HTTP request method like GET, POST, PUT, ... |
265
|
|
|
*/ |
266
|
63 |
|
public function setMethod($method) |
267
|
|
|
{ |
268
|
63 |
|
$this->method = strtoupper($method); |
269
|
63 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param array $post_fields |
273
|
|
|
*/ |
274
|
21 |
|
public function setPostFields(array $post_fields) |
275
|
|
|
{ |
276
|
21 |
|
$this->postFields = $post_fields; |
277
|
21 |
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param array $post_files |
281
|
|
|
*/ |
282
|
|
|
public function setPostFiles(array $post_files) |
283
|
|
|
{ |
284
|
|
|
$this->postFiles = $post_files; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $body |
289
|
|
|
*/ |
290
|
98 |
|
public function setBody($body) |
291
|
|
|
{ |
292
|
98 |
|
$this->body = $body; |
293
|
98 |
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Sets the authorization credentials as header. |
297
|
|
|
* |
298
|
|
|
* @param string $username Username. |
299
|
|
|
* @param string $password Password. |
300
|
|
|
*/ |
301
|
7 |
|
public function setAuthorization($username, $password) |
302
|
|
|
{ |
303
|
7 |
|
$this->setHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password)); |
304
|
7 |
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param array $curlOptions |
308
|
|
|
*/ |
309
|
|
|
public function setCurlOptions(array $curlOptions) |
310
|
|
|
{ |
311
|
|
|
$this->curlOptions = $curlOptions; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param $key |
316
|
|
|
* @param $value |
317
|
|
|
*/ |
318
|
664 |
|
public function setHeader($key, $value) |
319
|
|
|
{ |
320
|
664 |
|
$this->headers[$key] = $value; |
321
|
664 |
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param $key |
325
|
|
|
*/ |
326
|
7 |
|
public function removeHeader($key) |
327
|
|
|
{ |
328
|
7 |
|
unset($this->headers[$key]); |
329
|
7 |
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param $key |
333
|
|
|
* @param $value |
334
|
|
|
*/ |
335
|
28 |
|
public function setPostField($key, $value) |
336
|
|
|
{ |
337
|
28 |
|
$this->postFields[$key] = $value; |
338
|
28 |
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @param $key |
342
|
|
|
* @param $value |
343
|
|
|
*/ |
344
|
125 |
|
public function setCurlOption($key, $value) |
345
|
|
|
{ |
346
|
125 |
|
$this->curlOptions[$key] = $value; |
347
|
125 |
|
} |
348
|
|
|
|
349
|
14 |
|
public function addPostFile(array $file) |
350
|
|
|
{ |
351
|
14 |
|
$this->postFiles[] = $file; |
352
|
14 |
|
} |
353
|
|
|
} |
354
|
|
|
|