1
|
|
|
<?php |
2
|
|
|
namespace DevOp\Core\Http; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\ResponseInterface; |
5
|
|
|
|
6
|
|
|
class Response implements ResponseInterface |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
use MessageTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @link https://gist.github.com/Stoffo/53e093450aed067a8fa8 |
13
|
|
|
* */ |
14
|
|
|
private static $phrases = array( |
15
|
|
|
100 => 'Continue', |
16
|
|
|
101 => 'Switching Protocols', |
17
|
|
|
102 => 'Processing', |
18
|
|
|
200 => 'OK', |
19
|
|
|
201 => 'Created', |
20
|
|
|
202 => 'Accepted', |
21
|
|
|
203 => 'Non-Authoritative Information', |
22
|
|
|
204 => 'No Content', |
23
|
|
|
205 => 'Reset Content', |
24
|
|
|
206 => 'Partial Content', |
25
|
|
|
207 => 'Multi-Status', |
26
|
|
|
300 => 'Multiple Choices', |
27
|
|
|
301 => 'Moved Permanently', |
28
|
|
|
302 => 'Found', |
29
|
|
|
303 => 'See Other', |
30
|
|
|
304 => 'Not Modified', |
31
|
|
|
305 => 'Use Proxy', |
32
|
|
|
306 => 'Switch Proxy', |
33
|
|
|
307 => 'Temporary Redirect', |
34
|
|
|
400 => 'Bad Request', |
35
|
|
|
401 => 'Unauthorized', |
36
|
|
|
402 => 'Payment Required', |
37
|
|
|
403 => 'Forbidden', |
38
|
|
|
404 => 'Not Found', |
39
|
|
|
405 => 'Method Not Allowed', |
40
|
|
|
406 => 'Not Acceptable', |
41
|
|
|
407 => 'Proxy Authentication Required', |
42
|
|
|
408 => 'Request Timeout', |
43
|
|
|
409 => 'Conflict', |
44
|
|
|
410 => 'Gone', |
45
|
|
|
411 => 'Length Required', |
46
|
|
|
412 => 'Precondition Failed', |
47
|
|
|
413 => 'Request Entity Too Large', |
48
|
|
|
414 => 'Request-URI Too Long', |
49
|
|
|
415 => 'Unsupported Media Type', |
50
|
|
|
416 => 'Requested Range Not Satisfiable', |
51
|
|
|
417 => 'Expectation Failed', |
52
|
|
|
418 => 'I\'m a teapot', |
53
|
|
|
422 => 'Unprocessable Entity', |
54
|
|
|
423 => 'Locked', |
55
|
|
|
424 => 'Failed Dependency', |
56
|
|
|
425 => 'Unordered Collection', |
57
|
|
|
426 => 'Upgrade Required', |
58
|
|
|
449 => 'Retry With', |
59
|
|
|
450 => 'Blocked by Windows Parental Controls', |
60
|
|
|
500 => 'Internal Server Error', |
61
|
|
|
501 => 'Not Implemented', |
62
|
|
|
502 => 'Bad Gateway', |
63
|
|
|
503 => 'Service Unavailable', |
64
|
|
|
504 => 'Gateway Timeout', |
65
|
|
|
505 => 'HTTP Version Not Supported', |
66
|
|
|
506 => 'Variant Also Negotiates', |
67
|
|
|
507 => 'Insufficient Storage', |
68
|
|
|
509 => 'Bandwidth Limit Exceeded', |
69
|
|
|
510 => 'Not Extended' |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @var int |
75
|
|
|
*/ |
76
|
|
|
private $statusCode; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
private $reasonPhrase; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @var int |
87
|
|
|
*/ |
88
|
2 |
|
public function getStatusCode() |
89
|
|
|
{ |
90
|
2 |
|
return $this->statusCode; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param int $code |
95
|
|
|
* @param string $reasonPhrase |
96
|
|
|
* @return self |
97
|
|
|
*/ |
98
|
2 |
|
public function withStatus($code, $reasonPhrase = '') |
99
|
|
|
{ |
100
|
2 |
|
if ($code === $this->statusCode) { |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
$clone = clone $this; |
105
|
2 |
|
$clone->statusCode = $code; |
106
|
|
|
|
107
|
2 |
|
if (empty($reasonPhrase) && isset(self::$phrases[$code])) { |
108
|
2 |
|
$reasonPhrase = self::$phrases[$code]; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
$clone->reasonPhrase = $reasonPhrase; |
112
|
|
|
|
113
|
2 |
|
return $clone; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* |
118
|
|
|
* @var string |
119
|
|
|
*/ |
120
|
|
|
public function getReasonPhrase() |
121
|
|
|
{ |
122
|
|
|
return $this->reasonPhrase; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|