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