3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 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 * HTTP response encapsulation.
17 * Responses are considered immutable; all methods that might change state are
18 * implemented such that they retain the internal state of the current
19 * message and return a new instance that contains the changed state.
21 class Response implements ResponseInterface
25 const MIN_STATUS_CODE_VALUE = 100;
26 const MAX_STATUS_CODE_VALUE = 599;
29 * Map of standard HTTP status code/reason phrases
34 // INFORMATIONAL CODES
36 101 => 'Switching Protocols',
43 203 => 'Non-Authoritative Information',
45 205 => 'Reset Content',
46 206 => 'Partial Content',
47 207 => 'Multi-Status',
48 208 => 'Already Reported',
51 300 => 'Multiple Choices',
52 301 => 'Moved Permanently',
55 304 => 'Not Modified',
57 306 => 'Switch Proxy', // Deprecated to 306 => '(Unused)'
58 307 => 'Temporary Redirect',
59 308 => 'Permanent Redirect',
62 401 => 'Unauthorized',
63 402 => 'Payment Required',
66 405 => 'Method Not Allowed',
67 406 => 'Not Acceptable',
68 407 => 'Proxy Authentication Required',
69 408 => 'Request Timeout',
72 411 => 'Length Required',
73 412 => 'Precondition Failed',
74 413 => 'Payload Too Large',
75 414 => 'URI Too Long',
76 415 => 'Unsupported Media Type',
77 416 => 'Range Not Satisfiable',
78 417 => 'Expectation Failed',
79 418 => 'I\'m a teapot',
80 421 => 'Misdirected Request',
81 422 => 'Unprocessable Entity',
83 424 => 'Failed Dependency',
84 425 => 'Unordered Collection',
85 426 => 'Upgrade Required',
86 428 => 'Precondition Required',
87 429 => 'Too Many Requests',
88 431 => 'Request Header Fields Too Large',
89 444 => 'Connection Closed Without Response',
90 451 => 'Unavailable For Legal Reasons',
92 499 => 'Client Closed Request',
93 500 => 'Internal Server Error',
94 501 => 'Not Implemented',
96 503 => 'Service Unavailable',
97 504 => 'Gateway Timeout',
98 505 => 'HTTP Version Not Supported',
99 506 => 'Variant Also Negotiates',
100 507 => 'Insufficient Storage',
101 508 => 'Loop Detected',
102 510 => 'Not Extended',
103 511 => 'Network Authentication Required',
104 599 => 'Network Connect Timeout Error',
110 private $reasonPhrase = '';
118 * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
119 * @param int $status Status code for the response, if any.
120 * @param array $headers Headers for the response, if any.
121 * @throws InvalidArgumentException on any invalid element.
123 public function __construct($body = 'php://memory', $status = 200, array $headers = [])
125 $this->setStatusCode($status);
126 $this->stream = $this->getStream($body, 'wb+');
127 $this->setHeaders($headers);
133 public function getStatusCode()
135 return $this->statusCode;
141 public function getReasonPhrase()
143 if (! $this->reasonPhrase
144 && isset($this->phrases[$this->statusCode])
146 $this->reasonPhrase = $this->phrases[$this->statusCode];
149 return $this->reasonPhrase;
155 public function withStatus($code, $reasonPhrase = '')
158 $new->setStatusCode($code);
159 $new->reasonPhrase = $reasonPhrase;
164 * Set a valid status code.
167 * @throws InvalidArgumentException on an invalid status code.
169 private function setStatusCode($code)
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
183 $this->statusCode = $code;