|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * This file is part of simple-mvc-rest-api for PHP. |
| 6 | + * |
| 7 | + */ |
| 8 | +namespace Http; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class Response an Http response |
| 12 | + * |
| 13 | + * @author Mohammad Rahmani <rto1680@gmail.com> |
| 14 | + * |
| 15 | + * @package Http |
| 16 | + */ |
| 17 | +class Response { |
| 18 | + |
| 19 | + /** |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + protected $headers = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + protected $statusTexts = [ |
| 28 | + // INFORMATIONAL CODES |
| 29 | + 100 => 'Continue', |
| 30 | + 101 => 'Switching Protocols', |
| 31 | + 102 => 'Processing', |
| 32 | + // SUCCESS CODES |
| 33 | + 200 => 'OK', |
| 34 | + 201 => 'Created', |
| 35 | + 202 => 'Accepted', |
| 36 | + 203 => 'Non-Authoritative Information', |
| 37 | + 204 => 'No Content', |
| 38 | + 205 => 'Reset Content', |
| 39 | + 206 => 'Partial Content', |
| 40 | + 207 => 'Multi-status', |
| 41 | + 208 => 'Already Reported', |
| 42 | + // REDIRECTION CODES |
| 43 | + 300 => 'Multiple Choices', |
| 44 | + 301 => 'Moved Permanently', |
| 45 | + 302 => 'Found', |
| 46 | + 303 => 'See Other', |
| 47 | + 304 => 'Not Modified', |
| 48 | + 305 => 'Use Proxy', |
| 49 | + 306 => 'Switch Proxy', // Deprecated |
| 50 | + 307 => 'Temporary Redirect', |
| 51 | + // CLIENT ERROR |
| 52 | + 400 => 'Bad Request', |
| 53 | + 401 => 'Unauthorized', |
| 54 | + 402 => 'Payment Required', |
| 55 | + 403 => 'Forbidden', |
| 56 | + 404 => 'Not Found', |
| 57 | + 405 => 'Method Not Allowed', |
| 58 | + 406 => 'Not Acceptable', |
| 59 | + 407 => 'Proxy Authentication Required', |
| 60 | + 408 => 'Request Time-out', |
| 61 | + 409 => 'Conflict', |
| 62 | + 410 => 'Gone', |
| 63 | + 411 => 'Length Required', |
| 64 | + 412 => 'Precondition Failed', |
| 65 | + 413 => 'Request Entity Too Large', |
| 66 | + 414 => 'Request-URI Too Long', |
| 67 | + 415 => 'Unsupported Media Type', |
| 68 | + 416 => 'Requested range not satisfiable', |
| 69 | + 417 => 'Expectation Failed', |
| 70 | + 418 => 'I\'m a teapot', |
| 71 | + 422 => 'Unprocessable Entity', |
| 72 | + 423 => 'Locked', |
| 73 | + 424 => 'Failed Dependency', |
| 74 | + 425 => 'Unordered Collection', |
| 75 | + 426 => 'Upgrade Required', |
| 76 | + 428 => 'Precondition Required', |
| 77 | + 429 => 'Too Many Requests', |
| 78 | + 431 => 'Request Header Fields Too Large', |
| 79 | + // SERVER ERROR |
| 80 | + 500 => 'Internal Server Error', |
| 81 | + 501 => 'Not Implemented', |
| 82 | + 502 => 'Bad Gateway', |
| 83 | + 503 => 'Service Unavailable', |
| 84 | + 504 => 'Gateway Time-out', |
| 85 | + 505 => 'HTTP Version not supported', |
| 86 | + 506 => 'Variant Also Negotiates', |
| 87 | + 507 => 'Insufficient Storage', |
| 88 | + 508 => 'Loop Detected', |
| 89 | + 511 => 'Network Authentication Required', |
| 90 | + ]; |
| 91 | + |
| 92 | + /** |
| 93 | + * @var |
| 94 | + */ |
| 95 | + protected $version; |
| 96 | + |
| 97 | + /** |
| 98 | + * @var |
| 99 | + */ |
| 100 | + protected $content; |
| 101 | + |
| 102 | + /** |
| 103 | + * Response constructor. |
| 104 | + */ |
| 105 | + public function __construct () { |
| 106 | + $this->setVersion('1.1'); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Set the Http protocol version |
| 111 | + * |
| 112 | + * @param string $version |
| 113 | + */ |
| 114 | + public function setVersion(string $version) { |
| 115 | + $this->version = $version; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the Http protocol version |
| 120 | + * |
| 121 | + * @return string |
| 122 | + */ |
| 123 | + public function getVersion() : string { |
| 124 | + return $this->version; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Get the status code text. |
| 129 | + * |
| 130 | + * @param int $code |
| 131 | + * @return string |
| 132 | + */ |
| 133 | + public function getStatusCodeText(int $code) : string { |
| 134 | + return (string) isset($this->statusTexts[$code]) ? $this->statusTexts[$code] : 'unknown status'; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Set the response Headers. |
| 139 | + * |
| 140 | + * @param $header |
| 141 | + */ |
| 142 | + public function setHeader(String $header) { |
| 143 | + $this->headers[] = $header; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Get the response Headers. |
| 148 | + * |
| 149 | + * @return array |
| 150 | + */ |
| 151 | + public function getHeader() { |
| 152 | + return $this->headers; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Set content response. |
| 157 | + * |
| 158 | + * @param $content |
| 159 | + */ |
| 160 | + public function setContent($content) { |
| 161 | + $this->content = json_encode($content); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get content response. |
| 166 | + * |
| 167 | + * @return mixed |
| 168 | + */ |
| 169 | + public function getContent() { |
| 170 | + return $this->content; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @param $url |
| 175 | + */ |
| 176 | + public function redirect($url) { |
| 177 | + if (empty($url)) { |
| 178 | + trigger_error('Cannot redirect to an empty URL.'); |
| 179 | + exit; |
| 180 | + } |
| 181 | + |
| 182 | + header('Location: ' . str_replace(array('&', "\n", "\r"), array('&','', ''), $url), true, 302); |
| 183 | + exit(); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * check status code is invalid |
| 188 | + * |
| 189 | + * @return bool |
| 190 | + */ |
| 191 | + public function isInvalid(int $statusCode) : bool { |
| 192 | + return $statusCode < 100 || $statusCode >= 600; |
| 193 | + } |
| 194 | + |
| 195 | + public function sendStatus($code) { |
| 196 | + if (!$this->isInvalid($code)) { |
| 197 | + $this->setHeader(sprintf('HTTP/1.1 ' . $code . ' %s' , $this->getStatusCodeText($code))); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Render Output |
| 203 | + */ |
| 204 | + public function render() { |
| 205 | + if ($this->content) { |
| 206 | + $output = $this->content; |
| 207 | + |
| 208 | + // Headers |
| 209 | + if (!headers_sent()) { |
| 210 | + foreach ($this->headers as $header) { |
| 211 | + header($header, true); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + echo $output; |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments