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