1 | <?php |
||
18 | class Response |
||
19 | implements ResponseInterface, OutputInterface |
||
20 | { |
||
21 | /** |
||
22 | * Http headers array |
||
23 | * |
||
24 | * @var array |
||
25 | * @access public |
||
26 | */ |
||
27 | public $headers; |
||
28 | |||
29 | /** |
||
30 | * Response int |
||
31 | * |
||
32 | * @var string |
||
33 | * @access public |
||
34 | */ |
||
35 | public $status; |
||
36 | |||
37 | /** |
||
38 | * Response body |
||
39 | * |
||
40 | * @var string |
||
41 | * @access public |
||
42 | */ |
||
43 | public $content; |
||
44 | |||
45 | /** |
||
46 | * Response content type header |
||
47 | * |
||
48 | * @var string |
||
49 | * @access public |
||
50 | */ |
||
51 | public $contentType; |
||
52 | |||
53 | /** |
||
54 | * Requested URL |
||
55 | * |
||
56 | * @var string |
||
57 | * @access public |
||
58 | */ |
||
59 | public $url; |
||
60 | |||
61 | /** |
||
62 | * Redirected URL |
||
63 | * |
||
64 | * @var string |
||
65 | * @access public |
||
66 | */ |
||
67 | public $redirectURL; |
||
68 | |||
69 | /** |
||
70 | * Request time string |
||
71 | * |
||
72 | * @var string |
||
73 | * @access public |
||
74 | */ |
||
75 | public $time; |
||
76 | |||
77 | /** |
||
78 | * Console messages |
||
79 | * |
||
80 | * @var array |
||
81 | * @access public |
||
82 | */ |
||
83 | public $console; |
||
84 | |||
85 | /** |
||
86 | * Import response data |
||
87 | * |
||
88 | * @access public |
||
89 | * @param array $data |
||
90 | * @return \JonnyW\PhantomJs\Http\Response |
||
91 | */ |
||
92 | 26 | public function import(array $data) |
|
93 | { |
||
94 | 26 | foreach ($data as $param => $value) { |
|
95 | |||
96 | 26 | if ($param === 'headers') { |
|
97 | 24 | continue; |
|
98 | } |
||
99 | |||
100 | 26 | if (property_exists($this, $param)) { |
|
101 | 26 | $this->$param = $value; |
|
102 | 26 | } |
|
103 | 26 | } |
|
104 | |||
105 | 26 | $this->headers = array(); |
|
106 | |||
107 | 26 | if (isset($data['headers'])) { |
|
108 | 24 | $this->setHeaders((array) $data['headers']); |
|
109 | 24 | } |
|
110 | |||
111 | 26 | return $this; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Set headers array |
||
116 | * |
||
117 | * @access protected |
||
118 | * @param array $headers |
||
119 | * @return \JonnyW\PhantomJs\Http\Response |
||
120 | */ |
||
121 | 24 | protected function setHeaders(array $headers) |
|
132 | |||
133 | /** |
||
134 | * Get HTTP headers array |
||
135 | * |
||
136 | * @access public |
||
137 | * @return array |
||
138 | */ |
||
139 | 1 | public function getHeaders() |
|
143 | |||
144 | /** |
||
145 | * Get HTTP header value for code |
||
146 | * |
||
147 | * @access public |
||
148 | * @param string $code |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function getHeader($code) |
||
152 | { |
||
153 | if (isset($this->headers[$code])) { |
||
154 | return $this->headers[$code]; |
||
155 | } |
||
156 | |||
157 | return null; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Get response status code |
||
162 | * |
||
163 | * @access public |
||
164 | * @return integer |
||
165 | */ |
||
166 | 2 | public function getStatus() |
|
170 | |||
171 | /** |
||
172 | * Get page content from response |
||
173 | * |
||
174 | * @access public |
||
175 | * @return string |
||
176 | */ |
||
177 | 4 | public function getContent() |
|
181 | |||
182 | /** |
||
183 | * Get content type header |
||
184 | * |
||
185 | * @access public |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getContentType() |
||
189 | { |
||
190 | return $this->contentType; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Get request URL |
||
195 | * |
||
196 | * @access public |
||
197 | * @return string |
||
198 | */ |
||
199 | public function getUrl() |
||
200 | { |
||
201 | return $this->url; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Get redirect URL (if redirected) |
||
206 | * |
||
207 | * @access public |
||
208 | * @return string |
||
209 | */ |
||
210 | 1 | public function getRedirectUrl() |
|
214 | |||
215 | /** |
||
216 | * Is response a redirect |
||
217 | * - Checks status codes |
||
218 | * |
||
219 | * @access public |
||
220 | * @return boolean |
||
221 | */ |
||
222 | public function isRedirect() |
||
223 | { |
||
224 | $status = $this->getStatus(); |
||
225 | |||
226 | return (bool) ($status >= 300 && $status <= 307); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get time string |
||
231 | * |
||
232 | * @access public |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getTime() |
||
236 | { |
||
237 | return $this->time; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Get console messages |
||
242 | * |
||
243 | * @access public |
||
244 | * @return array |
||
245 | */ |
||
246 | 2 | public function getConsole() |
|
250 | } |
||
251 |