|
| 1 | +<?php namespace CodeIgniter\HTTP; |
| 2 | + |
| 3 | +class Response |
| 4 | +{ |
| 5 | + protected static $statusCodes = [ |
| 6 | + // 1xx: Informational |
| 7 | + 100 => 'Continue', |
| 8 | + 101 => 'Switching Protocols', |
| 9 | + 102 => 'Processing', // http://www.iana.org/go/rfc2518 |
| 10 | + |
| 11 | + // 2xx: Success |
| 12 | + 200 => 'OK', |
| 13 | + 201 => 'Created', |
| 14 | + 202 => 'Accepted', |
| 15 | + 203 => 'Non-Authoritative Information', // 1.1 |
| 16 | + 204 => 'No Content', |
| 17 | + 205 => 'Reset Content', |
| 18 | + 206 => 'Partial Content', |
| 19 | + 207 => 'Multi-Status', // http://www.iana.org/go/rfc4918 |
| 20 | + 208 => 'Already Reported', // http://www.iana.org/go/rfc5842 |
| 21 | + 226 => 'IM Used', // 1.1; http://www.ietf.org/rfc/rfc3229.txt |
| 22 | + |
| 23 | + // 3xx: Redirection |
| 24 | + 300 => 'Multiple Choices', |
| 25 | + 301 => 'Moved Permanently', |
| 26 | + 302 => 'Found', // Formerly 'Moved Temporarily' |
| 27 | + 303 => 'See Other', // 1.1 |
| 28 | + 304 => 'Not Modified', |
| 29 | + 305 => 'Use Proxy', // 1.1 |
| 30 | + 306 => 'Switch Proxy', // No longer used |
| 31 | + 307 => 'Temporary Redirect', // 1.1 |
| 32 | + 308 => 'Permanent Redirect', // 1.1; Experimental; http://www.ietf.org/rfc/rfc7238.txt |
| 33 | + |
| 34 | + // 4xx: Client error |
| 35 | + 400 => 'Bad Request', |
| 36 | + 401 => 'Unauthorized', |
| 37 | + 402 => 'Payment Required', |
| 38 | + 403 => 'Forbidden', |
| 39 | + 404 => 'Not Found', |
| 40 | + 405 => 'Method Not Allowed', |
| 41 | + 406 => 'Not Acceptable', |
| 42 | + 407 => 'Proxy Authentication Required', |
| 43 | + 408 => 'Request Timeout', |
| 44 | + 409 => 'Conflict', |
| 45 | + 410 => 'Gone', |
| 46 | + 411 => 'Length Required', |
| 47 | + 412 => 'Precondition Failed', |
| 48 | + 413 => 'Request Entity Too Large', |
| 49 | + 414 => 'Request-URI Too Long', |
| 50 | + 415 => 'Unsupported Media Type', |
| 51 | + 416 => 'Requested Range Not Satisfiable', |
| 52 | + 417 => 'Expectation Failed', |
| 53 | + 418 => "I'm a teapot", // April's Fools joke; http://www.ietf.org/rfc/rfc2324.txt |
| 54 | + // 419 (Authentication Timeout) is a non-standard status code with unknown origin |
| 55 | + 421 => 'Misdirected Request', // http://www.iana.org/go/rfc7540 Section 9.1.2 |
| 56 | + 422 => 'Unprocessable Entity', // http://www.iana.org/go/rfc4918 |
| 57 | + 423 => 'Locked', // http://www.iana.org/go/rfc4918 |
| 58 | + 424 => 'Failed Dependency', // http://www.iana.org/go/rfc4918 |
| 59 | + 426 => 'Upgrade Required', |
| 60 | + 428 => 'Precondition Required', // 1.1; http://www.ietf.org/rfc/rfc6585.txt |
| 61 | + 429 => 'Too Many Requests', // 1.1; http://www.ietf.org/rfc/rfc6585.txt |
| 62 | + 431 => 'Request Header Fields Too Large', // 1.1; http://www.ietf.org/rfc/rfc6585.txt |
| 63 | + |
| 64 | + // 5xx: Server error |
| 65 | + 500 => 'Internal Server Error', |
| 66 | + 501 => 'Not Implemented', |
| 67 | + 502 => 'Bad Gateway', |
| 68 | + 503 => 'Service Unavailable', |
| 69 | + 504 => 'Gateway Timeout', |
| 70 | + 505 => 'HTTP Version Not Supported', |
| 71 | + 506 => 'Variant Also Negotiates', // 1.1; http://www.ietf.org/rfc/rfc2295.txt |
| 72 | + 507 => 'Insufficient Storage', // http://www.iana.org/go/rfc4918 |
| 73 | + 508 => 'Loop Detected', // http://www.iana.org/go/rfc5842 |
| 74 | + 510 => 'Not Extended', // http://www.ietf.org/rfc/rfc2774.txt |
| 75 | + 511 => 'Network Authentication Required' // http://www.ietf.org/rfc/rfc6585.txt |
| 76 | + ]; |
| 77 | + |
| 78 | + /** |
| 79 | + * The current reason phrase for this response. |
| 80 | + * If null, will use the default provided for the status code. |
| 81 | + * |
| 82 | + * @var string |
| 83 | + */ |
| 84 | + protected $reason; |
| 85 | + |
| 86 | + /** |
| 87 | + * The current status code for this response. |
| 88 | + * |
| 89 | + * @var int |
| 90 | + */ |
| 91 | + protected $statusCode = 200; |
| 92 | + |
| 93 | + //-------------------------------------------------------------------- |
| 94 | + |
| 95 | + /** |
| 96 | + * Gets the response status code. |
| 97 | + * |
| 98 | + * The status code is a 3-digit integer result code of the server's attempt |
| 99 | + * to understand and satisfy the request. |
| 100 | + * |
| 101 | + * @return int Status code. |
| 102 | + */ |
| 103 | + public function statusCode(): int |
| 104 | + { |
| 105 | + return $this->statusCode; |
| 106 | + } |
| 107 | + |
| 108 | + //-------------------------------------------------------------------- |
| 109 | + |
| 110 | + /** |
| 111 | + * Return an instance with the specified status code and, optionally, reason phrase. |
| 112 | + * |
| 113 | + * If no reason phrase is specified, will default recommended reason phrase for |
| 114 | + * the response's status code. |
| 115 | + * |
| 116 | + * @see http://tools.ietf.org/html/rfc7231#section-6 |
| 117 | + * @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
| 118 | + * |
| 119 | + * @param int $code The 3-digit integer result code to set. |
| 120 | + * @param string $reasonPhrase The reason phrase to use with the |
| 121 | + * provided status code; if none is provided, will |
| 122 | + * default to the IANA name. |
| 123 | + * |
| 124 | + * @return self |
| 125 | + * @throws \InvalidArgumentException For invalid status code arguments. |
| 126 | + */ |
| 127 | + public function setStatusCode(int $code, string $reason = ''): self |
| 128 | + { |
| 129 | + } |
| 130 | + |
| 131 | + //-------------------------------------------------------------------- |
| 132 | + |
| 133 | + /** |
| 134 | + * Gets the response response phrase associated with the status code. |
| 135 | + * |
| 136 | + * @see http://tools.ietf.org/html/rfc7231#section-6 |
| 137 | + * @see http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
| 138 | + * |
| 139 | + * @return string |
| 140 | + */ |
| 141 | + public function reason(): string |
| 142 | + { |
| 143 | + if (empty($this->reason)) |
| 144 | + { |
| 145 | + return ! empty($this->statusCode) |
| 146 | + ? static::$statusCodes[$this->statusCode] |
| 147 | + : ''; |
| 148 | + } |
| 149 | + |
| 150 | + return $this->reason; |
| 151 | + } |
| 152 | + |
| 153 | + //-------------------------------------------------------------------- |
| 154 | + |
| 155 | + |
| 156 | +} |
0 commit comments