2 namespace GuzzleHttp\Psr7;
4 use Psr\Http\Message\ResponseInterface;
5 use Psr\Http\Message\StreamInterface;
8 * PSR-7 response implementation.
10 class Response implements ResponseInterface
14 /** @var array Map of standard HTTP status code/reason phrases */
15 private static $phrases = [
17 101 => 'Switching Protocols',
22 203 => 'Non-Authoritative Information',
24 205 => 'Reset Content',
25 206 => 'Partial Content',
26 207 => 'Multi-status',
27 208 => 'Already Reported',
28 300 => 'Multiple Choices',
29 301 => 'Moved Permanently',
32 304 => 'Not Modified',
34 306 => 'Switch Proxy',
35 307 => 'Temporary Redirect',
37 401 => 'Unauthorized',
38 402 => 'Payment Required',
41 405 => 'Method Not Allowed',
42 406 => 'Not Acceptable',
43 407 => 'Proxy Authentication Required',
44 408 => 'Request Time-out',
47 411 => 'Length Required',
48 412 => 'Precondition Failed',
49 413 => 'Request Entity Too Large',
50 414 => 'Request-URI Too Large',
51 415 => 'Unsupported Media Type',
52 416 => 'Requested range not satisfiable',
53 417 => 'Expectation Failed',
54 418 => 'I\'m a teapot',
55 422 => 'Unprocessable Entity',
57 424 => 'Failed Dependency',
58 425 => 'Unordered Collection',
59 426 => 'Upgrade Required',
60 428 => 'Precondition Required',
61 429 => 'Too Many Requests',
62 431 => 'Request Header Fields Too Large',
63 451 => 'Unavailable For Legal Reasons',
64 500 => 'Internal Server Error',
65 501 => 'Not Implemented',
67 503 => 'Service Unavailable',
68 504 => 'Gateway Time-out',
69 505 => 'HTTP Version not supported',
70 506 => 'Variant Also Negotiates',
71 507 => 'Insufficient Storage',
72 508 => 'Loop Detected',
73 511 => 'Network Authentication Required',
77 private $reasonPhrase = '';
80 private $statusCode = 200;
83 * @param int $status Status code
84 * @param array $headers Response headers
85 * @param string|null|resource|StreamInterface $body Response body
86 * @param string $version Protocol version
87 * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
89 public function __construct(
96 $this->statusCode = (int) $status;
98 if ($body !== '' && $body !== null) {
99 $this->stream = stream_for($body);
102 $this->setHeaders($headers);
103 if ($reason == '' && isset(self::$phrases[$this->statusCode])) {
104 $this->reasonPhrase = self::$phrases[$this->statusCode];
106 $this->reasonPhrase = (string) $reason;
109 $this->protocol = $version;
112 public function getStatusCode()
114 return $this->statusCode;
117 public function getReasonPhrase()
119 return $this->reasonPhrase;
122 public function withStatus($code, $reasonPhrase = '')
125 $new->statusCode = (int) $code;
126 if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) {
127 $reasonPhrase = self::$phrases[$new->statusCode];
129 $new->reasonPhrase = $reasonPhrase;