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