| 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Psr7;
|
|---|
| 3 |
|
|---|
| 4 | use Psr\Http\Message\ResponseInterface;
|
|---|
| 5 |
|
|---|
| 6 | /**
|
|---|
| 7 | * PSR-7 response implementation.
|
|---|
| 8 | */
|
|---|
| 9 | class Response implements ResponseInterface
|
|---|
| 10 | {
|
|---|
| 11 | use MessageTrait;
|
|---|
| 12 |
|
|---|
| 13 | /** @var array Map of standard HTTP status code/reason phrases */
|
|---|
| 14 | private static $phrases = [
|
|---|
| 15 | 100 => 'Continue',
|
|---|
| 16 | 101 => 'Switching Protocols',
|
|---|
| 17 | 102 => 'Processing',
|
|---|
| 18 | 200 => 'OK',
|
|---|
| 19 | 201 => 'Created',
|
|---|
| 20 | 202 => 'Accepted',
|
|---|
| 21 | 203 => 'Non-Authoritative Information',
|
|---|
| 22 | 204 => 'No Content',
|
|---|
| 23 | 205 => 'Reset Content',
|
|---|
| 24 | 206 => 'Partial Content',
|
|---|
| 25 | 207 => 'Multi-status',
|
|---|
| 26 | 208 => 'Already Reported',
|
|---|
| 27 | 300 => 'Multiple Choices',
|
|---|
| 28 | 301 => 'Moved Permanently',
|
|---|
| 29 | 302 => 'Found',
|
|---|
| 30 | 303 => 'See Other',
|
|---|
| 31 | 304 => 'Not Modified',
|
|---|
| 32 | 305 => 'Use Proxy',
|
|---|
| 33 | 306 => 'Switch Proxy',
|
|---|
| 34 | 307 => 'Temporary Redirect',
|
|---|
| 35 | 400 => 'Bad Request',
|
|---|
| 36 | 401 => 'Unauthorized',
|
|---|
| 37 | 402 => 'Payment Required',
|
|---|
| 38 | 403 => 'Forbidden',
|
|---|
| 39 | 404 => 'Not Found',
|
|---|
| 40 | 405 => 'Method Not Allowed',
|
|---|
| 41 | 406 => 'Not Acceptable',
|
|---|
| 42 | 407 => 'Proxy Authentication Required',
|
|---|
| 43 | 408 => 'Request Time-out',
|
|---|
| 44 | 409 => 'Conflict',
|
|---|
| 45 | 410 => 'Gone',
|
|---|
| 46 | 411 => 'Length Required',
|
|---|
| 47 | 412 => 'Precondition Failed',
|
|---|
| 48 | 413 => 'Request Entity Too Large',
|
|---|
| 49 | 414 => 'Request-URI Too Large',
|
|---|
| 50 | 415 => 'Unsupported Media Type',
|
|---|
| 51 | 416 => 'Requested range not satisfiable',
|
|---|
| 52 | 417 => 'Expectation Failed',
|
|---|
| 53 | 418 => 'I\'m a teapot',
|
|---|
| 54 | 422 => 'Unprocessable Entity',
|
|---|
| 55 | 423 => 'Locked',
|
|---|
| 56 | 424 => 'Failed Dependency',
|
|---|
| 57 | 425 => 'Unordered Collection',
|
|---|
| 58 | 426 => 'Upgrade Required',
|
|---|
| 59 | 428 => 'Precondition Required',
|
|---|
| 60 | 429 => 'Too Many Requests',
|
|---|
| 61 | 431 => 'Request Header Fields Too Large',
|
|---|
| 62 | 500 => 'Internal Server Error',
|
|---|
| 63 | 501 => 'Not Implemented',
|
|---|
| 64 | 502 => 'Bad Gateway',
|
|---|
| 65 | 503 => 'Service Unavailable',
|
|---|
| 66 | 504 => 'Gateway Time-out',
|
|---|
| 67 | 505 => 'HTTP Version not supported',
|
|---|
| 68 | 506 => 'Variant Also Negotiates',
|
|---|
| 69 | 507 => 'Insufficient Storage',
|
|---|
| 70 | 508 => 'Loop Detected',
|
|---|
| 71 | 511 => 'Network Authentication Required',
|
|---|
| 72 | ];
|
|---|
| 73 |
|
|---|
| 74 | /** @var null|string */
|
|---|
| 75 | private $reasonPhrase = '';
|
|---|
| 76 |
|
|---|
| 77 | /** @var int */
|
|---|
| 78 | private $statusCode = 200;
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * @param int $status Status code for the response, if any.
|
|---|
| 82 | * @param array $headers Headers for the response, if any.
|
|---|
| 83 | * @param mixed $body Stream body.
|
|---|
| 84 | * @param string $version Protocol version.
|
|---|
| 85 | * @param string $reason Reason phrase (a default will be used if possible).
|
|---|
| 86 | */
|
|---|
| 87 | public function __construct(
|
|---|
| 88 | $status = 200,
|
|---|
| 89 | array $headers = [],
|
|---|
| 90 | $body = null,
|
|---|
| 91 | $version = '1.1',
|
|---|
| 92 | $reason = null
|
|---|
| 93 | ) {
|
|---|
| 94 | $this->statusCode = (int) $status;
|
|---|
| 95 |
|
|---|
| 96 | if ($body !== null) {
|
|---|
| 97 | $this->stream = stream_for($body);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | $this->setHeaders($headers);
|
|---|
| 101 | if (!$reason && isset(self::$phrases[$this->statusCode])) {
|
|---|
| 102 | $this->reasonPhrase = self::$phrases[$status];
|
|---|
| 103 | } else {
|
|---|
| 104 | $this->reasonPhrase = (string) $reason;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | $this->protocol = $version;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | public function getStatusCode()
|
|---|
| 111 | {
|
|---|
| 112 | return $this->statusCode;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | public function getReasonPhrase()
|
|---|
| 116 | {
|
|---|
| 117 | return $this->reasonPhrase;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | public function withStatus($code, $reasonPhrase = '')
|
|---|
| 121 | {
|
|---|
| 122 | $new = clone $this;
|
|---|
| 123 | $new->statusCode = (int) $code;
|
|---|
| 124 | if (!$reasonPhrase && isset(self::$phrases[$new->statusCode])) {
|
|---|
| 125 | $reasonPhrase = self::$phrases[$new->statusCode];
|
|---|
| 126 | }
|
|---|
| 127 | $new->reasonPhrase = $reasonPhrase;
|
|---|
| 128 | return $new;
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|