Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@12
|
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@17
|
4 * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Zend\Diactoros;
|
Chris@0
|
9
|
Chris@0
|
10 use InvalidArgumentException;
|
Chris@0
|
11 use Psr\Http\Message\ResponseInterface;
|
Chris@0
|
12 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
13
|
Chris@16
|
14 use function gettype;
|
Chris@16
|
15 use function is_float;
|
Chris@16
|
16 use function is_numeric;
|
Chris@16
|
17 use function is_scalar;
|
Chris@16
|
18 use function sprintf;
|
Chris@16
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * HTTP response encapsulation.
|
Chris@0
|
22 *
|
Chris@0
|
23 * Responses are considered immutable; all methods that might change state are
|
Chris@0
|
24 * implemented such that they retain the internal state of the current
|
Chris@0
|
25 * message and return a new instance that contains the changed state.
|
Chris@0
|
26 */
|
Chris@0
|
27 class Response implements ResponseInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 use MessageTrait;
|
Chris@0
|
30
|
Chris@0
|
31 const MIN_STATUS_CODE_VALUE = 100;
|
Chris@0
|
32 const MAX_STATUS_CODE_VALUE = 599;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Map of standard HTTP status code/reason phrases
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var array
|
Chris@0
|
38 */
|
Chris@0
|
39 private $phrases = [
|
Chris@0
|
40 // INFORMATIONAL CODES
|
Chris@0
|
41 100 => 'Continue',
|
Chris@0
|
42 101 => 'Switching Protocols',
|
Chris@0
|
43 102 => 'Processing',
|
Chris@12
|
44 103 => 'Early Hints',
|
Chris@0
|
45 // SUCCESS CODES
|
Chris@0
|
46 200 => 'OK',
|
Chris@0
|
47 201 => 'Created',
|
Chris@0
|
48 202 => 'Accepted',
|
Chris@0
|
49 203 => 'Non-Authoritative Information',
|
Chris@0
|
50 204 => 'No Content',
|
Chris@0
|
51 205 => 'Reset Content',
|
Chris@0
|
52 206 => 'Partial Content',
|
Chris@0
|
53 207 => 'Multi-Status',
|
Chris@0
|
54 208 => 'Already Reported',
|
Chris@0
|
55 226 => 'IM Used',
|
Chris@0
|
56 // REDIRECTION CODES
|
Chris@0
|
57 300 => 'Multiple Choices',
|
Chris@0
|
58 301 => 'Moved Permanently',
|
Chris@0
|
59 302 => 'Found',
|
Chris@0
|
60 303 => 'See Other',
|
Chris@0
|
61 304 => 'Not Modified',
|
Chris@0
|
62 305 => 'Use Proxy',
|
Chris@0
|
63 306 => 'Switch Proxy', // Deprecated to 306 => '(Unused)'
|
Chris@0
|
64 307 => 'Temporary Redirect',
|
Chris@0
|
65 308 => 'Permanent Redirect',
|
Chris@0
|
66 // CLIENT ERROR
|
Chris@0
|
67 400 => 'Bad Request',
|
Chris@0
|
68 401 => 'Unauthorized',
|
Chris@0
|
69 402 => 'Payment Required',
|
Chris@0
|
70 403 => 'Forbidden',
|
Chris@0
|
71 404 => 'Not Found',
|
Chris@0
|
72 405 => 'Method Not Allowed',
|
Chris@0
|
73 406 => 'Not Acceptable',
|
Chris@0
|
74 407 => 'Proxy Authentication Required',
|
Chris@0
|
75 408 => 'Request Timeout',
|
Chris@0
|
76 409 => 'Conflict',
|
Chris@0
|
77 410 => 'Gone',
|
Chris@0
|
78 411 => 'Length Required',
|
Chris@0
|
79 412 => 'Precondition Failed',
|
Chris@0
|
80 413 => 'Payload Too Large',
|
Chris@0
|
81 414 => 'URI Too Long',
|
Chris@0
|
82 415 => 'Unsupported Media Type',
|
Chris@0
|
83 416 => 'Range Not Satisfiable',
|
Chris@0
|
84 417 => 'Expectation Failed',
|
Chris@0
|
85 418 => 'I\'m a teapot',
|
Chris@0
|
86 421 => 'Misdirected Request',
|
Chris@0
|
87 422 => 'Unprocessable Entity',
|
Chris@0
|
88 423 => 'Locked',
|
Chris@0
|
89 424 => 'Failed Dependency',
|
Chris@16
|
90 425 => 'Too Early',
|
Chris@0
|
91 426 => 'Upgrade Required',
|
Chris@0
|
92 428 => 'Precondition Required',
|
Chris@0
|
93 429 => 'Too Many Requests',
|
Chris@0
|
94 431 => 'Request Header Fields Too Large',
|
Chris@0
|
95 444 => 'Connection Closed Without Response',
|
Chris@0
|
96 451 => 'Unavailable For Legal Reasons',
|
Chris@0
|
97 // SERVER ERROR
|
Chris@0
|
98 499 => 'Client Closed Request',
|
Chris@0
|
99 500 => 'Internal Server Error',
|
Chris@0
|
100 501 => 'Not Implemented',
|
Chris@0
|
101 502 => 'Bad Gateway',
|
Chris@0
|
102 503 => 'Service Unavailable',
|
Chris@0
|
103 504 => 'Gateway Timeout',
|
Chris@0
|
104 505 => 'HTTP Version Not Supported',
|
Chris@0
|
105 506 => 'Variant Also Negotiates',
|
Chris@0
|
106 507 => 'Insufficient Storage',
|
Chris@0
|
107 508 => 'Loop Detected',
|
Chris@0
|
108 510 => 'Not Extended',
|
Chris@0
|
109 511 => 'Network Authentication Required',
|
Chris@0
|
110 599 => 'Network Connect Timeout Error',
|
Chris@0
|
111 ];
|
Chris@0
|
112
|
Chris@0
|
113 /**
|
Chris@0
|
114 * @var string
|
Chris@0
|
115 */
|
Chris@17
|
116 private $reasonPhrase;
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * @var int
|
Chris@0
|
120 */
|
Chris@0
|
121 private $statusCode;
|
Chris@0
|
122
|
Chris@0
|
123 /**
|
Chris@0
|
124 * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
|
Chris@0
|
125 * @param int $status Status code for the response, if any.
|
Chris@0
|
126 * @param array $headers Headers for the response, if any.
|
Chris@0
|
127 * @throws InvalidArgumentException on any invalid element.
|
Chris@0
|
128 */
|
Chris@0
|
129 public function __construct($body = 'php://memory', $status = 200, array $headers = [])
|
Chris@0
|
130 {
|
Chris@0
|
131 $this->setStatusCode($status);
|
Chris@0
|
132 $this->stream = $this->getStream($body, 'wb+');
|
Chris@0
|
133 $this->setHeaders($headers);
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 /**
|
Chris@0
|
137 * {@inheritdoc}
|
Chris@0
|
138 */
|
Chris@0
|
139 public function getStatusCode()
|
Chris@0
|
140 {
|
Chris@0
|
141 return $this->statusCode;
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 /**
|
Chris@0
|
145 * {@inheritdoc}
|
Chris@0
|
146 */
|
Chris@0
|
147 public function getReasonPhrase()
|
Chris@0
|
148 {
|
Chris@0
|
149 return $this->reasonPhrase;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * {@inheritdoc}
|
Chris@0
|
154 */
|
Chris@0
|
155 public function withStatus($code, $reasonPhrase = '')
|
Chris@0
|
156 {
|
Chris@0
|
157 $new = clone $this;
|
Chris@17
|
158 $new->setStatusCode($code, $reasonPhrase);
|
Chris@0
|
159 return $new;
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 /**
|
Chris@0
|
163 * Set a valid status code.
|
Chris@0
|
164 *
|
Chris@0
|
165 * @param int $code
|
Chris@17
|
166 * @param string $reasonPhrase
|
Chris@0
|
167 * @throws InvalidArgumentException on an invalid status code.
|
Chris@0
|
168 */
|
Chris@17
|
169 private function setStatusCode($code, $reasonPhrase = '')
|
Chris@0
|
170 {
|
Chris@0
|
171 if (! is_numeric($code)
|
Chris@0
|
172 || is_float($code)
|
Chris@0
|
173 || $code < static::MIN_STATUS_CODE_VALUE
|
Chris@0
|
174 || $code > static::MAX_STATUS_CODE_VALUE
|
Chris@0
|
175 ) {
|
Chris@0
|
176 throw new InvalidArgumentException(sprintf(
|
Chris@0
|
177 'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
|
Chris@17
|
178 is_scalar($code) ? $code : gettype($code),
|
Chris@0
|
179 static::MIN_STATUS_CODE_VALUE,
|
Chris@0
|
180 static::MAX_STATUS_CODE_VALUE
|
Chris@0
|
181 ));
|
Chris@0
|
182 }
|
Chris@17
|
183
|
Chris@17
|
184 if (! is_string($reasonPhrase)) {
|
Chris@17
|
185 throw new InvalidArgumentException(sprintf(
|
Chris@17
|
186 'Unsupported response reason phrase; must be a string, received %s',
|
Chris@17
|
187 is_object($reasonPhrase) ? get_class($reasonPhrase) : gettype($reasonPhrase)
|
Chris@17
|
188 ));
|
Chris@17
|
189 }
|
Chris@17
|
190
|
Chris@17
|
191 if ($reasonPhrase === '' && isset($this->phrases[$code])) {
|
Chris@17
|
192 $reasonPhrase = $this->phrases[$code];
|
Chris@17
|
193 }
|
Chris@17
|
194
|
Chris@17
|
195 $this->reasonPhrase = $reasonPhrase;
|
Chris@17
|
196 $this->statusCode = (int) $code;
|
Chris@0
|
197 }
|
Chris@0
|
198 }
|