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