1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine HTTP |
5
|
|
|
* |
6
|
|
|
* Platine HTTP Message is the implementation of PSR 7 |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine HTTP |
11
|
|
|
* Copyright (c) 2011 - 2017 rehyved.com |
12
|
|
|
* |
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
15
|
|
|
* in the Software without restriction, including without limitation the rights |
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
18
|
|
|
* furnished to do so, subject to the following conditions: |
19
|
|
|
* |
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
21
|
|
|
* copies or substantial portions of the Software. |
22
|
|
|
* |
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29
|
|
|
* SOFTWARE. |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @file HttpStatus.php |
34
|
|
|
* |
35
|
|
|
* The Http Status class |
36
|
|
|
* |
37
|
|
|
* An enumeration of possible HTTP status codes. |
38
|
|
|
* This class provides convenience methods to check the type |
39
|
|
|
* of status and retrieve the reason phrase for the status. |
40
|
|
|
* |
41
|
|
|
* @package Platine\Http\Client |
42
|
|
|
* @author Platine Developers team |
43
|
|
|
* @copyright Copyright (c) 2020 |
44
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
45
|
|
|
* @link https://www.platine-php.com |
46
|
|
|
* @version 1.0.0 |
47
|
|
|
* @filesource |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
declare(strict_types=1); |
51
|
|
|
|
52
|
|
|
namespace Platine\Http\Client; |
53
|
|
|
|
54
|
|
|
use InvalidArgumentException; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @class HttpStatus |
58
|
|
|
* @package Platine\Http\Client |
59
|
|
|
*/ |
60
|
|
|
class HttpStatus |
61
|
|
|
{ |
62
|
|
|
// Informational 1xx: |
63
|
|
|
public const INFORMATIONAL = 100; |
64
|
|
|
public const CONTINUE = 100; |
65
|
|
|
public const SWITCHING_PROTOCOLS = 101; |
66
|
|
|
|
67
|
|
|
// Successful 2xx: |
68
|
|
|
public const SUCCESSFUL = 200; |
69
|
|
|
public const OK = 200; |
70
|
|
|
public const CREATED = 201; |
71
|
|
|
public const ACCEPTED = 202; |
72
|
|
|
public const NON_AUTHORITATIVE_INFORMATION = 203; |
73
|
|
|
public const NO_CONTENT = 204; |
74
|
|
|
public const RESET_CONTENT = 205; |
75
|
|
|
public const PARTIAL_CONTENT = 206; |
76
|
|
|
|
77
|
|
|
// Redirection 3xx: |
78
|
|
|
public const REDIRECTION = 300; |
79
|
|
|
public const MULTIPLE_CHOICES = 300; |
80
|
|
|
public const MOVED_PERMANENTLY = 301; |
81
|
|
|
public const FOUND = 302; |
82
|
|
|
public const SEE_OTHER = 303; |
83
|
|
|
public const NOT_MODIFIED = 304; |
84
|
|
|
public const USE_PROXY = 305; |
85
|
|
|
// Code 306 was used in a previous HTTP specification but no longer used but kept reserved. |
86
|
|
|
public const TEMPORARY_REDIRECT = 307; |
87
|
|
|
|
88
|
|
|
// Client Error 4xx: |
89
|
|
|
public const CLIENT_ERROR = 400; |
90
|
|
|
public const BAD_REQUEST = 400; |
91
|
|
|
public const UNAUTHORIZED = 401; |
92
|
|
|
public const PAYMENT_REQUIRED = 402; |
93
|
|
|
public const FORBIDDEN = 403; |
94
|
|
|
public const NOT_FOUND = 404; |
95
|
|
|
public const METHOD_NOT_ALLOWED = 405; |
96
|
|
|
public const NOT_ACCEPTABLE = 406; |
97
|
|
|
public const PROXY_AUTHENTICATION_REQUIRED = 407; |
98
|
|
|
public const REQUEST_TIMEOUT = 408; |
99
|
|
|
public const CONFLICT = 409; |
100
|
|
|
public const GONE = 410; |
101
|
|
|
public const LENGTH_REQUIRED = 411; |
102
|
|
|
public const PRECONDITION_FAILED = 412; |
103
|
|
|
public const REQUEST_ENTITY_TOO_LARGE = 413; |
104
|
|
|
public const REQUEST_URI_TOO_LONG = 414; |
105
|
|
|
public const UNSUPPORTED_MEDIA_TYPE = 415; |
106
|
|
|
public const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
107
|
|
|
public const EXPECTATION_FAILED = 417; |
108
|
|
|
|
109
|
|
|
// Server Error 5xx: |
110
|
|
|
public const SERVER_ERROR = 500; |
111
|
|
|
public const INTERNAL_SERVER_ERROR = 500; |
112
|
|
|
public const NOT_IMPLEMENTED = 501; |
113
|
|
|
public const BAD_GATEWAY = 502; |
114
|
|
|
public const SERVICE_UNAVAILABLE = 503; |
115
|
|
|
public const GATEWAY_TIMEOUT = 504; |
116
|
|
|
public const HTTP_VERSION_NOT_SUPPORTED = 505; |
117
|
|
|
public const SERVER_ERROR_END = 600; |
118
|
|
|
|
119
|
|
|
public const REASON_PHRASES = [ |
120
|
|
|
self::CONTINUE => 'Continue', |
121
|
|
|
self::SWITCHING_PROTOCOLS => 'Switching Protocols', |
122
|
|
|
|
123
|
|
|
self::OK => 'OK', |
124
|
|
|
self::CREATED => 'Created', |
125
|
|
|
self::ACCEPTED => 'Accepted', |
126
|
|
|
self::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
127
|
|
|
self::NO_CONTENT => 'No Content', |
128
|
|
|
self::RESET_CONTENT => 'Reset Content', |
129
|
|
|
self::PARTIAL_CONTENT => 'Partial Content', |
130
|
|
|
|
131
|
|
|
self::MULTIPLE_CHOICES => 'Multiple Choices', |
132
|
|
|
self::MOVED_PERMANENTLY => 'Moved Permanently', |
133
|
|
|
self::FOUND => 'Found', |
134
|
|
|
self::SEE_OTHER => 'See Other', |
135
|
|
|
self::NOT_MODIFIED => 'Not Modified', |
136
|
|
|
self::USE_PROXY => 'Use Proxy', |
137
|
|
|
self::TEMPORARY_REDIRECT => 'Temporary Redirect', |
138
|
|
|
|
139
|
|
|
self::BAD_REQUEST => 'Bad Request', |
140
|
|
|
self::UNAUTHORIZED => 'Unauthorized', |
141
|
|
|
self::PAYMENT_REQUIRED => 'Payment Required', |
142
|
|
|
self::FORBIDDEN => 'Forbidden', |
143
|
|
|
self::NOT_FOUND => 'Not Found', |
144
|
|
|
self::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
145
|
|
|
self::NOT_ACCEPTABLE => 'Not Acceptable', |
146
|
|
|
self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
147
|
|
|
self::REQUEST_TIMEOUT => 'Request Time-out', |
148
|
|
|
self::CONFLICT => 'Conflict', |
149
|
|
|
self::GONE => 'Gone', |
150
|
|
|
self::LENGTH_REQUIRED => 'Length Required', |
151
|
|
|
self::PRECONDITION_FAILED => 'Precondition Failed', |
152
|
|
|
self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
153
|
|
|
self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
154
|
|
|
self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
155
|
|
|
self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
156
|
|
|
self::EXPECTATION_FAILED => 'Expectation Failed', |
157
|
|
|
|
158
|
|
|
self::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
159
|
|
|
self::NOT_IMPLEMENTED => 'Not Implemented', |
160
|
|
|
self::BAD_GATEWAY => 'Bad Gateway', |
161
|
|
|
self::SERVICE_UNAVAILABLE => 'Service Unavailable', |
162
|
|
|
self::GATEWAY_TIMEOUT => 'Gateway Timeout', |
163
|
|
|
self::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported' |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Checks if the provided status code falls in the Informational range of HTTP statuses |
168
|
|
|
* @param int $statusCode the status code to check |
169
|
|
|
* @return bool TRUE if it falls into the Informational range, FALSE if not |
170
|
|
|
*/ |
171
|
|
|
public static function isInformational(int $statusCode): bool |
172
|
|
|
{ |
173
|
|
|
return ($statusCode >= self::INFORMATIONAL) && ($statusCode < self::SUCCESSFUL); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Checks if the provided status code falls in the Successful range of HTTP statuses |
178
|
|
|
* @param int $statusCode the status code to check |
179
|
|
|
* @return bool TRUE if it falls into the Successful range, FALSE if not |
180
|
|
|
*/ |
181
|
|
|
public static function isSuccessful(int $statusCode): bool |
182
|
|
|
{ |
183
|
|
|
return ($statusCode >= self::SUCCESSFUL) && ($statusCode < self::REDIRECTION); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Checks if the provided status code falls in the Redirection range of HTTP statuses |
188
|
|
|
* @param int $statusCode the status code to check |
189
|
|
|
* @return bool TRUE if it falls into the Redirection range, FALSE if not |
190
|
|
|
*/ |
191
|
|
|
public static function isRedirection(int $statusCode): bool |
192
|
|
|
{ |
193
|
|
|
return ($statusCode >= self::REDIRECTION) && ($statusCode < self::CLIENT_ERROR); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Checks if the provided status code falls in the Client error range of HTTP statuses |
198
|
|
|
* @param int $statusCode the status code to check |
199
|
|
|
* @return bool TRUE if it falls into the Client error range, FALSE if not |
200
|
|
|
*/ |
201
|
|
|
public static function isClientError(int $statusCode): bool |
202
|
|
|
{ |
203
|
|
|
return ($statusCode >= self::CLIENT_ERROR) && ($statusCode < self::SERVER_ERROR); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Checks if the provided status code falls in the Server error range of HTTP statuses |
208
|
|
|
* @param int $statusCode the status code to check |
209
|
|
|
* @return bool TRUE if it falls into the Server error range, FALSE if not |
210
|
|
|
*/ |
211
|
|
|
public static function isServerError(int $statusCode): bool |
212
|
|
|
{ |
213
|
|
|
return ($statusCode >= self::SERVER_ERROR) && ($statusCode < self::SERVER_ERROR_END); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Checks if the provided status code falls in the Client or Server error range of HTTP statuses |
218
|
|
|
* @param int $statusCode the status code to check |
219
|
|
|
* @return bool TRUE if it falls into the Client or Server error range, FALSE if not |
220
|
|
|
*/ |
221
|
|
|
public static function isError(int $statusCode): bool |
222
|
|
|
{ |
223
|
|
|
return ($statusCode >= self::CLIENT_ERROR) && ($statusCode < self::SERVER_ERROR_END); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Retrieve the reason phrase for the provided HTTP status code |
228
|
|
|
* @param int $statusCode the status code for which to retrieve the reason phrase |
229
|
|
|
* @return string the reason phrase |
230
|
|
|
* @throws InvalidArgumentException if the provided value is not a valid HTTP status code |
231
|
|
|
*/ |
232
|
|
|
public static function getReasonPhrase(int $statusCode): string |
233
|
|
|
{ |
234
|
|
|
if (array_key_exists($statusCode, self::REASON_PHRASES)) { |
235
|
|
|
return self::REASON_PHRASES[$statusCode]; |
236
|
|
|
} |
237
|
|
|
throw new InvalidArgumentException('Invalid status code'); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|