1 | <?php |
||
48 | class Response |
||
49 | { |
||
50 | /** |
||
51 | * @access protected |
||
52 | * @var RequestAbstract |
||
53 | */ |
||
54 | protected $request; |
||
55 | |||
56 | /** |
||
57 | * @access protected |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $data = array(); |
||
61 | |||
62 | /** |
||
63 | * @access protected |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $outputParts = array(); |
||
67 | |||
68 | /** |
||
69 | * @access protected |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $output; |
||
73 | |||
74 | /** |
||
75 | * @var boolean |
||
76 | */ |
||
77 | protected $outputLocked = false; |
||
78 | |||
79 | /** |
||
80 | * Constructs the object |
||
81 | * |
||
82 | * @access public |
||
83 | * @param \Zepi\Turbo\Request\RequestAbstract $request |
||
84 | */ |
||
85 | 25 | public function __construct(RequestAbstract $request) |
|
89 | |||
90 | /** |
||
91 | * Return the data for the given key. If the key does |
||
92 | * not exists the function will return false. |
||
93 | * |
||
94 | * @access public |
||
95 | * @param string $key |
||
96 | * @return mixed |
||
97 | */ |
||
98 | 4 | public function getData($key) |
|
106 | |||
107 | /** |
||
108 | * Returns true if the given key is set. |
||
109 | * |
||
110 | * @access public |
||
111 | * @param string $key |
||
112 | * @return boolean |
||
113 | */ |
||
114 | 4 | public function hasData($key) |
|
118 | |||
119 | /** |
||
120 | * Saves the value for the given key in the response object. |
||
121 | * |
||
122 | * @access public |
||
123 | * @param string $key |
||
124 | * @param mixed $value |
||
125 | */ |
||
126 | 3 | public function setData($key, $value) |
|
130 | |||
131 | /** |
||
132 | * Returns the output for the given key. If the key does |
||
133 | * not exists the function will return false. |
||
134 | * |
||
135 | * @access public |
||
136 | * @param string $key |
||
137 | * @return false|string |
||
138 | */ |
||
139 | 2 | public function getOutputPart($key) |
|
147 | |||
148 | /** |
||
149 | * Returns true if the given key exists as output key. |
||
150 | * |
||
151 | * @access public |
||
152 | * @param string $key |
||
153 | * @return boolean |
||
154 | */ |
||
155 | 2 | public function hasOutputPart($key) |
|
159 | |||
160 | /** |
||
161 | * Saves the output for the given key in the Response object. |
||
162 | * |
||
163 | * @access public |
||
164 | * @param string $key |
||
165 | * @param string $output |
||
166 | */ |
||
167 | 2 | public function setOutputPart($key, $output) |
|
171 | |||
172 | /** |
||
173 | * Returns all output parts of the Response object. |
||
174 | * |
||
175 | * @access public |
||
176 | * @return array |
||
177 | */ |
||
178 | 1 | public function getOutputParts() |
|
182 | |||
183 | /** |
||
184 | * Returns the output of the response. |
||
185 | * |
||
186 | * @access public |
||
187 | * @return string |
||
188 | */ |
||
189 | 3 | public function getOutput() |
|
193 | |||
194 | /** |
||
195 | * Returns true if the response has an output. |
||
196 | * |
||
197 | * @access public |
||
198 | * @return boolean |
||
199 | */ |
||
200 | 1 | public function hasOutput() |
|
204 | |||
205 | /** |
||
206 | * Sets the output of the response. |
||
207 | * If $lock is true the output will be locked and the method |
||
208 | * will not accept any other output. |
||
209 | * |
||
210 | * @access public |
||
211 | * @param string $output |
||
212 | * @param boolean $lock |
||
213 | * @return boolean |
||
214 | */ |
||
215 | 1 | public function setOutput($output, $lock = false) |
|
216 | { |
||
217 | 1 | if ($this->outputLocked) { |
|
218 | return false; |
||
219 | } |
||
220 | |||
221 | 1 | if ($lock) { |
|
222 | $this->outputLocked = true; |
||
223 | } |
||
224 | |||
225 | 1 | $this->output = $output; |
|
226 | 1 | return true; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Returns true if the output in the response is locked. |
||
231 | * |
||
232 | * @return boolean |
||
233 | */ |
||
234 | public function isOutputLocked() |
||
238 | |||
239 | /** |
||
240 | * Set the Location header to redirect a request |
||
241 | * |
||
242 | * @access public |
||
243 | * @param string $target |
||
244 | * @param integer $headerCode |
||
245 | * @param boolean $withOrigin |
||
246 | */ |
||
247 | 2 | public function redirectTo($target, $headerCode = 301, $withOrigin = false) |
|
248 | { |
||
249 | 2 | if (!($this->request instanceof WebRequest)) { |
|
250 | return; |
||
251 | } |
||
252 | |||
253 | 2 | if (!preg_match('/http(s?)\:\/\//', $target)) { |
|
254 | 1 | $target = $this->request->getFullRoute($target); |
|
255 | } |
||
256 | |||
257 | 2 | if ($withOrigin) { |
|
258 | $target = $this->addOriginToTargetUrl($target); |
||
259 | } |
||
260 | |||
261 | 2 | header("Location: " . $target, true, $headerCode); |
|
262 | 2 | } |
|
263 | |||
264 | /** |
||
265 | * Sends a header |
||
266 | * |
||
267 | * @access public |
||
268 | * @param string $message |
||
269 | * @param integer $code |
||
270 | */ |
||
271 | public function sendHeader($message, $code = null) |
||
272 | { |
||
273 | if (!($this->request instanceof WebRequest)) { |
||
274 | return; |
||
275 | } |
||
276 | |||
277 | if ($code !== null) { |
||
278 | header($message, true, $code); |
||
279 | } else { |
||
280 | header($message); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Sends the status code for the give code |
||
286 | * |
||
287 | * @access public |
||
288 | * @param integer $code |
||
289 | * @param boolean $resetOutput |
||
290 | */ |
||
291 | public function sendHttpStatus($code, $resetOutput = false) |
||
292 | { |
||
293 | if (!($this->request instanceof WebRequest)) { |
||
294 | return; |
||
295 | } |
||
296 | |||
297 | $codes = array( |
||
298 | 100 => 'Continue', |
||
299 | 101 => 'Switching Protocols', |
||
300 | 102 => 'Processing', |
||
301 | 200 => 'OK', |
||
302 | 201 => 'Created', |
||
303 | 202 => 'Accepted', |
||
304 | 203 => 'Non-Authoritative Information', |
||
305 | 204 => 'No Content', |
||
306 | 205 => 'Reset Content', |
||
307 | 206 => 'Partial Content', |
||
308 | 207 => 'Multi-Status', |
||
309 | 300 => 'Multiple Choices', |
||
310 | 301 => 'Moved Permanently', |
||
311 | 302 => 'Found', |
||
312 | 303 => 'See Other', |
||
313 | 304 => 'Not Modified', |
||
314 | 305 => 'Use Proxy', |
||
315 | 306 => 'Switch Proxy', |
||
316 | 307 => 'Temporary Redirect', |
||
317 | 400 => 'Bad Request', |
||
318 | 401 => 'Unauthorized', |
||
319 | 402 => 'Payment Required', |
||
320 | 403 => 'Forbidden', |
||
321 | 404 => 'Not Found', |
||
322 | 405 => 'Method Not Allowed', |
||
323 | 406 => 'Not Acceptable', |
||
324 | 407 => 'Proxy Authentication Required', |
||
325 | 408 => 'Request Timeout', |
||
326 | 409 => 'Conflict', |
||
327 | 410 => 'Gone', |
||
328 | 411 => 'Length Required', |
||
329 | 412 => 'Precondition Failed', |
||
330 | 413 => 'Request Entity Too Large', |
||
331 | 414 => 'Request-URI Too Long', |
||
332 | 415 => 'Unsupported Media Type', |
||
333 | 416 => 'Requested Range Not Satisfiable', |
||
334 | 417 => 'Expectation Failed', |
||
335 | 418 => 'I\'m a teapot', |
||
336 | 422 => 'Unprocessable Entity', |
||
337 | 423 => 'Locked', |
||
338 | 424 => 'Failed Dependency', |
||
339 | 425 => 'Unordered Collection', |
||
340 | 426 => 'Upgrade Required', |
||
341 | 449 => 'Retry With', |
||
342 | 450 => 'Blocked by Windows Parental Controls', |
||
343 | 500 => 'Internal Server Error', |
||
344 | 501 => 'Not Implemented', |
||
345 | 502 => 'Bad Gateway', |
||
346 | 503 => 'Service Unavailable', |
||
347 | 504 => 'Gateway Timeout', |
||
348 | 505 => 'HTTP Version Not Supported', |
||
349 | 506 => 'Variant Also Negotiates', |
||
350 | 507 => 'Insufficient Storage', |
||
351 | 509 => 'Bandwidth Limit Exceeded', |
||
352 | 510 => 'Not Extended' |
||
353 | ); |
||
354 | |||
355 | if (!isset($codes[$code])) { |
||
356 | return false; |
||
357 | } |
||
358 | |||
359 | $message = $this->request->getProtocol() . ' ' . $code . ' ' . $codes[$code]; |
||
360 | header($message, true, $code); |
||
361 | |||
362 | if ($resetOutput) { |
||
363 | $this->setOutput($message); |
||
364 | } |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Returns a full url for the given url parts array |
||
369 | * from the function `parse_url()`. |
||
370 | * |
||
371 | * @access public |
||
372 | * @param array $urlParts |
||
373 | * @return string |
||
374 | */ |
||
375 | public function buildUrl($urlParts) |
||
379 | |||
380 | /** |
||
381 | * Adds the origin and returns the given target url |
||
382 | * with the origin query parameter. |
||
383 | * |
||
384 | * @param string $target |
||
385 | * @return void|string |
||
386 | */ |
||
387 | protected function addOriginToTargetUrl($target) |
||
407 | } |
||
408 |