|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Utility - Collection of various PHP utility functions. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Eric Sizemore <[email protected]> |
|
9
|
|
|
* @version 2.0.0 |
|
10
|
|
|
* @copyright (C) 2017 - 2024 Eric Sizemore |
|
11
|
|
|
* @license The MIT License (MIT) |
|
12
|
|
|
* |
|
13
|
|
|
* Copyright (C) 2017 - 2024 Eric Sizemore <https://www.secondversion.com>. |
|
14
|
|
|
* |
|
15
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
16
|
|
|
* of this software and associated documentation files (the "Software"), to |
|
17
|
|
|
* deal in the Software without restriction, including without limitation the |
|
18
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
19
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
|
20
|
|
|
* furnished to do so, subject to the following conditions: |
|
21
|
|
|
* |
|
22
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
23
|
|
|
* all copies or substantial portions of the Software. |
|
24
|
|
|
* |
|
25
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
26
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
28
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
29
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
30
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
31
|
|
|
* THE SOFTWARE. |
|
32
|
|
|
*/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Esi\Utility\Enums\Http is a fork of Crell/EnumTools (https://github.com/Crell/EnumTools) |
|
36
|
|
|
* which is also licensed under the MIT license. |
|
37
|
|
|
* Crell/EnumTools Copyright 2021 Larry Garfield<[email protected]> |
|
38
|
|
|
* |
|
39
|
|
|
* For changes made in Esi\Utility\Enums\Http compared to the original Crell/EnumTools, see CHANGELOG.md#2.0.0 |
|
40
|
|
|
*/ |
|
41
|
|
|
|
|
42
|
|
|
namespace Esi\Utility\Enums\Http; |
|
43
|
|
|
|
|
44
|
|
|
use Esi\Utility\Enums\Http\StatusCodeCategories; |
|
45
|
|
|
use Esi\Utility\Enums\Http\StatusCodeDescriptions; |
|
46
|
|
|
|
|
47
|
|
|
use function str_replace; |
|
48
|
|
|
use function floor; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Enum of HTTP status codes. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 2.0.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
enum StatusCodes: int |
|
56
|
|
|
{ |
|
57
|
|
|
// RFC7231, Section 6.2.1 |
|
58
|
|
|
case Continue = 100; |
|
59
|
|
|
// RFC7231, Section 6.2.2 |
|
60
|
|
|
case Switching_Protocols = 101; |
|
61
|
|
|
// RFC2518 |
|
62
|
|
|
case Processing = 102; |
|
63
|
|
|
// RFC8297 |
|
64
|
|
|
case Early_Hints = 103; |
|
65
|
|
|
// RFC7231, Section 6.3.1 |
|
66
|
|
|
case OK = 200; |
|
67
|
|
|
// RFC7231, Section 6.3.2 |
|
68
|
|
|
case Created = 201; |
|
69
|
|
|
// RFC7231, Section 6.3.3 |
|
70
|
|
|
case Accepted = 202; |
|
71
|
|
|
// RFC7231, Section 6.3.4 |
|
72
|
|
|
case Non_Authoritative_Information = 203; |
|
73
|
|
|
// RFC7231, Section 6.3.5 |
|
74
|
|
|
case No_Content = 204; |
|
75
|
|
|
// RFC7231, Section 6.3.6 |
|
76
|
|
|
case Reset_Content = 205; |
|
77
|
|
|
// RFC7233, Section 4.1 |
|
78
|
|
|
case Partial_Content = 206; |
|
79
|
|
|
// RFC4918 |
|
80
|
|
|
case Multi_Status = 207; |
|
81
|
|
|
// RFC5842 |
|
82
|
|
|
case Already_Reported = 208; |
|
83
|
|
|
// RFC3229 |
|
84
|
|
|
case IM_Used = 226; |
|
85
|
|
|
// RFC7231, Section 6.4.1 |
|
86
|
|
|
case Multiple_Choices = 300; |
|
87
|
|
|
// RFC7231, Section 6.4.2 |
|
88
|
|
|
case Moved_Permanently = 301; |
|
89
|
|
|
// RFC7231, Section 6.4.3 |
|
90
|
|
|
case Found = 302; |
|
91
|
|
|
// RFC7231, Section 6.4.4 |
|
92
|
|
|
case See_Other = 303; |
|
93
|
|
|
// RFC7232, Section 4.1 |
|
94
|
|
|
case Not_Modified = 304; |
|
95
|
|
|
// RFC7231, Section 6.4.5 |
|
96
|
|
|
case Use_Proxy = 305; |
|
97
|
|
|
// RFC7231, Section 6.4.6 |
|
98
|
|
|
case Explicitly_Unused = 306; |
|
99
|
|
|
// RFC7231, Section 6.4.7 |
|
100
|
|
|
case Temporary_Redirect = 307; |
|
101
|
|
|
// RFC7538 |
|
102
|
|
|
case Permanent_Redirect = 308; |
|
103
|
|
|
// RFC7231, Section 6.5.1 |
|
104
|
|
|
case Bad_Request = 400; |
|
105
|
|
|
// RFC7235, Section 3.1 |
|
106
|
|
|
case Unauthorized = 401; |
|
107
|
|
|
// RFC7231, Section 6.5.2 |
|
108
|
|
|
case Payment_Required = 402; |
|
109
|
|
|
// RFC7231, Section 6.5.3 |
|
110
|
|
|
case Forbidden = 403; |
|
111
|
|
|
// RFC7231, Section 6.5.4 |
|
112
|
|
|
case Not_Found = 404; |
|
113
|
|
|
// RFC7231, Section 6.5.5 |
|
114
|
|
|
case Method_Not_Allowed = 405; |
|
115
|
|
|
// RFC7231, Section 6.5.6 |
|
116
|
|
|
case Not_Acceptable = 406; |
|
117
|
|
|
// RFC7235, Section 3.2 |
|
118
|
|
|
case Proxy_Authentication_Required = 407; |
|
119
|
|
|
// RFC7231, Section 6.5.7 |
|
120
|
|
|
case Request_Timeout = 408; |
|
121
|
|
|
// RFC7231, Section 6.5.8 |
|
122
|
|
|
case Conflict = 409; |
|
123
|
|
|
// RFC7231, Section 6.5.9 |
|
124
|
|
|
case Gone = 410; |
|
125
|
|
|
// RFC7231, Section 6.5.10 |
|
126
|
|
|
case Length_Required = 411; |
|
127
|
|
|
// RFC7232, Section 4.2RFC8144, Section 3.2 |
|
128
|
|
|
case Precondition_Failed = 412; |
|
129
|
|
|
// RFC7231, Section 6.5.11 |
|
130
|
|
|
case Payload_Too_Large = 413; |
|
131
|
|
|
// RFC7231, Section 6.5.12 |
|
132
|
|
|
case Request_URI_Too_Long = 414; |
|
133
|
|
|
// RFC7231, Section 6.5.13RFC7694, Section 3 |
|
134
|
|
|
case Unsupported_Media_Type = 415; |
|
135
|
|
|
// RFC7233, Section 4.4 |
|
136
|
|
|
case Requested_Range_Not_Satisfiable = 416; |
|
137
|
|
|
// RFC7231, Section 6.5.14 |
|
138
|
|
|
case Expectation_Failed = 417; |
|
139
|
|
|
// RFC2324 |
|
140
|
|
|
case Im_A_Teapot = 418; |
|
141
|
|
|
// RFC7540, Section 9.1.2 |
|
142
|
|
|
case Misdirected_Request = 421; |
|
143
|
|
|
// RFC4918 |
|
144
|
|
|
case Unprocessable_Entity = 422; |
|
145
|
|
|
// RFC4918 |
|
146
|
|
|
case Locked = 423; |
|
147
|
|
|
// RFC4918 |
|
148
|
|
|
case Failed_Dependency = 424; |
|
149
|
|
|
// RFC8470 |
|
150
|
|
|
case Too_Early = 425; |
|
151
|
|
|
// RFC7231, Section 6.5.15 |
|
152
|
|
|
case Upgrade_Required = 426; |
|
153
|
|
|
// RFC6585 |
|
154
|
|
|
case Precondition_Required = 428; |
|
155
|
|
|
// RFC6585 |
|
156
|
|
|
case Too_Many_Requests = 429; |
|
157
|
|
|
// RFC6585 |
|
158
|
|
|
case Request_Header_Fields_Too_Large = 431; |
|
159
|
|
|
// RFC7725 |
|
160
|
|
|
case Unavailable_For_Legal_Reasons = 451; |
|
161
|
|
|
// RFC7231, Section 6.6.1 |
|
162
|
|
|
case Internal_Server_Error = 500; |
|
163
|
|
|
// RFC7231, Section 6.6.2 |
|
164
|
|
|
case Not_Implemented = 501; |
|
165
|
|
|
// RFC7231, Section 6.6.3 |
|
166
|
|
|
case Bad_Gateway = 502; |
|
167
|
|
|
// RFC7231, Section 6.6.4 |
|
168
|
|
|
case Service_Unavailable = 503; |
|
169
|
|
|
// RFC7231, Section 6.6.5 |
|
170
|
|
|
case Gateway_Timeout = 504; |
|
171
|
|
|
// RFC7231, Section 6.6.6 |
|
172
|
|
|
case HTTP_Version_Not_Supported = 505; |
|
173
|
|
|
// RFC2295 |
|
174
|
|
|
case Variant_Also_Negotiates = 506; |
|
175
|
|
|
// RFC4918 |
|
176
|
|
|
case Insufficient_Storage = 507; |
|
177
|
|
|
// RFC5842 |
|
178
|
|
|
case Loop_Detected = 508; |
|
179
|
|
|
// RFC2774 |
|
180
|
|
|
case Not_Extended = 510; |
|
181
|
|
|
// RFC6585 |
|
182
|
|
|
case Network_Authentication_Required = 511; |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns the value of a given case. |
|
186
|
|
|
* eg: StatusCodes::Not_Extended->getValue() // 510 |
|
187
|
|
|
* |
|
188
|
|
|
* @return int Status Code |
|
189
|
|
|
*/ |
|
190
|
2 |
|
public function getValue(): int |
|
191
|
|
|
{ |
|
192
|
2 |
|
return $this->value; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Returns the name of a given case. |
|
197
|
|
|
* eg: StatusCodes::Not_Extended->getName() // Not_Extended |
|
198
|
|
|
* |
|
199
|
|
|
* @return string Case name |
|
200
|
|
|
*/ |
|
201
|
2 |
|
public function getName(): string |
|
202
|
|
|
{ |
|
203
|
2 |
|
return $this->name; |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Returns the human-friendly message version of the status code. |
|
208
|
|
|
* |
|
209
|
|
|
* These message strings are defined by the RFC that defined the code. In most |
|
210
|
|
|
* cases it is the same as the enum value name, give or take a little formatting. |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
1 |
|
public function getMessage(): string |
|
215
|
|
|
{ |
|
216
|
|
|
// A few need special handling because of non-alphabetic characters. |
|
217
|
|
|
// The rest are a trivial string transformation. |
|
218
|
1 |
|
return match ($this) { |
|
219
|
1 |
|
self::Non_Authoritative_Information => 'Non-authoritative Information', |
|
220
|
1 |
|
self::Multi_Status => 'Multi-Status', |
|
221
|
1 |
|
self::Im_A_Teapot => "I'm A Teapot", |
|
222
|
1 |
|
self::Request_URI_Too_Long => 'Request-URI Too Long', |
|
223
|
1 |
|
default => str_replace('_', ' ', $this->name), |
|
|
|
|
|
|
224
|
1 |
|
}; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Determines what category of message this status code is. |
|
229
|
|
|
* |
|
230
|
|
|
* @return StatusCodeCategories |
|
231
|
|
|
*/ |
|
232
|
1 |
|
public function getCategory(): StatusCodeCategories |
|
233
|
|
|
{ |
|
234
|
1 |
|
return match(floor($this->value / 100)) { |
|
235
|
1 |
|
1.0 => StatusCodeCategories::Informational, |
|
236
|
1 |
|
2.0 => StatusCodeCategories::Success, |
|
237
|
1 |
|
3.0 => StatusCodeCategories::Redirection, |
|
238
|
1 |
|
4.0 => StatusCodeCategories::ClientError, |
|
239
|
1 |
|
5.0 => StatusCodeCategories::ServerError, |
|
240
|
1 |
|
default => StatusCodeCategories::Unknown, |
|
241
|
1 |
|
}; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Returns the description of this status code. |
|
246
|
|
|
* |
|
247
|
|
|
* @return StatusCodeDescriptions |
|
248
|
|
|
*/ |
|
249
|
1 |
|
public function getDescription(): StatusCodeDescriptions |
|
250
|
|
|
{ |
|
251
|
1 |
|
return match($this) { |
|
252
|
1 |
|
self::Continue => StatusCodeDescriptions::Continue, |
|
253
|
1 |
|
self::Switching_Protocols => StatusCodeDescriptions::Switching_Protocols, |
|
254
|
1 |
|
self::Processing => StatusCodeDescriptions::Processing, |
|
255
|
1 |
|
self::Early_Hints => StatusCodeDescriptions::Early_Hints, |
|
256
|
1 |
|
self::OK => StatusCodeDescriptions::OK, |
|
257
|
1 |
|
self::Created => StatusCodeDescriptions::Created, |
|
258
|
1 |
|
self::Accepted => StatusCodeDescriptions::Accepted, |
|
259
|
1 |
|
self::Non_Authoritative_Information => StatusCodeDescriptions::Non_Authoritative_Information, |
|
260
|
1 |
|
self::No_Content => StatusCodeDescriptions::No_Content, |
|
261
|
1 |
|
self::Reset_Content => StatusCodeDescriptions::Reset_Content, |
|
262
|
1 |
|
self::Partial_Content => StatusCodeDescriptions::Partial_Content, |
|
263
|
1 |
|
self::Multi_Status => StatusCodeDescriptions::Multi_Status, |
|
264
|
1 |
|
self::Already_Reported => StatusCodeDescriptions::Already_Reported, |
|
265
|
1 |
|
self::IM_Used => StatusCodeDescriptions::IM_Used, |
|
266
|
1 |
|
self::Multiple_Choices => StatusCodeDescriptions::Multiple_Choices, |
|
267
|
1 |
|
self::Moved_Permanently => StatusCodeDescriptions::Moved_Permanently, |
|
268
|
1 |
|
self::Found => StatusCodeDescriptions::Found, |
|
269
|
1 |
|
self::See_Other => StatusCodeDescriptions::See_Other, |
|
270
|
1 |
|
self::Not_Modified => StatusCodeDescriptions::Not_Modified, |
|
271
|
1 |
|
self::Use_Proxy => StatusCodeDescriptions::Use_Proxy, |
|
272
|
1 |
|
self::Explicitly_Unused => StatusCodeDescriptions::Explicitly_Unused, |
|
273
|
1 |
|
self::Temporary_Redirect => StatusCodeDescriptions::Temporary_Redirect, |
|
274
|
1 |
|
self::Permanent_Redirect => StatusCodeDescriptions::Permanent_Redirect, |
|
275
|
1 |
|
self::Bad_Request => StatusCodeDescriptions::Bad_Request, |
|
276
|
1 |
|
self::Unauthorized => StatusCodeDescriptions::Unauthorized, |
|
277
|
1 |
|
self::Payment_Required => StatusCodeDescriptions::Payment_Required, |
|
278
|
1 |
|
self::Forbidden => StatusCodeDescriptions::Forbidden, |
|
279
|
1 |
|
self::Not_Found => StatusCodeDescriptions::Not_Found, |
|
280
|
1 |
|
self::Method_Not_Allowed => StatusCodeDescriptions::Method_Not_Allowed, |
|
281
|
1 |
|
self::Not_Acceptable => StatusCodeDescriptions::Not_Acceptable, |
|
282
|
1 |
|
self::Proxy_Authentication_Required => StatusCodeDescriptions::Proxy_Authentication_Required, |
|
283
|
1 |
|
self::Request_Timeout => StatusCodeDescriptions::Request_Timeout, |
|
284
|
1 |
|
self::Conflict => StatusCodeDescriptions::Conflict, |
|
285
|
1 |
|
self::Gone => StatusCodeDescriptions::Gone, |
|
286
|
1 |
|
self::Length_Required => StatusCodeDescriptions::Length_Required, |
|
287
|
1 |
|
self::Precondition_Failed => StatusCodeDescriptions::Precondition_Failed, |
|
288
|
1 |
|
self::Payload_Too_Large => StatusCodeDescriptions::Payload_Too_Large, |
|
289
|
1 |
|
self::Request_URI_Too_Long => StatusCodeDescriptions::Request_URI_Too_Long, |
|
290
|
1 |
|
self::Unsupported_Media_Type => StatusCodeDescriptions::Unsupported_Media_Type, |
|
291
|
1 |
|
self::Requested_Range_Not_Satisfiable => StatusCodeDescriptions::Requested_Range_Not_Satisfiable, |
|
292
|
1 |
|
self::Expectation_Failed => StatusCodeDescriptions::Expectation_Failed, |
|
293
|
1 |
|
self::Im_A_Teapot => StatusCodeDescriptions::Im_A_Teapot, |
|
294
|
1 |
|
self::Misdirected_Request => StatusCodeDescriptions::Misdirected_Request, |
|
295
|
1 |
|
self::Unprocessable_Entity => StatusCodeDescriptions::Unprocessable_Entity, |
|
296
|
1 |
|
self::Locked => StatusCodeDescriptions::Locked, |
|
297
|
1 |
|
self::Failed_Dependency => StatusCodeDescriptions::Failed_Dependency, |
|
298
|
1 |
|
self::Too_Early => StatusCodeDescriptions::Too_Early, |
|
299
|
1 |
|
self::Upgrade_Required => StatusCodeDescriptions::Upgrade_Required, |
|
300
|
1 |
|
self::Precondition_Required => StatusCodeDescriptions::Precondition_Required, |
|
301
|
1 |
|
self::Too_Many_Requests => StatusCodeDescriptions::Too_Many_Requests, |
|
302
|
1 |
|
self::Request_Header_Fields_Too_Large => StatusCodeDescriptions::Request_Header_Fields_Too_Large, |
|
303
|
1 |
|
self::Unavailable_For_Legal_Reasons => StatusCodeDescriptions::Unavailable_For_Legal_Reasons, |
|
304
|
1 |
|
self::Internal_Server_Error => StatusCodeDescriptions::Internal_Server_Error, |
|
305
|
1 |
|
self::Not_Implemented => StatusCodeDescriptions::Not_Implemented, |
|
306
|
1 |
|
self::Bad_Gateway => StatusCodeDescriptions::Bad_Gateway, |
|
307
|
1 |
|
self::Service_Unavailable => StatusCodeDescriptions::Service_Unavailable, |
|
308
|
1 |
|
self::Gateway_Timeout => StatusCodeDescriptions::Gateway_Timeout, |
|
309
|
1 |
|
self::HTTP_Version_Not_Supported => StatusCodeDescriptions::HTTP_Version_Not_Supported, |
|
310
|
1 |
|
self::Variant_Also_Negotiates => StatusCodeDescriptions::Variant_Also_Negotiates, |
|
311
|
1 |
|
self::Insufficient_Storage => StatusCodeDescriptions::Insufficient_Storage, |
|
312
|
1 |
|
self::Loop_Detected => StatusCodeDescriptions::Loop_Detected, |
|
313
|
1 |
|
self::Not_Extended => StatusCodeDescriptions::Not_Extended, |
|
314
|
1 |
|
self::Network_Authentication_Required => StatusCodeDescriptions::Network_Authentication_Required |
|
315
|
1 |
|
}; |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|