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