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