1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class representing a Value Object of the HTTP Status-Line, as |
4
|
|
|
* specified in RFC 2616 and RFC 7231. |
5
|
|
|
* |
6
|
|
|
* PHP version 5.3 |
7
|
|
|
* |
8
|
|
|
* @category StatusLine |
9
|
|
|
* |
10
|
|
|
* @package Teapot\StatusLine |
11
|
|
|
* |
12
|
|
|
* @author Barney Hanlon <[email protected]> |
13
|
|
|
* @copyright 2013-2016 B Hanlon. All rights reserved. |
14
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
15
|
|
|
* @link https://shrikeh.github.com/teapot |
16
|
|
|
*/ |
17
|
|
|
namespace Teapot\StatusLine; |
18
|
|
|
|
19
|
|
|
use Teapot\StatusCode; |
20
|
|
|
use Teapot\StatusLine; |
21
|
|
|
use Teapot\StatusCodeException\InvalidStatusCodeException; |
22
|
|
|
use Psr\Http\Message\ResponseInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class representing a Value Object of the HTTP Status-Line, as |
26
|
|
|
* specified in RFC 2616 and RFC 7231. |
27
|
|
|
* |
28
|
|
|
* PHP version 5.3 |
29
|
|
|
* |
30
|
|
|
* @category StatusLine |
31
|
|
|
* |
32
|
|
|
* @package Teapot |
33
|
|
|
* |
34
|
|
|
* @author Barney Hanlon <[email protected]> |
35
|
|
|
* @copyright 2013-2016 B Hanlon. All rights reserved. |
36
|
|
|
* @license MIT http://opensource.org/licenses/MIT |
37
|
|
|
* @link https://shrikeh.github.com/teapot |
38
|
|
|
*/ |
39
|
|
|
final class ResponseStatusLine implements StatusLine |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* The actual response code. |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
private $code; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The reason phrase. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private $reason; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
* |
58
|
|
|
* @param int $code The response code |
59
|
|
|
* @param string $reason The reason phrase |
60
|
|
|
*/ |
61
|
|
|
public function __construct($code, $reason) |
62
|
|
|
{ |
63
|
|
|
$this->setCode($code); |
64
|
|
|
$this->reason = $reason; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Return the response code. |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function statusCode() |
73
|
|
|
{ |
74
|
|
|
return $this->code; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the reason phrase |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function reasonPhrase() |
83
|
|
|
{ |
84
|
|
|
return (string) $this->reason; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add the status code and reason phrase to a Response. |
89
|
|
|
* |
90
|
|
|
* @param ResponseInterface $response The response |
91
|
|
|
* @return ResponseInterface |
92
|
|
|
*/ |
93
|
|
|
public function response(ResponseInterface $response) |
94
|
|
|
{ |
95
|
|
|
return $response->withStatus( |
96
|
|
|
$this->statusCode(), |
97
|
|
|
$this->reasonPhrase() |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the status class of the response code. |
103
|
|
|
* |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
|
|
public function statusClass() |
107
|
|
|
{ |
108
|
|
|
return (int) floor($this->code / 100); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Helper to establish if the class of the status code |
113
|
|
|
* is informational (1xx). |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function isInformational() |
118
|
|
|
{ |
119
|
|
|
return $this->isStatusClass(StatusCode::INFORMATIONAL); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Helper to establish if the class of the status code |
124
|
|
|
* is successful (2xx). |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function isSuccessful() |
129
|
|
|
{ |
130
|
|
|
return $this->isStatusClass(StatusCode::SUCCESSFUL); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Helper to establish if the class of the status code |
135
|
|
|
* is redirection (3xx). |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isRedirection() |
140
|
|
|
{ |
141
|
|
|
return $this->isStatusClass(StatusCode::REDIRECTION); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Helper to establish if the class of the status code |
146
|
|
|
* is client error (4xx). |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function isClientError() |
151
|
|
|
{ |
152
|
|
|
return $this->isStatusClass(StatusCode::CLIENT_ERROR); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Helper to establish if the class of the status code |
157
|
|
|
* is server error (5xx). |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public function isServerError() |
162
|
|
|
{ |
163
|
|
|
return $this->isStatusClass(StatusCode::SERVER_ERROR); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set the code. Used in constructor to ensure the code meets the |
168
|
|
|
* requirements for a status code. |
169
|
|
|
* |
170
|
|
|
* @param int $code The status code |
171
|
|
|
* @throws InvalidStatusCodeException If the status code is invalid |
172
|
|
|
*/ |
173
|
|
|
private function setCode($code) |
174
|
|
|
{ |
175
|
|
|
if (!is_numeric($code)) { |
176
|
|
|
throw InvalidStatusCodeException::notNumeric($code); |
177
|
|
|
} |
178
|
|
|
$code = (int) $code; |
179
|
|
|
|
180
|
|
|
if ($code < 100) { |
181
|
|
|
throw InvalidStatusCodeException::notGreaterOrEqualTo100($code); |
182
|
|
|
} |
183
|
|
|
$this->code = $code; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Test whether the response class matches the class passed to it. |
188
|
|
|
* |
189
|
|
|
* @param int $class The class of the response code |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
private function isStatusClass($class) |
193
|
|
|
{ |
194
|
|
|
return $this->statusClass() === $class; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|