Security update for Core, with self-updated composer
[yaffs-website] / vendor / zendframework / zend-diactoros / src / Response.php
1 <?php
2 /**
3  * @see       https://github.com/zendframework/zend-diactoros for the canonical source repository
4  * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5  * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6  */
7
8 namespace Zend\Diactoros;
9
10 use InvalidArgumentException;
11 use Psr\Http\Message\ResponseInterface;
12 use Psr\Http\Message\StreamInterface;
13
14 /**
15  * HTTP response encapsulation.
16  *
17  * Responses are considered immutable; all methods that might change state are
18  * implemented such that they retain the internal state of the current
19  * message and return a new instance that contains the changed state.
20  */
21 class Response implements ResponseInterface
22 {
23     use MessageTrait;
24
25     const MIN_STATUS_CODE_VALUE = 100;
26     const MAX_STATUS_CODE_VALUE = 599;
27
28     /**
29      * Map of standard HTTP status code/reason phrases
30      *
31      * @var array
32      */
33     private $phrases = [
34         // INFORMATIONAL CODES
35         100 => 'Continue',
36         101 => 'Switching Protocols',
37         102 => 'Processing',
38         103 => 'Early Hints',
39         // SUCCESS CODES
40         200 => 'OK',
41         201 => 'Created',
42         202 => 'Accepted',
43         203 => 'Non-Authoritative Information',
44         204 => 'No Content',
45         205 => 'Reset Content',
46         206 => 'Partial Content',
47         207 => 'Multi-Status',
48         208 => 'Already Reported',
49         226 => 'IM Used',
50         // REDIRECTION CODES
51         300 => 'Multiple Choices',
52         301 => 'Moved Permanently',
53         302 => 'Found',
54         303 => 'See Other',
55         304 => 'Not Modified',
56         305 => 'Use Proxy',
57         306 => 'Switch Proxy', // Deprecated to 306 => '(Unused)'
58         307 => 'Temporary Redirect',
59         308 => 'Permanent Redirect',
60         // CLIENT ERROR
61         400 => 'Bad Request',
62         401 => 'Unauthorized',
63         402 => 'Payment Required',
64         403 => 'Forbidden',
65         404 => 'Not Found',
66         405 => 'Method Not Allowed',
67         406 => 'Not Acceptable',
68         407 => 'Proxy Authentication Required',
69         408 => 'Request Timeout',
70         409 => 'Conflict',
71         410 => 'Gone',
72         411 => 'Length Required',
73         412 => 'Precondition Failed',
74         413 => 'Payload Too Large',
75         414 => 'URI Too Long',
76         415 => 'Unsupported Media Type',
77         416 => 'Range Not Satisfiable',
78         417 => 'Expectation Failed',
79         418 => 'I\'m a teapot',
80         421 => 'Misdirected Request',
81         422 => 'Unprocessable Entity',
82         423 => 'Locked',
83         424 => 'Failed Dependency',
84         425 => 'Unordered Collection',
85         426 => 'Upgrade Required',
86         428 => 'Precondition Required',
87         429 => 'Too Many Requests',
88         431 => 'Request Header Fields Too Large',
89         444 => 'Connection Closed Without Response',
90         451 => 'Unavailable For Legal Reasons',
91         // SERVER ERROR
92         499 => 'Client Closed Request',
93         500 => 'Internal Server Error',
94         501 => 'Not Implemented',
95         502 => 'Bad Gateway',
96         503 => 'Service Unavailable',
97         504 => 'Gateway Timeout',
98         505 => 'HTTP Version Not Supported',
99         506 => 'Variant Also Negotiates',
100         507 => 'Insufficient Storage',
101         508 => 'Loop Detected',
102         510 => 'Not Extended',
103         511 => 'Network Authentication Required',
104         599 => 'Network Connect Timeout Error',
105     ];
106
107     /**
108      * @var string
109      */
110     private $reasonPhrase = '';
111
112     /**
113      * @var int
114      */
115     private $statusCode;
116
117     /**
118      * @param string|resource|StreamInterface $body Stream identifier and/or actual stream resource
119      * @param int $status Status code for the response, if any.
120      * @param array $headers Headers for the response, if any.
121      * @throws InvalidArgumentException on any invalid element.
122      */
123     public function __construct($body = 'php://memory', $status = 200, array $headers = [])
124     {
125         $this->setStatusCode($status);
126         $this->stream = $this->getStream($body, 'wb+');
127         $this->setHeaders($headers);
128     }
129
130     /**
131      * {@inheritdoc}
132      */
133     public function getStatusCode()
134     {
135         return $this->statusCode;
136     }
137
138     /**
139      * {@inheritdoc}
140      */
141     public function getReasonPhrase()
142     {
143         if (! $this->reasonPhrase
144             && isset($this->phrases[$this->statusCode])
145         ) {
146             $this->reasonPhrase = $this->phrases[$this->statusCode];
147         }
148
149         return $this->reasonPhrase;
150     }
151
152     /**
153      * {@inheritdoc}
154      */
155     public function withStatus($code, $reasonPhrase = '')
156     {
157         $new = clone $this;
158         $new->setStatusCode($code);
159         $new->reasonPhrase = $reasonPhrase;
160         return $new;
161     }
162
163     /**
164      * Set a valid status code.
165      *
166      * @param int $code
167      * @throws InvalidArgumentException on an invalid status code.
168      */
169     private function setStatusCode($code)
170     {
171         if (! is_numeric($code)
172             || is_float($code)
173             || $code < static::MIN_STATUS_CODE_VALUE
174             || $code > static::MAX_STATUS_CODE_VALUE
175         ) {
176             throw new InvalidArgumentException(sprintf(
177                 'Invalid status code "%s"; must be an integer between %d and %d, inclusive',
178                 (is_scalar($code) ? $code : gettype($code)),
179                 static::MIN_STATUS_CODE_VALUE,
180                 static::MAX_STATUS_CODE_VALUE
181             ));
182         }
183         $this->statusCode = $code;
184     }
185 }