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