Debian Codegraph @

vendor/guzzlehttp/psr7/src/Response.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace GuzzleHttp\Psr7;
6
7 use Psr\Http\Message\ResponseInterface;
8 use Psr\Http\Message\StreamInterface;
9
10 /**
11 * PSR-7 response implementation.
12 */
13 class Response implements ResponseInterface
14 {
15 use MessageTrait;
16
17 /** Map of standard HTTP status code/reason phrases */
18 private const PHRASES = [
19 100 => 'Continue',
20 101 => 'Switching Protocols',
21 102 => 'Processing',
22 200 => 'OK',
23 201 => 'Created',
24 202 => 'Accepted',
25 203 => 'Non-Authoritative Information',
26 204 => 'No Content',
27 205 => 'Reset Content',
28 206 => 'Partial Content',
29 207 => 'Multi-status',
30 208 => 'Already Reported',
31 300 => 'Multiple Choices',
32 301 => 'Moved Permanently',
33 302 => 'Found',
34 303 => 'See Other',
35 304 => 'Not Modified',
36 305 => 'Use Proxy',
37 306 => 'Switch Proxy',
38 307 => 'Temporary Redirect',
39 308 => 'Permanent Redirect',
40 400 => 'Bad Request',
41 401 => 'Unauthorized',
42 402 => 'Payment Required',
43 403 => 'Forbidden',
44 404 => 'Not Found',
45 405 => 'Method Not Allowed',
46 406 => 'Not Acceptable',
47 407 => 'Proxy Authentication Required',
48 408 => 'Request Time-out',
49 409 => 'Conflict',
50 410 => 'Gone',
51 411 => 'Length Required',
52 412 => 'Precondition Failed',
53 413 => 'Request Entity Too Large',
54 414 => 'Request-URI Too Large',
55 415 => 'Unsupported Media Type',
56 416 => 'Requested range not satisfiable',
57 417 => 'Expectation Failed',
58 418 => 'I\'m a teapot',
59 422 => 'Unprocessable Entity',
60 423 => 'Locked',
61 424 => 'Failed Dependency',
62 425 => 'Unordered Collection',
63 426 => 'Upgrade Required',
64 428 => 'Precondition Required',
65 429 => 'Too Many Requests',
66 431 => 'Request Header Fields Too Large',
67 451 => 'Unavailable For Legal Reasons',
68 500 => 'Internal Server Error',
69 501 => 'Not Implemented',
70 502 => 'Bad Gateway',
71 503 => 'Service Unavailable',
72 504 => 'Gateway Time-out',
73 505 => 'HTTP Version not supported',
74 506 => 'Variant Also Negotiates',
75 507 => 'Insufficient Storage',
76 508 => 'Loop Detected',
77 510 => 'Not Extended',
78 511 => 'Network Authentication Required',
79 ];
80
81 /** @var string */
82 private $reasonPhrase;
83
84 /** @var int */
85 private $statusCode;
86
87 /**
88 * @param int $status Status code
89 * @param (string|string[])[] $headers Response headers
90 * @param string|resource|StreamInterface|null $body Response body
91 * @param string $version Protocol version
92 * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
93 */
94 public function __construct(
95 int $status = 200,
96 array $headers = [],
97 $body = null,
98 string $version = '1.1',
99 ?string $reason = null
100 ) {
101 $this->assertStatusCodeRange($status);
102 $this->assertProtocolVersion($version);
103
104 $this->statusCode = $status;
105
106 if ($body !== '' && $body !== null) {
107 $this->stream = Utils::streamFor($body);
108 }
109
110 $this->setHeaders($headers);
111 if ($reason == '' && isset(self::PHRASES[$this->statusCode])) {
112 $reasonPhrase = self::PHRASES[$this->statusCode];
113 } else {
114 $reasonPhrase = (string) $reason;
115 }
116
117 $this->assertNoLineSeparators($reasonPhrase, 'Reason phrase');
118 $this->reasonPhrase = $reasonPhrase;
119
120 $this->protocol = $version;
121 }
122
123 public function getStatusCode(): int
124 {
125 return $this->statusCode;
126 }
127
128 public function getReasonPhrase(): string
129 {
130 return $this->reasonPhrase;
131 }
132
133 public function withStatus($code, $reasonPhrase = ''): ResponseInterface
134 {
135 if (!\is_int($code) && \filter_var($code, \FILTER_VALIDATE_INT) !== false) {
136 \trigger_deprecation(
137 'guzzlehttp/psr7',
138 '2.11',
139 'Passing %s to ResponseInterface::withStatus() is deprecated; guzzlehttp/psr7 3.0 requires int for $code.',
140 \get_debug_type($code)
141 );
142 }
143
144 if (!\is_string($reasonPhrase)) {
145 \trigger_deprecation(
146 'guzzlehttp/psr7',
147 '2.11',
148 'Passing %s to ResponseInterface::withStatus() is deprecated; guzzlehttp/psr7 3.0 requires string for $reasonPhrase.',
149 \get_debug_type($reasonPhrase)
150 );
151 }
152
153 $this->assertStatusCodeIsInteger($code);
154 $code = (int) $code;
155 $this->assertStatusCodeRange($code);
156
157 $new = clone $this;
158 $new->statusCode = $code;
159 if ($reasonPhrase == '' && isset(self::PHRASES[$new->statusCode])) {
160 $reasonPhrase = self::PHRASES[$new->statusCode];
161 }
162 $reasonPhrase = (string) $reasonPhrase;
163 $this->assertNoLineSeparators($reasonPhrase, 'Reason phrase');
164 $new->reasonPhrase = $reasonPhrase;
165
166 return $new;
167 }
168
169 /**
170 * @param mixed $statusCode
171 */
172 private function assertStatusCodeIsInteger($statusCode): void
173 {
174 if (filter_var($statusCode, FILTER_VALIDATE_INT) === false) {
175 throw new \InvalidArgumentException('Status code must be an integer value.');
176 }
177 }
178
179 private function assertStatusCodeRange(int $statusCode): void
180 {
181 if ($statusCode < 100 || $statusCode >= 600) {
182 throw new \InvalidArgumentException('Status code must be an integer value between 1xx and 5xx.');
183 }
184 }
185 }