1 | <?php |
||
18 | class Response extends Listener |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * List of supported response formats. |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $formats = array('json', 'xml', 'html'); |
||
26 | |||
27 | /** |
||
28 | * Holds the current output format. |
||
29 | * Also use to set the default value. |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $format = 'html'; |
||
33 | |||
34 | /** |
||
35 | * Character encoding. |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $encoding = 'UTF-8'; |
||
39 | |||
40 | /** |
||
41 | * Holds the arrays of HTTP headers. |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $headers = array(); |
||
45 | |||
46 | /** |
||
47 | * Holds the current HTTP Code. |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $http_code = 200; |
||
51 | |||
52 | /** |
||
53 | * Holds the current output. |
||
54 | * @var string |
||
55 | */ |
||
56 | public $output = null; |
||
57 | |||
58 | /** |
||
59 | * Associative array of HTTP phrases. |
||
60 | * |
||
61 | * @var array |
||
62 | * @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
||
63 | * @link http://tools.ietf.org/html/rfc2616#section-10 |
||
64 | */ |
||
65 | protected static $http_phrases = array( |
||
66 | |||
67 | // 1xx: Informational - Request received, continuing process |
||
68 | 100 => 'Continue', |
||
69 | 101 => 'Switching Protocols', |
||
70 | |||
71 | // 2xx: Success - The action was successfully received, understood and |
||
72 | // accepted |
||
73 | 200 => 'OK', |
||
74 | 201 => 'Created', |
||
75 | 202 => 'Accepted', |
||
76 | 203 => 'Non-Authoritative Information', |
||
77 | 204 => 'No Content', |
||
78 | 205 => 'Reset Content', |
||
79 | 206 => 'Partial Content', |
||
80 | 207 => 'Multi-Status', |
||
81 | |||
82 | // 3xx: Redirection - Further action must be taken in order to complete |
||
83 | // the request |
||
84 | 300 => 'Multiple Choices', |
||
85 | 301 => 'Moved Permanently', |
||
86 | 302 => 'Found', // 1.1 |
||
87 | 303 => 'See Other', |
||
88 | 304 => 'Not Modified', |
||
89 | 305 => 'Use Proxy', |
||
90 | 307 => 'Temporary Redirect', |
||
91 | |||
92 | // 4xx: Client Error - The request contains bad syntax or cannot be |
||
93 | // fulfilled |
||
94 | 400 => 'Bad Request', |
||
95 | 401 => 'Unauthorized', |
||
96 | 402 => 'Payment Required', |
||
97 | 403 => 'Forbidden', |
||
98 | 404 => 'Not Found', |
||
99 | 405 => 'Method Not Allowed', |
||
100 | 406 => 'Not Acceptable', |
||
101 | 407 => 'Proxy Authentication Required', |
||
102 | 408 => 'Request Timeout', |
||
103 | 409 => 'Conflict', |
||
104 | 410 => 'Gone', |
||
105 | 411 => 'Length Required', |
||
106 | 412 => 'Precondition Failed', |
||
107 | 413 => 'Request Entity Too Large', |
||
108 | 414 => 'Request-URI Too Long', |
||
109 | 415 => 'Unsupported Media Type', |
||
110 | 416 => 'Requested Range Not Satisfiable', |
||
111 | 417 => 'Expectation Failed', |
||
112 | 422 => 'Unprocessable Entity', |
||
113 | 423 => 'Locked', |
||
114 | 424 => 'Failed Dependency', |
||
115 | 426 => 'Upgrade Required', |
||
116 | |||
117 | // 5xx: Server Error - The server failed to fulfill an apparently |
||
118 | // valid request |
||
119 | 500 => 'Internal Server Error', |
||
120 | 501 => 'Not Implemented', |
||
121 | 502 => 'Bad Gateway', |
||
122 | 503 => 'Service Unavailable', |
||
123 | 504 => 'Gateway Timeout', |
||
124 | 505 => 'HTTP Version Not Supported', |
||
125 | 506 => 'Variant Also Negotiates', |
||
126 | 507 => 'Insufficient Storage', |
||
127 | 509 => 'Bandwidth Limit Exceeded', |
||
128 | 510 => 'Not Extended' |
||
129 | ); |
||
130 | |||
131 | /** |
||
132 | * Associative array of long HTTP phrases. |
||
133 | * |
||
134 | * @var array |
||
135 | */ |
||
136 | protected static $long_http_phrases = array( |
||
137 | |||
138 | 200 => 'The request has succeeded.', |
||
139 | 201 => 'The request has been fulfilled and resulted in a new resource being created.', |
||
140 | |||
141 | // Resulting from a POST, requires to use ->setHeader("Location", "/resource/action/id") |
||
142 | 202 => 'The request has been accepted for processing, but the processing has not been completed.', |
||
143 | |||
144 | // DELETE |
||
145 | 204 => 'Request fulfilled successfully.', |
||
146 | |||
147 | // Errors |
||
148 | 400 => 'Request is malformed.', |
||
149 | 401 => 'Not Authenticated.', |
||
150 | 403 => 'Access to this ressource has been denied.', |
||
151 | 404 => 'No ressource found at the Request-URI.', |
||
152 | 501 => 'This resource entity is not (yet) implemented. Try again later.', |
||
153 | 503 => 'The service is currently unable to handle the request due to a temporary overloading or maintenance of the server. Try again later.' |
||
154 | ); |
||
155 | |||
156 | /** |
||
157 | * @var Apix\Request |
||
158 | */ |
||
159 | protected $request; |
||
160 | |||
161 | /** |
||
162 | * Returns the request object. |
||
163 | * |
||
164 | * @return Request |
||
165 | */ |
||
166 | public function getRequest() |
||
170 | |||
171 | /** |
||
172 | * @var Apix\Router |
||
173 | */ |
||
174 | protected $route; |
||
175 | |||
176 | /** |
||
177 | * Sets the route object. |
||
178 | */ |
||
179 | public function setRoute(Router $route) |
||
183 | |||
184 | /** |
||
185 | * Returns the route object. |
||
186 | * |
||
187 | * @return Router |
||
188 | */ |
||
189 | public function getRoute() |
||
193 | |||
194 | /** |
||
195 | * Constructor. |
||
196 | * |
||
197 | * @param Request $request |
||
198 | */ |
||
199 | public function __construct(Request $request) |
||
203 | |||
204 | /** |
||
205 | * Sets the output format. |
||
206 | * |
||
207 | * @param string $format |
||
208 | * @param string $default |
||
209 | * @throws \DomainException 406 |
||
210 | */ |
||
211 | public function setFormat($format, $default=null) |
||
223 | |||
224 | /** |
||
225 | * Returns the output format. |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | public function getFormat() |
||
233 | |||
234 | /** |
||
235 | * Sets all the response formats available. |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | public function setFormats(array $formats) |
||
243 | |||
244 | /** |
||
245 | * Returns all the response formats available. |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | public function getFormats() |
||
253 | |||
254 | /** |
||
255 | * Sets a response header. |
||
256 | * |
||
257 | * @param string $key |
||
258 | * @param string $value |
||
259 | * @param boolean $overwrite Wether to overwrite an existing header. |
||
260 | */ |
||
261 | public function setHeader($key, $value, $overwrite=true) |
||
268 | |||
269 | /** |
||
270 | * Gets the specified response header. |
||
271 | * |
||
272 | * @param string $key |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getHeader($key) |
||
279 | |||
280 | /** |
||
281 | * Returns the header array. |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getHeaders() |
||
289 | |||
290 | /** |
||
291 | * Sends the headers thru HTTP. |
||
292 | * |
||
293 | * header('Cache-Control: no-cache, must-revalidate'); // HTTP/1.1 |
||
294 | * header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past |
||
295 | * // upload example |
||
296 | * header('Content-Disposition: attachment; filename="downloaded.pdf"'); |
||
297 | * readfile('original.pdf'); |
||
298 | * |
||
299 | * @param integer $http_code |
||
300 | * @param string $version |
||
301 | */ |
||
302 | public function sendAllHttpHeaders($http_code, $version) |
||
319 | |||
320 | /** |
||
321 | * Sends one header thru HTTP |
||
322 | * @param vary |
||
323 | */ |
||
324 | public function sendHeader() |
||
332 | |||
333 | /** |
||
334 | * Sets the current HTTP code. |
||
335 | * |
||
336 | * @param integer $int |
||
337 | * @return void |
||
338 | */ |
||
339 | public function setHttpCode($int) |
||
343 | |||
344 | /** |
||
345 | * Gets the current HTTP code. |
||
346 | * |
||
347 | * @return intger |
||
348 | */ |
||
349 | public function getHttpCode() |
||
353 | |||
354 | /** |
||
355 | * Returns an HTTP status phrase. |
||
356 | * |
||
357 | * @param integer $http_code |
||
358 | * @param boolean $long |
||
359 | * @return string |
||
360 | */ |
||
361 | public static function getStatusPrases($http_code=null, $long=false) |
||
371 | |||
372 | /** |
||
373 | * Returns sucessful or failed string. |
||
374 | * |
||
375 | * @param integer $http_code |
||
376 | * @return string |
||
377 | */ |
||
378 | public static function getStatusAdjective($http_code) |
||
382 | |||
383 | /** |
||
384 | * Returns a collated array representation of the output. |
||
385 | * |
||
386 | * @return array |
||
387 | */ |
||
388 | public function collate(array $results) |
||
397 | |||
398 | /** |
||
399 | * Generates the response & send the headers... |
||
400 | * |
||
401 | * @param array $results |
||
402 | * @param string $version_string |
||
403 | * @param string $rootNode |
||
404 | * @return string |
||
405 | */ |
||
406 | public function generate(array $results, $version_string='ouarz', $rootNode='root') |
||
430 | |||
431 | /** |
||
432 | * Returns the response output. |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | public function getOutput() |
||
440 | |||
441 | /** |
||
442 | * Sets the response output. |
||
443 | * |
||
444 | * @param string $string |
||
445 | */ |
||
446 | public function setOutput($string) |
||
450 | |||
451 | } |
||
452 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..