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
|
|
|
|
46
|
|
|
use function str_replace; |
47
|
|
|
use function floor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Enum of HTTP status codes. |
51
|
|
|
* |
52
|
|
|
* @since 2.0.0 |
53
|
|
|
*/ |
54
|
|
|
enum StatusCodes: int |
55
|
|
|
{ |
56
|
|
|
// RFC7231, Section 6.2.1 |
57
|
|
|
case Continue = 100; |
58
|
|
|
// RFC7231, Section 6.2.2 |
59
|
|
|
case Switching_Protocols = 101; |
60
|
|
|
// RFC2518 |
61
|
|
|
case Processing = 102; |
62
|
|
|
// RFC8297 |
63
|
|
|
case Early_Hints = 103; |
64
|
|
|
// RFC7231, Section 6.3.1 |
65
|
|
|
case OK = 200; |
66
|
|
|
// RFC7231, Section 6.3.2 |
67
|
|
|
case Created = 201; |
68
|
|
|
// RFC7231, Section 6.3.3 |
69
|
|
|
case Accepted = 202; |
70
|
|
|
// RFC7231, Section 6.3.4 |
71
|
|
|
case Non_Authoritative_Information = 203; |
72
|
|
|
// RFC7231, Section 6.3.5 |
73
|
|
|
case No_Content = 204; |
74
|
|
|
// RFC7231, Section 6.3.6 |
75
|
|
|
case Reset_Content = 205; |
76
|
|
|
// RFC7233, Section 4.1 |
77
|
|
|
case Partial_Content = 206; |
78
|
|
|
// RFC4918 |
79
|
|
|
case Multi_Status = 207; |
80
|
|
|
// RFC5842 |
81
|
|
|
case Already_Reported = 208; |
82
|
|
|
// RFC3229 |
83
|
|
|
case IM_Used = 226; |
84
|
|
|
// RFC7231, Section 6.4.1 |
85
|
|
|
case Multiple_Choices = 300; |
86
|
|
|
// RFC7231, Section 6.4.2 |
87
|
|
|
case Moved_Permanently = 301; |
88
|
|
|
// RFC7231, Section 6.4.3 |
89
|
|
|
case Found = 302; |
90
|
|
|
// RFC7231, Section 6.4.4 |
91
|
|
|
case See_Other = 303; |
92
|
|
|
// RFC7232, Section 4.1 |
93
|
|
|
case Not_Modified = 304; |
94
|
|
|
// RFC7231, Section 6.4.5 |
95
|
|
|
case Use_Proxy = 305; |
96
|
|
|
// RFC7231, Section 6.4.6 |
97
|
|
|
case Explicitly_Unused = 306; |
98
|
|
|
// RFC7231, Section 6.4.7 |
99
|
|
|
case Temporary_Redirect = 307; |
100
|
|
|
// RFC7538 |
101
|
|
|
case Permanent_Redirect = 308; |
102
|
|
|
// RFC7231, Section 6.5.1 |
103
|
|
|
case Bad_Request = 400; |
104
|
|
|
// RFC7235, Section 3.1 |
105
|
|
|
case Unauthorized = 401; |
106
|
|
|
// RFC7231, Section 6.5.2 |
107
|
|
|
case Payment_Required = 402; |
108
|
|
|
// RFC7231, Section 6.5.3 |
109
|
|
|
case Forbidden = 403; |
110
|
|
|
// RFC7231, Section 6.5.4 |
111
|
|
|
case Not_Found = 404; |
112
|
|
|
// RFC7231, Section 6.5.5 |
113
|
|
|
case Method_Not_Allowed = 405; |
114
|
|
|
// RFC7231, Section 6.5.6 |
115
|
|
|
case Not_Acceptable = 406; |
116
|
|
|
// RFC7235, Section 3.2 |
117
|
|
|
case Proxy_Authentication_Required = 407; |
118
|
|
|
// RFC7231, Section 6.5.7 |
119
|
|
|
case Request_Timeout = 408; |
120
|
|
|
// RFC7231, Section 6.5.8 |
121
|
|
|
case Conflict = 409; |
122
|
|
|
// RFC7231, Section 6.5.9 |
123
|
|
|
case Gone = 410; |
124
|
|
|
// RFC7231, Section 6.5.10 |
125
|
|
|
case Length_Required = 411; |
126
|
|
|
// RFC7232, Section 4.2RFC8144, Section 3.2 |
127
|
|
|
case Precondition_Failed = 412; |
128
|
|
|
// RFC7231, Section 6.5.11 |
129
|
|
|
case Payload_Too_Large = 413; |
130
|
|
|
// RFC7231, Section 6.5.12 |
131
|
|
|
case Request_URI_Too_Long = 414; |
132
|
|
|
// RFC7231, Section 6.5.13RFC7694, Section 3 |
133
|
|
|
case Unsupported_Media_Type = 415; |
134
|
|
|
// RFC7233, Section 4.4 |
135
|
|
|
case Requested_Range_Not_Satisfiable = 416; |
136
|
|
|
// RFC7231, Section 6.5.14 |
137
|
|
|
case Expectation_Failed = 417; |
138
|
|
|
// RFC2324 |
139
|
|
|
case Im_A_Teapot = 418; |
140
|
|
|
// RFC7540, Section 9.1.2 |
141
|
|
|
case Misdirected_Request = 421; |
142
|
|
|
// RFC4918 |
143
|
|
|
case Unprocessable_Entity = 422; |
144
|
|
|
// RFC4918 |
145
|
|
|
case Locked = 423; |
146
|
|
|
// RFC4918 |
147
|
|
|
case Failed_Dependency = 424; |
148
|
|
|
// RFC8470 |
149
|
|
|
case Too_Early = 425; |
150
|
|
|
// RFC7231, Section 6.5.15 |
151
|
|
|
case Upgrade_Required = 426; |
152
|
|
|
// RFC6585 |
153
|
|
|
case Precondition_Required = 428; |
154
|
|
|
// RFC6585 |
155
|
|
|
case Too_Many_Requests = 429; |
156
|
|
|
// RFC6585 |
157
|
|
|
case Request_Header_Fields_Too_Large = 431; |
158
|
|
|
// RFC7725 |
159
|
|
|
case Unavailable_For_Legal_Reasons = 451; |
160
|
|
|
// RFC7231, Section 6.6.1 |
161
|
|
|
case Internal_Server_Error = 500; |
162
|
|
|
// RFC7231, Section 6.6.2 |
163
|
|
|
case Not_Implemented = 501; |
164
|
|
|
// RFC7231, Section 6.6.3 |
165
|
|
|
case Bad_Gateway = 502; |
166
|
|
|
// RFC7231, Section 6.6.4 |
167
|
|
|
case Service_Unavailable = 503; |
168
|
|
|
// RFC7231, Section 6.6.5 |
169
|
|
|
case Gateway_Timeout = 504; |
170
|
|
|
// RFC7231, Section 6.6.6 |
171
|
|
|
case HTTP_Version_Not_Supported = 505; |
172
|
|
|
// RFC2295 |
173
|
|
|
case Variant_Also_Negotiates = 506; |
174
|
|
|
// RFC4918 |
175
|
|
|
case Insufficient_Storage = 507; |
176
|
|
|
// RFC5842 |
177
|
|
|
case Loop_Detected = 508; |
178
|
|
|
// RFC2774 |
179
|
|
|
case Not_Extended = 510; |
180
|
|
|
// RFC6585 |
181
|
|
|
case Network_Authentication_Required = 511; |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Returns the value of a given case. |
185
|
|
|
* eg: StatusCodes::Not_Extended->getValue() // 510 |
186
|
|
|
* |
187
|
|
|
* @return int Status Code |
188
|
|
|
*/ |
189
|
2 |
|
public function getValue(): int |
190
|
|
|
{ |
191
|
2 |
|
return $this->value; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Returns the name of a given case. |
196
|
|
|
* eg: StatusCodes::Not_Extended->getName() // Not_Extended |
197
|
|
|
* |
198
|
|
|
* @return string Case name |
199
|
|
|
*/ |
200
|
1 |
|
public function getName(): string |
201
|
|
|
{ |
202
|
1 |
|
return $this->name; |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the human-friendly message version of the status code. |
207
|
|
|
* |
208
|
|
|
* These message strings are defined by the RFC that defined the code. In most |
209
|
|
|
* cases it is the same as the enum value name, give or take a little formatting. |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
1 |
|
public function getMessage(): string |
214
|
|
|
{ |
215
|
|
|
// A few need special handling because of non-alphabetic characters. |
216
|
|
|
// The rest are a trivial string transformation. |
217
|
1 |
|
return match ($this) { |
218
|
1 |
|
self::Non_Authoritative_Information => 'Non-authoritative Information', |
219
|
1 |
|
self::Multi_Status => 'Multi-Status', |
220
|
1 |
|
self::Im_A_Teapot => "I'm A Teapot", |
221
|
1 |
|
self::Request_URI_Too_Long => 'Request-URI Too Long', |
222
|
1 |
|
default => str_replace('_', ' ', $this->name), |
|
|
|
|
223
|
1 |
|
}; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Determines what category of message this status code is. |
228
|
|
|
* |
229
|
|
|
* @return StatusCodeCategories |
230
|
|
|
*/ |
231
|
1 |
|
public function getCategory(): StatusCodeCategories |
232
|
|
|
{ |
233
|
1 |
|
return match(floor($this->value / 100)) { |
234
|
1 |
|
1.0 => StatusCodeCategories::Informational, |
235
|
1 |
|
2.0 => StatusCodeCategories::Success, |
236
|
1 |
|
3.0 => StatusCodeCategories::Redirection, |
237
|
1 |
|
4.0 => StatusCodeCategories::ClientError, |
238
|
1 |
|
5.0 => StatusCodeCategories::ServerError, |
239
|
1 |
|
default => StatusCodeCategories::Unknown, |
240
|
1 |
|
}; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|