3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros;
10 use InvalidArgumentException;
11 use Psr\Http\Message\ResponseInterface;
12 use Psr\Http\Message\StreamInterface;
15 use function is_float;
16 use function is_numeric;
17 use function is_scalar;
21 * HTTP response encapsulation.
23 * Responses are considered immutable; all methods that might change state are
24 * implemented such that they retain the internal state of the current
25 * message and return a new instance that contains the changed state.
27 class Response implements ResponseInterface
31 const MIN_STATUS_CODE_VALUE = 100;
32 const MAX_STATUS_CODE_VALUE = 599;
35 * Map of standard HTTP status code/reason phrases
40 // INFORMATIONAL CODES
42 101 => 'Switching Protocols',
49 203 => 'Non-Authoritative Information',
51 205 => 'Reset Content',
52 206 => 'Partial Content',
53 207 => 'Multi-Status',
54 208 => 'Already Reported',
57 300 => 'Multiple Choices',
58 301 => 'Moved Permanently',
61 304 => 'Not Modified',
63 306 => 'Switch Proxy', // Deprecated to 306 => '(Unused)'
64 307 => 'Temporary Redirect',
65 308 => 'Permanent Redirect',
68 401 => 'Unauthorized',
69 402 => 'Payment Required',
72 405 => 'Method Not Allowed',
73 406 => 'Not Acceptable',
74 407 => 'Proxy Authentication Required',
75 408 => 'Request Timeout',
78 411 => 'Length Required',
79 412 => 'Precondition Failed',
80 413 => 'Payload Too Large',
81 414 => 'URI Too Long',
82 415 => 'Unsupported Media Type',
83 416 => 'Range Not Satisfiable',
84 417 => 'Expectation Failed',
85 418 => 'I\'m a teapot',
86 421 => 'Misdirected Request',
87 422 => 'Unprocessable Entity',
89 424 => 'Failed Dependency',
91 426 => 'Upgrade Required',
92 428 => 'Precondition Required',
93 429 => 'Too Many Requests',
94 431 => 'Request Header Fields Too Large',
95 444 => 'Connection Closed Without Response',
96 451 => 'Unavailable For Legal Reasons',
98 499 => 'Client Closed Request',
99 500 => 'Internal Server Error',
100 501 => 'Not Implemented',
101 502 => 'Bad Gateway',
102 503 => 'Service Unavailable',
103 504 => 'Gateway Timeout',
104 505 => 'HTTP Version Not Supported',
105 506 => 'Variant Also Negotiates',
106 507 => 'Insufficient Storage',
107 508 => 'Loop Detected',
108 510 => 'Not Extended',
109 511 => 'Network Authentication Required',
110 599 => 'Network Connect Timeout Error',
116 private $reasonPhrase;
124 * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
125 * @param int $status Status code for the response, if any.
126 * @param array $headers Headers for the response, if any.
127 * @throws InvalidArgumentException on any invalid element.
129 public function __construct($body = 'php://memory', $status = 200, array $headers = [])
131 $this->setStatusCode($status);
132 $this->stream = $this->getStream($body, 'wb+');
133 $this->setHeaders($headers);
139 public function getStatusCode()
141 return $this->statusCode;
147 public function getReasonPhrase()
149 return $this->reasonPhrase;
155 public function withStatus($code, $reasonPhrase = '')
158 $new->setStatusCode($code, $reasonPhrase);
163 * Set a valid status code.
166 * @param string $reasonPhrase
167 * @throws InvalidArgumentException on an invalid status code.
169 private function setStatusCode($code, $reasonPhrase = '')
171 if (! is_numeric($code)
173 || $code < static::MIN_STATUS_CODE_VALUE
174 || $code > static::MAX_STATUS_CODE_VALUE
176 throw new InvalidArgumentException(sprintf(
177 'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
178 is_scalar($code) ? $code : gettype($code),
179 static::MIN_STATUS_CODE_VALUE,
180 static::MAX_STATUS_CODE_VALUE
184 if (! is_string($reasonPhrase)) {
185 throw new InvalidArgumentException(sprintf(
186 'Unsupported response reason phrase; must be a string, received %s',
187 is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
191 if ($reasonPhrase === '' && isset($this->phrases[$code])) {
192 $reasonPhrase = $this->phrases[$code];
195 $this->reasonPhrase = $reasonPhrase;
196 $this->statusCode = (int) $code;