1 | <?php |
||
35 | class Response extends Message implements ResponseInterface |
||
36 | { |
||
37 | private $status = 200; |
||
38 | private $reasonPhrase = ''; |
||
39 | |||
40 | /** |
||
41 | * Status codes and reason phrases |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $messages = [ |
||
46 | //Informational 1xx |
||
47 | 100 => 'Continue', |
||
48 | 101 => 'Switching Protocols', |
||
49 | 102 => 'Processing', |
||
50 | //Successful 2xx |
||
51 | 200 => 'OK', |
||
52 | 201 => 'Created', |
||
53 | 202 => 'Accepted', |
||
54 | 203 => 'Non-Authoritative Information', |
||
55 | 204 => 'No Content', |
||
56 | 205 => 'Reset Content', |
||
57 | 206 => 'Partial Content', |
||
58 | 207 => 'Multi-Status', |
||
59 | 208 => 'Already Reported', |
||
60 | 226 => 'IM Used', |
||
61 | //Redirection 3xx |
||
62 | 300 => 'Multiple Choices', |
||
63 | 301 => 'Moved Permanently', |
||
64 | 302 => 'Found', |
||
65 | 303 => 'See Other', |
||
66 | 304 => 'Not Modified', |
||
67 | 305 => 'Use Proxy', |
||
68 | 306 => '(Unused)', |
||
69 | 307 => 'Temporary Redirect', |
||
70 | 308 => 'Permanent Redirect', |
||
71 | //Client Error 4xx |
||
72 | 400 => 'Bad Request', |
||
73 | 401 => 'Unauthorized', |
||
74 | 402 => 'Payment Required', |
||
75 | 403 => 'Forbidden', |
||
76 | 404 => 'Not Found', |
||
77 | 405 => 'Method Not Allowed', |
||
78 | 406 => 'Not Acceptable', |
||
79 | 407 => 'Proxy Authentication Required', |
||
80 | 408 => 'Request Timeout', |
||
81 | 409 => 'Conflict', |
||
82 | 410 => 'Gone', |
||
83 | 411 => 'Length Required', |
||
84 | 412 => 'Precondition Failed', |
||
85 | 413 => 'Request Entity Too Large', |
||
86 | 414 => 'Request-URI Too Long', |
||
87 | 415 => 'Unsupported Media Type', |
||
88 | 416 => 'Requested Range Not Satisfiable', |
||
89 | 417 => 'Expectation Failed', |
||
90 | 418 => 'I\'m a teapot', |
||
91 | 422 => 'Unprocessable Entity', |
||
92 | 423 => 'Locked', |
||
93 | 424 => 'Failed Dependency', |
||
94 | 426 => 'Upgrade Required', |
||
95 | 428 => 'Precondition Required', |
||
96 | 429 => 'Too Many Requests', |
||
97 | 431 => 'Request Header Fields Too Large', |
||
98 | 451 => 'Unavailable For Legal Reasons', |
||
99 | //Server Error 5xx |
||
100 | 500 => 'Internal Server Error', |
||
101 | 501 => 'Not Implemented', |
||
102 | 502 => 'Bad Gateway', |
||
103 | 503 => 'Service Unavailable', |
||
104 | 504 => 'Gateway Timeout', |
||
105 | 505 => 'HTTP Version Not Supported', |
||
106 | 506 => 'Variant Also Negotiates', |
||
107 | 507 => 'Insufficient Storage', |
||
108 | 508 => 'Loop Detected', |
||
109 | 510 => 'Not Extended', |
||
110 | 511 => 'Network Authentication Required', |
||
111 | ]; |
||
112 | |||
113 | /** |
||
114 | * Create new outgoing, server-side response. |
||
115 | * |
||
116 | * @param int $status The response status code. |
||
117 | * @param Headers|null $headers The response headers. |
||
118 | * @param StreamInterface|null $body The response body. |
||
119 | */ |
||
120 | 7 | public function __construct($status = 200, Headers $headers = null, StreamInterface $body = null) |
|
127 | |||
128 | /** |
||
129 | * Gets the response status code. |
||
130 | * |
||
131 | * The status code is a 3-digit integer result code of the server's attempt |
||
132 | * to understand and satisfy the request. |
||
133 | * |
||
134 | * @return int Status code. |
||
135 | */ |
||
136 | 3 | public function getStatusCode() |
|
140 | |||
141 | /** |
||
142 | * Return an instance with the specified status code and, optionally, reason phrase. |
||
143 | * |
||
144 | * If no reason phrase is specified, the RFC 7231 or IANA recommended reason |
||
145 | * phrase is returned according response status code. |
||
146 | * |
||
147 | * This method retain the immutability of the message, and return an |
||
148 | * instance that has the updated status and reason phrase. |
||
149 | * |
||
150 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
||
151 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
||
152 | * |
||
153 | * @param int $code The 3-digit integer result code to set. |
||
154 | * @param string $reasonPhrase The reason phrase to use with the provided status code; |
||
155 | * if none is provided, implementations use the defaults |
||
156 | * as suggested in the HTTP specification. |
||
157 | * |
||
158 | * @return self |
||
159 | * |
||
160 | * @throws \InvalidArgumentException For invalid status code arguments. |
||
161 | */ |
||
162 | 4 | public function withStatus($code, $reasonPhrase = '') |
|
178 | |||
179 | /** |
||
180 | * Gets the response reason phrase associated with the status code. |
||
181 | * |
||
182 | * The default RFC 7231 recommended reason phrase is selected according |
||
183 | * response's status code. |
||
184 | * |
||
185 | * @link http://tools.ietf.org/html/rfc7231#section-6 |
||
186 | * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
||
187 | * |
||
188 | * @return string Reason phrase; return an empty string if none present. |
||
189 | */ |
||
190 | 3 | public function getReasonPhrase() |
|
202 | |||
203 | // ------------ PRIVATE METHODS |
||
204 | |||
205 | /** |
||
206 | * Validate status code |
||
207 | * |
||
208 | * @param int $code The 3-digit integer result code to set. |
||
209 | * |
||
210 | * @return void |
||
211 | * @throws \InvalidArgumentException For invalid status code. |
||
212 | */ |
||
213 | 7 | private function validateStatus($code) |
|
225 | } |
||
226 |