1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
<?php
namespace Alpha\Util\Http;
use Alpha\Exception\IllegalArguementException;
use Alpha\Util\Helper\Validator;
/**
* A class to encapsulate a HTTP Response.
*
* @since 2.0
*
* @author John Collins <dev@alphaframework.org>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
* All rights reserved.
*
* <pre>
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the
* following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
* * Neither the name of the Alpha Framework nor the names
* of its contributors may be used to endorse or promote
* products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* </pre>
*/
class Response
{
/**
* The body of the response.
*
* @var string
*
* @since 2.0
*/
private $body;
/**
* The status code of the response.
*
* @var int
*
* @since 2.0
*/
private $status;
/**
* Array of supported HTTP response codes.
*
* @var array
*
* @since 2.0
*/
private $HTTPStatusCodes = array(
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
306 => 'Switch Proxy',
307 => 'Temporary Redirect',
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m a teapot',
422 => 'Unprocessable Entity',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
449 => 'Retry With',
450 => 'Blocked by Windows Parental Controls',
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
509 => 'Bandwidth Limit Exceeded',
510 => 'Not Extended',
);
/**
* An associative array of headers for the response.
*
* @var array
*
* @since 2.0
*/
private $headers;
/**
* An associative array of HTTP cookies on this response.
*
* @var array
*
* @since 2.0
*/
private $cookies;
/**
* Build the response.
*
* @param int $status The HTTP status code of the response.
* @param string $body The body of the response (optional).
* @param array $headers The headers to set on the response (optional).
*
* @throws \Alpha\Exception\IllegalArguementException
*/
public function __construct($status, $body = null, $headers = array())
{
$this->headers = $headers;
if (isset($body)) {
$this->body = $body;
}
if (array_key_exists($status, $this->HTTPStatusCodes)) {
$this->status = $status;
} else {
throw new IllegalArguementException('The status code provided ['.$status.'] is invalid');
}
}
/**
* Get the response body.
*
* @return string|null
*
* @since 2.0
*/
public function getBody()
{
return $this->body;
}
/**
* Set the response body.
*
* @param string $body The response body.
*
* @since 2.0
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* Get the status code of the response.
*
* @return int
*
* @since 2.0
*/
public function getStatus()
{
return $this->status;
}
/**
* Get the status message of the response.
*
* @return string
*/
public function getStatusMessage()
{
return $this->HTTPStatusCodes[$this->status];
}
/**
* Set the status code of the response.
*
* @param int $status The response code.
*
* @throws \Alpha\Exception\IllegalArguementException
*
* @since 2.0
*/
public function setStatus($status)
{
if (array_key_exists($status, $this->HTTPStatusCodes)) {
$this->status = $status;
} else {
throw new IllegalArguementException('The status code provided ['.$status.'] is invalid');
}
}
/**
* Set a header key/value tuple for the response.
*
* @param string $header The header key name.
* @param string $value The header value.
*
* @since 2.0
*/
public function setHeader($header, $value)
{
$this->headers[$header] = $value;
}
/**
* Get all of the headers for the response.
*
* @return array
*
* @since 2.0
*/
public function getHeaders()
{
return $this->headers;
}
/**
* Get the header matching the key provided.
*
* @param string $key The key to search for
* @param mixed $default If key is not found, return this instead
*
* @return mixed
*
* @since 2.0
*/
public function getHeader($key, $default = null)
{
if (array_key_exists($key, $this->headers)) {
return $this->headers[$key];
} else {
return $default;
}
}
/**
* Set a cookie key/value tuple for the response.
*
* @param string $cookie The cookie key name.
* @param string $value The cookie value.
*
* @since 2.0
*/
public function setCookie($cookie, $value)
{
$this->cookies[$cookie] = $value;
}
/**
* Get all of the cookies for the response.
*
* @return array
*
* @since 2.0
*/
public function getCookies()
{
return $this->cookies;
}
/**
* Get the cookie matching the key provided.
*
* @param string $key The key to search for
* @param mixed $default If key is not found, return this instead
*
* @return mixed
*
* @since 2.0
*/
public function getCookie($key, $default = null)
{
if (array_key_exists($key, $this->cookies)) {
return $this->cookies[$key];
} else {
return $default;
}
}
/**
* Get the content length of the response.
*
* @return int
*
* @since 2.0
*/
public function getContentLength()
{
return strlen($this->body);
}
/**
* Builds a redirect response.
*
* @param string $URL The URL to redirect the client to.
*
* @throws \Alpha\Exception\IllegalArguementException
*
* @since 2.0
*/
public function redirect($URL)
{
if (Validator::isURL($URL)) {
$this->headers = array();
$this->setHeader('Location', $URL);
} else {
throw new IllegalArguementException('Unable to redirect to URL ['.$URL.'] as it is invalid');
}
}
/**
* Sends the current response to standard output before exiting the current process.
*
* @since 2.0
*/
public function send()
{
http_response_code($this->status);
foreach ($this->headers as $header => $value) {
header($header.': '.$value);
}
header_remove('X-Powered-By');
if (isset($this->body)) {
header('Content-Length: '.$this->getContentLength());
echo $this->body;
}
}
}