1 | <?php |
||
20 | abstract class AbstractRequest |
||
21 | implements RequestInterface, InputInterface |
||
22 | { |
||
23 | /** |
||
24 | * Headers |
||
25 | * |
||
26 | * @var array |
||
27 | * @access protected |
||
28 | */ |
||
29 | protected $headers; |
||
30 | |||
31 | /** |
||
32 | * Request data |
||
33 | * |
||
34 | * @var array |
||
35 | * @access protected |
||
36 | */ |
||
37 | protected $data; |
||
38 | |||
39 | /** |
||
40 | * Request URL |
||
41 | * |
||
42 | * @var string |
||
43 | * @access protected |
||
44 | */ |
||
45 | protected $url; |
||
46 | |||
47 | /** |
||
48 | * Request method |
||
49 | * |
||
50 | * @var string |
||
51 | * @access protected |
||
52 | */ |
||
53 | protected $method; |
||
54 | |||
55 | /** |
||
56 | * Timeout period |
||
57 | * |
||
58 | * @var int |
||
59 | * @access protected |
||
60 | */ |
||
61 | protected $timeout; |
||
62 | |||
63 | /** |
||
64 | * Page load delay time. |
||
65 | * |
||
66 | * @var int |
||
67 | * @access protected |
||
68 | */ |
||
69 | protected $delay; |
||
70 | |||
71 | /** |
||
72 | * Viewport width. |
||
73 | * |
||
74 | * @var int |
||
75 | * @access protected |
||
76 | */ |
||
77 | protected $viewportWidth; |
||
78 | |||
79 | /** |
||
80 | * Viewport height. |
||
81 | * |
||
82 | * @var int |
||
83 | * @access protected |
||
84 | */ |
||
85 | protected $viewportHeight; |
||
86 | |||
87 | /** |
||
88 | * Body styles. |
||
89 | * |
||
90 | * @var array |
||
91 | * @access protected |
||
92 | */ |
||
93 | protected $bodyStyles; |
||
94 | |||
95 | /** |
||
96 | * Internal constructor |
||
97 | * |
||
98 | * @access public |
||
99 | * @param string $url (default: null) |
||
100 | * @param string $method (default: RequestInterface::METHOD_GET) |
||
101 | * @param int $timeout (default: 5000) |
||
102 | */ |
||
103 | 1 | public function __construct($url = null, $method = RequestInterface::METHOD_GET, $timeout = 5000) |
|
104 | { |
||
105 | 1 | $this->headers = array(); |
|
106 | 1 | $this->data = array(); |
|
107 | 1 | $this->bodyStyles = array(); |
|
108 | 1 | $this->delay = 0; |
|
109 | 1 | $this->viewportWidth = 0; |
|
110 | 1 | $this->viewportHeight = 0; |
|
111 | |||
112 | 1 | $this->setMethod($method); |
|
113 | 1 | $this->setTimeout($timeout); |
|
114 | |||
115 | 1 | if ($url) { |
|
|
|||
116 | $this->setUrl($url); |
||
117 | } |
||
118 | 1 | } |
|
119 | |||
120 | /** |
||
121 | * Set request method |
||
122 | * |
||
123 | * @access public |
||
124 | * @param string $method |
||
125 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
126 | * @throws \JonnyW\PhantomJs\Exception\InvalidMethodException |
||
127 | */ |
||
128 | 1 | public function setMethod($method) |
|
129 | { |
||
130 | 1 | $method = strtoupper($method); |
|
131 | 1 | $reflection = new \ReflectionClass('\JonnyW\PhantomJs\Http\RequestInterface'); |
|
132 | |||
133 | 1 | if (!$reflection->hasConstant('METHOD_' . $method)) { |
|
134 | throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method)); |
||
135 | } |
||
136 | |||
137 | 1 | $this->method = $method; |
|
138 | |||
139 | 1 | return $this; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get request method |
||
144 | * |
||
145 | * @access public |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getMethod() |
||
149 | { |
||
150 | return $this->method; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Set timeout period |
||
155 | * |
||
156 | * @access public |
||
157 | * @param int $timeout |
||
158 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
159 | */ |
||
160 | 1 | public function setTimeout($timeout) |
|
166 | |||
167 | /** |
||
168 | * Get timeout period |
||
169 | * |
||
170 | * @access public |
||
171 | * @return int |
||
172 | */ |
||
173 | public function getTimeout() |
||
177 | |||
178 | /** |
||
179 | * Set page load delay time (seconds). |
||
180 | * |
||
181 | * @access public |
||
182 | * @param int $delay |
||
183 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
184 | */ |
||
185 | public function setDelay($delay) |
||
191 | |||
192 | /** |
||
193 | * Get page load delay time (seconds). |
||
194 | * |
||
195 | * @access public |
||
196 | * @return int |
||
197 | */ |
||
198 | public function getDelay() |
||
202 | |||
203 | /** |
||
204 | * Set viewport size. |
||
205 | * |
||
206 | * @access public |
||
207 | * @param int $width |
||
208 | * @param int $height |
||
209 | * @return void |
||
210 | */ |
||
211 | public function setViewportSize($width, $height) |
||
218 | |||
219 | /** |
||
220 | * Get viewport width. |
||
221 | * |
||
222 | * @access public |
||
223 | * @return int |
||
224 | */ |
||
225 | public function getViewportWidth() |
||
229 | |||
230 | /** |
||
231 | * Get viewport height. |
||
232 | * |
||
233 | * @access public |
||
234 | * @return int |
||
235 | */ |
||
236 | public function getViewportHeight() |
||
240 | |||
241 | /** |
||
242 | * Set request URL |
||
243 | * |
||
244 | * @access public |
||
245 | * @param string $url |
||
246 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
247 | */ |
||
248 | public function setUrl($url) |
||
254 | |||
255 | /** |
||
256 | * Get request URL |
||
257 | * - Assembles query string for GET |
||
258 | * and HEAD requests |
||
259 | * |
||
260 | * @access public |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getUrl() |
||
279 | |||
280 | /** |
||
281 | * Get content body |
||
282 | * - Returns query string if not GET or HEAD |
||
283 | * |
||
284 | * @access public |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getBody() |
||
295 | |||
296 | /** |
||
297 | * Set request data |
||
298 | * |
||
299 | * @access public |
||
300 | * @param array $data |
||
301 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
302 | */ |
||
303 | public function setRequestData(array $data) |
||
309 | |||
310 | /** |
||
311 | * Get request data |
||
312 | * |
||
313 | * @access public |
||
314 | * @param boolean $flat |
||
315 | * @return array |
||
316 | */ |
||
317 | public function getRequestData($flat = true) |
||
325 | |||
326 | /** |
||
327 | * Set headers |
||
328 | * |
||
329 | * @access public |
||
330 | * @param array $headers |
||
331 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
332 | */ |
||
333 | public function setHeaders(array $headers) |
||
337 | |||
338 | /** |
||
339 | * Add single header |
||
340 | * |
||
341 | * @access public |
||
342 | * @param string $header |
||
343 | * @param string $value |
||
344 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
345 | */ |
||
346 | public function addHeader($header, $value) |
||
352 | |||
353 | /** |
||
354 | * Merge headers with existing |
||
355 | * |
||
356 | * @access public |
||
357 | * @param array $headers |
||
358 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
359 | */ |
||
360 | public function addHeaders(array $headers) |
||
366 | |||
367 | /** |
||
368 | * Get request headers |
||
369 | * |
||
370 | * @access public |
||
371 | * @param string $format |
||
372 | * @return array|string |
||
373 | */ |
||
374 | public function getHeaders($format = 'default') |
||
382 | |||
383 | /** |
||
384 | * Set body styles |
||
385 | * |
||
386 | * @access public |
||
387 | * @param array $styles |
||
388 | * @return \JonnyW\PhantomJs\Http\AbstractRequest |
||
389 | */ |
||
390 | public function setBodyStyles(array $styles) |
||
396 | |||
397 | /** |
||
398 | * Get body styles |
||
399 | * |
||
400 | * @access public |
||
401 | * @param string $format (default: 'default') |
||
402 | * @return array|string |
||
403 | */ |
||
404 | public function getBodyStyles($format = 'default') |
||
412 | |||
413 | /** |
||
414 | * Flatten data into single |
||
415 | * dimensional array |
||
416 | * |
||
417 | * @access protected |
||
418 | * @param array $data |
||
419 | * @param string $prefix |
||
420 | * @param string $format |
||
421 | * @return array |
||
422 | */ |
||
423 | protected function flattenData(array $data, $prefix = '', $format = '%s') |
||
442 | } |
||
443 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: