|
1
|
|
|
<?php namespace Comodojo\Dispatcher\Components; |
|
2
|
|
|
|
|
3
|
|
|
use \Exception; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @package Comodojo Dispatcher |
|
7
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
8
|
|
|
* @author Marco Castiello <[email protected]> |
|
9
|
|
|
* @license MIT |
|
10
|
|
|
* |
|
11
|
|
|
* LICENSE: |
|
12
|
|
|
* |
|
13
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
14
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
15
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
16
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
17
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
18
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
19
|
|
|
* THE SOFTWARE. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
class HttpStatusCodes { |
|
23
|
|
|
|
|
24
|
|
|
private $codes = array( |
|
25
|
|
|
// Informational 1xx |
|
26
|
|
|
100 => 'Continue', |
|
27
|
|
|
101 => 'Switching Protocols', |
|
28
|
|
|
102 => 'Processing', |
|
29
|
|
|
// Successful 2xx |
|
30
|
|
|
200 => 'OK', |
|
31
|
|
|
201 => 'Created', |
|
32
|
|
|
202 => 'Accepted', |
|
33
|
|
|
203 => 'Non-Authoritative Information', |
|
34
|
|
|
204 => 'No Content', |
|
35
|
|
|
205 => 'Reset Content', |
|
36
|
|
|
206 => 'Partial Content', |
|
37
|
|
|
207 => 'Multi-Status', // missing |
|
38
|
|
|
208 => 'Already Reported', // missing |
|
39
|
|
|
226 => 'IM Used', // missing |
|
40
|
|
|
// Redirection 3xx |
|
41
|
|
|
300 => 'Multiple Choices', |
|
42
|
|
|
301 => 'Moved Permanently', |
|
43
|
|
|
302 => 'Found', |
|
44
|
|
|
303 => 'See Other', |
|
45
|
|
|
304 => 'Not Modified', |
|
46
|
|
|
305 => 'Use Proxy', |
|
47
|
|
|
307 => 'Temporary Redirect', |
|
48
|
|
|
308 => 'Permanent Redirect', |
|
49
|
|
|
// Client Error 4xx |
|
50
|
|
|
400 => 'Bad Request', |
|
51
|
|
|
401 => 'Unauthorized', // missing |
|
52
|
|
|
402 => 'Payment Required', // missing |
|
53
|
|
|
403 => 'Forbidden', |
|
54
|
|
|
404 => 'Not Found', |
|
55
|
|
|
405 => 'Method Not Allowed', |
|
56
|
|
|
406 => 'Not Acceptable', // missing |
|
57
|
|
|
407 => 'Proxy Authentication Required', // missing |
|
58
|
|
|
408 => 'Request Timeout', // missing |
|
59
|
|
|
409 => 'Conflict', // missing |
|
60
|
|
|
410 => 'Gone', |
|
61
|
|
|
411 => 'Length Required', // missing |
|
62
|
|
|
412 => 'Precondition Failed', // missing |
|
63
|
|
|
413 => 'Payload Too Large', // missing |
|
64
|
|
|
414 => 'URI Too Long', // missing |
|
65
|
|
|
415 => 'Unsupported Media Type', // missing |
|
66
|
|
|
416 => 'Range Not Satisfiable', // missing |
|
67
|
|
|
417 => 'Expectation Failed', // missing |
|
68
|
|
|
421 => 'Misdirected Request', // missing |
|
69
|
|
|
422 => 'Unprocessable Entity', // missing |
|
70
|
|
|
423 => 'Locked', // missing |
|
71
|
|
|
424 => 'Failed Dependency', // missing |
|
72
|
|
|
426 => 'Upgrade Required', // missing |
|
73
|
|
|
428 => 'Precondition Required', // missing |
|
74
|
|
|
429 => 'Too Many Requests', // missing |
|
75
|
|
|
431 => 'Request Header Fields Too Large', // missing |
|
76
|
|
|
451 => 'Unavailable For Legal Reasons', // missing |
|
77
|
|
|
// Server Error 5xx |
|
78
|
|
|
500 => 'Internal Server Error', |
|
79
|
|
|
501 => 'Not Implemented', |
|
80
|
|
|
502 => 'Bad Gateway', |
|
81
|
|
|
503 => 'Service Unavailable', |
|
82
|
|
|
504 => 'Gateway Timeout', |
|
83
|
|
|
505 => 'HTTP Version Not Supported', |
|
84
|
|
|
506 => 'Variant Also Negotiates (Experimental)', // missing |
|
85
|
|
|
507 => 'Insufficient Storage', // missing |
|
86
|
|
|
508 => 'Loop Detected', // missing |
|
87
|
|
|
510 => 'Not Extended', // missing |
|
88
|
|
|
511 => 'Network Authentication Required' // missing |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
6 |
|
public function exists($code) { |
|
92
|
|
|
|
|
93
|
6 |
|
return array_key_exists($code, $this->codes); |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
5 |
|
public function getMessage($code) { |
|
98
|
|
|
|
|
99
|
5 |
|
if ($this->exists($code)) return $this->codes[$code]; |
|
100
|
|
|
|
|
101
|
|
|
throw new Exception("Invalid HTTP status code $code"); |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function isInformational($code) { |
|
106
|
|
|
|
|
107
|
|
|
return $code >= 100 && $code < 200; |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function isSuccessful($code) { |
|
112
|
|
|
|
|
113
|
|
|
return $code >= 200 && $code < 300; |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function isRedirection($code) { |
|
118
|
|
|
|
|
119
|
|
|
return $code >= 300 && $code < 400; |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function isClientError($code) { |
|
124
|
|
|
|
|
125
|
|
|
return $code >= 400 && $code < 500; |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function isServerError($code) { |
|
130
|
|
|
|
|
131
|
|
|
return $code >= 500 && $code < 600; |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.