3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use InvalidArgumentException;
13 use Psr\Http\Message\ResponseInterface;
14 use Psr\Http\Message\StreamInterface;
17 * HTTP response encapsulation.
19 * Responses are considered immutable; all methods that might change state are
20 * implemented such that they retain the internal state of the current
21 * message and return a new instance that contains the changed state.
23 class Response implements ResponseInterface
27 const MIN_STATUS_CODE_VALUE = 100;
28 const MAX_STATUS_CODE_VALUE = 599;
31 * Map of standard HTTP status code/reason phrases
36 // INFORMATIONAL CODES
38 101 => 'Switching Protocols',
44 203 => 'Non-Authoritative Information',
46 205 => 'Reset Content',
47 206 => 'Partial Content',
48 207 => 'Multi-Status',
49 208 => 'Already Reported',
52 300 => 'Multiple Choices',
53 301 => 'Moved Permanently',
56 304 => 'Not Modified',
58 306 => 'Switch Proxy', // Deprecated to 306 => '(Unused)'
59 307 => 'Temporary Redirect',
60 308 => 'Permanent Redirect',
63 401 => 'Unauthorized',
64 402 => 'Payment Required',
67 405 => 'Method Not Allowed',
68 406 => 'Not Acceptable',
69 407 => 'Proxy Authentication Required',
70 408 => 'Request Timeout',
73 411 => 'Length Required',
74 412 => 'Precondition Failed',
75 413 => 'Payload Too Large',
76 414 => 'URI Too Long',
77 415 => 'Unsupported Media Type',
78 416 => 'Range Not Satisfiable',
79 417 => 'Expectation Failed',
80 418 => 'I\'m a teapot',
81 421 => 'Misdirected Request',
82 422 => 'Unprocessable Entity',
84 424 => 'Failed Dependency',
85 425 => 'Unordered Collection',
86 426 => 'Upgrade Required',
87 428 => 'Precondition Required',
88 429 => 'Too Many Requests',
89 431 => 'Request Header Fields Too Large',
90 444 => 'Connection Closed Without Response',
91 451 => 'Unavailable For Legal Reasons',
93 499 => 'Client Closed Request',
94 500 => 'Internal Server Error',
95 501 => 'Not Implemented',
97 503 => 'Service Unavailable',
98 504 => 'Gateway Timeout',
99 505 => 'HTTP Version Not Supported',
100 506 => 'Variant Also Negotiates',
101 507 => 'Insufficient Storage',
102 508 => 'Loop Detected',
103 510 => 'Not Extended',
104 511 => 'Network Authentication Required',
105 599 => 'Network Connect Timeout Error',
111 private $reasonPhrase = '';
119 * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
120 * @param int $status Status code for the response, if any.
121 * @param array $headers Headers for the response, if any.
122 * @throws InvalidArgumentException on any invalid element.
124 public function __construct($body = 'php://memory', $status = 200, array $headers = [])
126 $this->setStatusCode($status);
127 $this->stream = $this->getStream($body, 'wb+');
128 $this->setHeaders($headers);
134 public function getStatusCode()
136 return $this->statusCode;
142 public function getReasonPhrase()
144 if (! $this->reasonPhrase
145 && isset($this->phrases[$this->statusCode])
147 $this->reasonPhrase = $this->phrases[$this->statusCode];
150 return $this->reasonPhrase;
156 public function withStatus($code, $reasonPhrase = '')
159 $new->setStatusCode($code);
160 $new->reasonPhrase = $reasonPhrase;
165 * Set a valid status code.
168 * @throws InvalidArgumentException on an invalid status code.
170 private function setStatusCode($code)
172 if (! is_numeric($code)
174 || $code < static::MIN_STATUS_CODE_VALUE
175 || $code > static::MAX_STATUS_CODE_VALUE
177 throw new InvalidArgumentException(sprintf(
178 'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
179 (is_scalar($code) ? $code : gettype($code)),
180 static::MIN_STATUS_CODE_VALUE,
181 static::MAX_STATUS_CODE_VALUE
184 $this->statusCode = $code;