[BUGFIX] Use proper phpDoc in PSR-7 implementation
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Http / Response.php
1 <?php
2 namespace TYPO3\CMS\Core\Http;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use Psr\Http\Message\ResponseInterface;
18 use Psr\Http\Message\StreamInterface;
19
20 /**
21 * Default implementation for the ResponseInterface of the PSR-7 standard.
22 *
23 * Highly inspired by https://github.com/phly/http/
24 *
25 * @internal Note that this is not public API yet.
26 */
27 class Response extends Message implements ResponseInterface
28 {
29 /**
30 * The HTTP status code of the response
31 * @var int $statusCode
32 */
33 protected $statusCode;
34
35 /**
36 * The reason phrase of the response
37 * @var string $reasonPhrase
38 */
39 protected $reasonPhrase = '';
40
41 /**
42 * The standardized and other important HTTP Status Codes
43 * @var array
44 */
45 protected $availableStatusCodes = [
46 // INFORMATIONAL CODES
47 100 => 'Continue',
48 101 => 'Switching Protocols',
49 102 => 'Processing',
50 103 => 'Early Hints',
51 // SUCCESS CODES
52 200 => 'OK',
53 201 => 'Created',
54 202 => 'Accepted',
55 203 => 'Non-Authoritative Information',
56 204 => 'No Content',
57 205 => 'Reset Content',
58 206 => 'Partial Content',
59 207 => 'Multi-Status',
60 208 => 'Already Reported',
61 226 => 'IM Used',
62 // REDIRECTION CODES
63 300 => 'Multiple Choices',
64 301 => 'Moved Permanently',
65 302 => 'Found',
66 303 => 'See Other',
67 304 => 'Not Modified',
68 305 => 'Use Proxy',
69 306 => 'Switch Proxy', // Deprecated
70 307 => 'Temporary Redirect',
71 308 => 'Permanent Redirect',
72 // CLIENT ERROR
73 400 => 'Bad Request',
74 401 => 'Unauthorized',
75 402 => 'Payment Required',
76 403 => 'Forbidden',
77 404 => 'Not Found',
78 405 => 'Method Not Allowed',
79 406 => 'Not Acceptable',
80 407 => 'Proxy Authentication Required',
81 408 => 'Request Timeout',
82 409 => 'Conflict',
83 410 => 'Gone',
84 411 => 'Length Required',
85 412 => 'Precondition Failed',
86 413 => 'Payload Too Large',
87 414 => 'URI Too Long',
88 415 => 'Unsupported Media Type',
89 416 => 'Range Not Satisfiable',
90 417 => 'Expectation Failed',
91 418 => 'I\'m a teapot',
92 421 => 'Misdirected Request',
93 422 => 'Unprocessable Entity',
94 423 => 'Locked',
95 424 => 'Failed Dependency',
96 425 => 'Unordered Collection',
97 426 => 'Upgrade Required',
98 428 => 'Precondition Required',
99 429 => 'Too Many Requests',
100 431 => 'Request Header Fields Too Large',
101 451 => 'Unavailable For Legal Reasons',
102 // SERVER ERROR
103 500 => 'Internal Server Error',
104 501 => 'Not Implemented',
105 502 => 'Bad Gateway',
106 503 => 'Service Unavailable',
107 504 => 'Gateway Timeout',
108 505 => 'HTTP Version Not Supported',
109 506 => 'Variant Also Negotiates',
110 507 => 'Insufficient Storage',
111 508 => 'Loop Detected',
112 509 => 'Bandwidth Limit Exceeded',
113 510 => 'Not Extended',
114 511 => 'Network Authentication Required'
115 ];
116
117 /**
118 * Constructor for generating new responses
119 *
120 * @param StreamInterface|string $body
121 * @param int $statusCode
122 * @param array $headers
123 * @throws \InvalidArgumentException if any of the given arguments are given
124 */
125 public function __construct($body = 'php://temp', $statusCode = 200, $headers = [])
126 {
127 // Build a streamable object for the body
128 if (!is_string($body) && !is_resource($body) && !$body instanceof StreamInterface) {
129 throw new \InvalidArgumentException('Body must be a string stream resource identifier, a stream resource, or a StreamInterface instance', 1436717277);
130 }
131
132 if (!$body instanceof StreamInterface) {
133 $body = new Stream($body, 'rw');
134 }
135 $this->body = $body;
136
137 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($statusCode) === false || !array_key_exists((int)$statusCode, $this->availableStatusCodes)) {
138 throw new \InvalidArgumentException('The given status code is not a valid HTTP status code.', 1436717278);
139 }
140 $this->statusCode = (int)$statusCode;
141
142 $this->reasonPhrase = $this->availableStatusCodes[$this->statusCode];
143 list($this->lowercasedHeaderNames, $headers) = $this->filterHeaders($headers);
144 $this->assertHeaders($headers);
145 $this->headers = $headers;
146 }
147
148 /**
149 * Gets the response status code.
150 *
151 * The status code is a 3-digit integer result code of the server's attempt
152 * to understand and satisfy the request.
153 *
154 * @return int Status code.
155 */
156 public function getStatusCode()
157 {
158 return $this->statusCode;
159 }
160
161 /**
162 * Return an instance with the specified status code and, optionally, reason phrase.
163 *
164 * If no reason phrase is specified, implementations MAY choose to default
165 * to the RFC 7231 or IANA recommended reason phrase for the response's
166 * status code.
167 *
168 * This method MUST be implemented in such a way as to retain the
169 * immutability of the message, and MUST return an instance that has the
170 * updated status and reason phrase.
171 *
172 * @link http://tools.ietf.org/html/rfc7231#section-6
173 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
174 *
175 * @param int $code The 3-digit integer result code to set.
176 * @param string $reasonPhrase The reason phrase to use with the
177 * provided status code; if none is provided, implementations MAY
178 * use the defaults as suggested in the HTTP specification.
179 * @return static
180 * @throws \InvalidArgumentException For invalid status code arguments.
181 */
182 public function withStatus($code, $reasonPhrase = '')
183 {
184 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($code) === false || !array_key_exists((int)$code, $this->availableStatusCodes)) {
185 throw new \InvalidArgumentException('The given status code is not a valid HTTP status code', 1436717279);
186 }
187 $clonedObject = clone $this;
188 $clonedObject->statusCode = (int)$code;
189 $clonedObject->reasonPhrase = $reasonPhrase !== '' ? $reasonPhrase : $this->availableStatusCodes[$code];
190 return $clonedObject;
191 }
192
193 /**
194 * Gets the response reason phrase associated with the status code.
195 *
196 * Because a reason phrase is not a required element in a response
197 * status line, the reason phrase value MAY be null. Implementations MAY
198 * choose to return the default RFC 7231 recommended reason phrase (or those
199 * listed in the IANA HTTP Status Code Registry) for the response's
200 * status code.
201 *
202 * @link http://tools.ietf.org/html/rfc7231#section-6
203 * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
204 * @return string Reason phrase; must return an empty string if none present.
205 */
206 public function getReasonPhrase()
207 {
208 return $this->reasonPhrase;
209 }
210 }