1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HTTP Status |
4
|
|
|
* |
5
|
|
|
* PHP version 5 |
6
|
|
|
* |
7
|
|
|
* Copyright (C) 2016 Jake Johns |
8
|
|
|
* |
9
|
|
|
* This software may be modified and distributed under the terms |
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
11
|
|
|
* |
12
|
|
|
* @category Class |
13
|
|
|
* @package Jnjxp\HttpStatus |
14
|
|
|
* @author Jake Johns <[email protected]> |
15
|
|
|
* @copyright 2016 Jake Johns |
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
17
|
|
|
* @link https://github.com/jnjxp/jnjxp.http-status |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Jnjxp\HttpStatus; |
21
|
|
|
|
22
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Response Class |
26
|
|
|
* |
27
|
|
|
* @category Class |
28
|
|
|
* @package Jnjxp\HttpStatus |
29
|
|
|
* @author Jake Johns <[email protected]> |
30
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
31
|
|
|
* @link https://github.com/jnjxp/jnjxp.http-status |
32
|
|
|
*/ |
33
|
|
|
class ResponseClass |
34
|
|
|
{ |
35
|
|
|
const INFORMATIONAL = 'INFORMATIONAL'; |
36
|
|
|
const SUCCESS = 'SUCCESSFUL'; |
37
|
|
|
const REDIRECTION = 'REDIRECTION'; |
38
|
|
|
const CLIENT_ERROR = 'CLIENT_ERROR'; |
39
|
|
|
const SERVER_ERROR = 'SERVER_ERROR'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Classes |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
* |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected $classes = [ |
49
|
|
|
1 => self::INFORMATIONAL, |
50
|
|
|
2 => self::SUCCESS, |
51
|
|
|
3 => self::REDIRECTION, |
52
|
|
|
4 => self::CLIENT_ERROR, |
53
|
|
|
5 => self::SERVER_ERROR, |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the classification for an HTTP Status Code |
58
|
|
|
* |
59
|
|
|
* @param int|Response $code HTTP Status Code |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
* |
63
|
|
|
* @access public |
64
|
|
|
*/ |
65
|
2001 |
|
public function getClass($code) |
66
|
|
|
{ |
67
|
2001 |
|
$code = $this->fixCode($code); |
68
|
2000 |
|
$prefix = substr($code, 0, 1); |
69
|
2000 |
|
return $this->classes[$prefix]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get a valid HTTP Status Code |
74
|
|
|
* |
75
|
|
|
* @param Response|int $code HTTP Status Code |
76
|
|
|
* |
77
|
|
|
* @return int |
78
|
|
|
* @throws Exception if invalid HTTP Status Code |
79
|
|
|
* |
80
|
|
|
* @access protected |
81
|
|
|
*/ |
82
|
2001 |
|
protected function fixCode($code) |
83
|
|
|
{ |
84
|
2001 |
|
if ($code instanceof Response) { |
85
|
1000 |
|
$code = $code->getStatusCode(); |
86
|
1000 |
|
} |
87
|
|
|
|
88
|
2001 |
|
$this->assertValidHttpCode($code); |
89
|
|
|
|
90
|
2000 |
|
return $code; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Assert Valid HTTP Code |
95
|
|
|
* |
96
|
|
|
* @param int $code HTTP Status Code |
97
|
|
|
* |
98
|
|
|
* @return null |
99
|
|
|
* @throws Exception if invalid HTTP Status Code; |
100
|
|
|
* |
101
|
|
|
* @access protected |
102
|
|
|
*/ |
103
|
2001 |
|
protected function assertValidHttpCode($code) |
104
|
|
|
{ |
105
|
2001 |
|
if (! is_numeric($code) |
106
|
2001 |
|
|| is_float($code) |
107
|
2001 |
|
|| $code < 100 |
108
|
2001 |
|
|| $code >= 600 |
109
|
2001 |
|
) { |
110
|
|
|
|
111
|
1 |
|
$msg = sprintf( |
112
|
|
|
'Invalid status code "%s"; ' |
113
|
1 |
|
. 'must be an integer between 100 and 599, inclusive', |
114
|
1 |
|
(is_scalar($code) ? $code : gettype($code)) |
115
|
1 |
|
); |
116
|
|
|
|
117
|
1 |
|
throw new Exception($msg); |
118
|
|
|
} |
119
|
2000 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Is Response of given class? |
123
|
|
|
* |
124
|
|
|
* @param string $class class to test for |
125
|
|
|
* @param int|Response $code code to test |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
* |
129
|
|
|
* @access public |
130
|
|
|
*/ |
131
|
1000 |
|
public function isResponse($class, $code) |
132
|
|
|
{ |
133
|
1000 |
|
return $class == $this->getClass($code); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Is Status Informational? |
138
|
|
|
* |
139
|
|
|
* @param int|Response $code HTTP Status Code |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
* |
143
|
|
|
* @access public |
144
|
|
|
*/ |
145
|
1000 |
|
public function isInformational($code) |
146
|
|
|
{ |
147
|
1000 |
|
return $this->isResponse(self::INFORMATIONAL, $code); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Is Status a Success? |
152
|
|
|
* |
153
|
|
|
* @param int|Response $code HTTP Status Code |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
* |
157
|
|
|
* @access public |
158
|
|
|
*/ |
159
|
1000 |
|
public function isSuccess($code) |
160
|
|
|
{ |
161
|
1000 |
|
return $this->isResponse(self::SUCCESS, $code); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Is Status a Redirect? |
166
|
|
|
* |
167
|
|
|
* @param int|Response $code HTTP Status Code |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
* |
171
|
|
|
* @access public |
172
|
|
|
*/ |
173
|
1000 |
|
public function isRedirection($code) |
174
|
|
|
{ |
175
|
1000 |
|
return $this->isResponse(self::REDIRECTION, $code); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Is Status a Client Error? |
180
|
|
|
* |
181
|
|
|
* @param int|Response $code HTTP Status Code |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
* |
185
|
|
|
* @access public |
186
|
|
|
*/ |
187
|
1000 |
|
public function isClientError($code) |
188
|
|
|
{ |
189
|
1000 |
|
return $this->isResponse(self::CLIENT_ERROR, $code); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Is Status a Server Error? |
194
|
|
|
* |
195
|
|
|
* @param int|Response $code HTTP Status Code |
196
|
|
|
* |
197
|
|
|
* @return bool |
198
|
|
|
* |
199
|
|
|
* @access public |
200
|
|
|
*/ |
201
|
1000 |
|
public function isServerError($code) |
202
|
|
|
{ |
203
|
1000 |
|
return $this->isResponse(self::SERVER_ERROR, $code); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|