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 | * Session cookies |
||
87 | * |
||
88 | * @var array |
||
89 | * @access public |
||
90 | */ |
||
91 | public $cookies; |
||
92 | |||
93 | /** |
||
94 | * Import response data |
||
95 | * |
||
96 | * @access public |
||
97 | * @param array $data |
||
98 | * @return \JonnyW\PhantomJs\Http\Response |
||
99 | */ |
||
100 | 11 | public function import(array $data) |
|
101 | { |
||
102 | 11 | foreach ($data as $param => $value) { |
|
103 | |||
104 | 11 | if ($param === 'headers') { |
|
105 | 9 | continue; |
|
106 | } |
||
107 | |||
108 | 11 | if (property_exists($this, $param)) { |
|
109 | 11 | $this->$param = $value; |
|
110 | } |
||
111 | } |
||
112 | |||
113 | 11 | $this->headers = array(); |
|
114 | |||
115 | 11 | if (isset($data['headers'])) { |
|
116 | 9 | $this->setHeaders((array) $data['headers']); |
|
117 | } |
||
118 | |||
119 | 11 | return $this; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Set headers array |
||
124 | * |
||
125 | * @access protected |
||
126 | * @param array $headers |
||
127 | * @return \JonnyW\PhantomJs\Http\Response |
||
128 | */ |
||
129 | 9 | protected function setHeaders(array $headers) |
|
130 | { |
||
131 | 9 | foreach ($headers as $header) { |
|
132 | |||
133 | 9 | if (isset($header['name']) && isset($header['value'])) { |
|
134 | 9 | $this->headers[$header['name']] = $header['value']; |
|
135 | } |
||
136 | } |
||
137 | |||
138 | 9 | return $this; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Get HTTP headers array |
||
143 | * |
||
144 | * @access public |
||
145 | * @return array |
||
146 | */ |
||
147 | public function getHeaders() |
||
148 | { |
||
149 | return (array) $this->headers; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Get HTTP header value for code |
||
154 | * |
||
155 | * @access public |
||
156 | * @param string $code |
||
157 | * @return mixed |
||
158 | */ |
||
159 | public function getHeader($code) |
||
160 | { |
||
161 | if (isset($this->headers[$code])) { |
||
162 | return $this->headers[$code]; |
||
163 | } |
||
164 | |||
165 | return null; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Get response status code |
||
170 | * |
||
171 | * @access public |
||
172 | * @return integer |
||
173 | */ |
||
174 | 2 | public function getStatus() |
|
178 | |||
179 | /** |
||
180 | * Get page content from response |
||
181 | * |
||
182 | * @access public |
||
183 | * @return string |
||
184 | */ |
||
185 | 5 | public function getContent() |
|
189 | |||
190 | /** |
||
191 | * Get content type header |
||
192 | * |
||
193 | * @access public |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getContentType() |
||
197 | { |
||
198 | return $this->contentType; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Get request URL |
||
203 | * |
||
204 | * @access public |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getUrl() |
||
208 | { |
||
209 | return $this->url; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Get redirect URL (if redirected) |
||
214 | * |
||
215 | * @access public |
||
216 | * @return string |
||
217 | */ |
||
218 | public function getRedirectUrl() |
||
219 | { |
||
220 | return $this->redirectURL; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Is response a redirect |
||
225 | * - Checks status codes |
||
226 | * |
||
227 | * @access public |
||
228 | * @return boolean |
||
229 | */ |
||
230 | public function isRedirect() |
||
231 | { |
||
232 | $status = $this->getStatus(); |
||
233 | |||
234 | return (bool) ($status >= 300 && $status <= 307); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Get time string |
||
239 | * |
||
240 | * @access public |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getTime() |
||
244 | { |
||
245 | return $this->time; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get console messages |
||
250 | * |
||
251 | * @access public |
||
252 | * @return array |
||
253 | */ |
||
254 | public function getConsole() |
||
255 | { |
||
256 | return $this->console; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get session cookies |
||
261 | * |
||
262 | * @access public |
||
263 | * @return array |
||
264 | */ |
||
265 | 1 | public function getCookies() |
|
269 | } |
||
270 |