-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatus.php
527 lines (479 loc) · 13.4 KB
/
Status.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
namespace ICanBoogie\HTTP;
use ICanBoogie\Accessor\AccessorTrait;
use InvalidArgumentException;
use function in_array;
use function is_array;
use function is_numeric;
use function preg_match;
/**
* Representation of a response status.
*
* @property int $code HTTP Status code.
* @property string $message Status message.
*
* @property-read bool $is_cacheable Whether the status is cacheable.
* @property-read bool $is_client_error Whether the status is a client error.
* @property-read bool $is_empty Whether the status is empty.
* @property-read bool $is_forbidden Whether the status is forbidden.
* @property-read bool $is_informational Whether the status is informational.
* @property-read bool $is_not_found Whether the status is not found.
* @property-read bool $is_ok Whether the status is ok.
* @property-read bool $is_redirect Whether the status is a redirection.
* @property-read bool $is_server_error Whether the status is a server error.
* @property-read bool $is_successful Whether the status is successful.
* @property-read bool $is_valid Whether the status is valid.
*/
final class Status
{
use AccessorTrait;
/**
* @deprecated
* @see ResponseStatus::STATUS_CONTINUE
*/
public const CONTINUE_ = 100;
/**
* @deprecated
* @see ResponseStatus::STATUS_SWITCHING_PROTOCOLS
*/
public const SWITCHING_PROTOCOLS = 101;
/**
* @deprecated
* @see ResponseStatus::STATUS_OK
*/
public const OK = 200;
/**
* @deprecated
* @see ResponseStatus::STATUS_CREATED
*/
public const CREATED = 201;
/**
* @deprecated
* @see ResponseStatus::STATUS_ACCEPTED
*/
public const ACCEPTED = 202;
/**
* @deprecated
* @see ResponseStatus::STATUS_NON_AUTHORITATIVE_INFORMATION
*/
public const NON_AUTHORITATIVE_INFORMATION = 203;
/**
* @deprecated
* @see ResponseStatus::STATUS_NO_CONTENT
*/
public const NO_CONTENT = 204;
/**
* @deprecated
* @see ResponseStatus::STATUS_RESET_CONTENT
*/
public const RESET_CONTENT = 205;
/**
* @deprecated
* @see ResponseStatus::STATUS_PARTIAL_CONTENT
*/
public const PARTIAL_CONTENT = 206;
/**
* @deprecated
* @see ResponseStatus::STATUS_MULTIPLE_CHOICES
*/
public const MULTIPLE_CHOICES = 300;
/**
* @deprecated
* @see ResponseStatus::STATUS_MOVED_PERMANENTLY
*/
public const MOVED_PERMANENTLY = 301;
/**
* @deprecated
* @see ResponseStatus::STATUS_FOUND
*/
public const FOUND = 302;
/**
* @deprecated
* @see ResponseStatus::STATUS_SEE_OTHER
*/
public const SEE_OTHER = 303;
/**
* @deprecated
* @see ResponseStatus::STATUS_NOT_MODIFIED
*/
public const NOT_MODIFIED = 304;
/**
* @deprecated
* @see ResponseStatus::STATUS_USE_PROXY
*/
public const USE_PROXY = 305;
/**
* @deprecated
* @see ResponseStatus::STATUS_TEMPORARY_REDIRECT
*/
public const TEMPORARY_REDIRECT = 307;
/**
* @deprecated
* @see ResponseStatus::STATUS_BAD_REQUEST
*/
public const BAD_REQUEST = 400;
/**
* @deprecated
* @see ResponseStatus::STATUS_UNAUTHORIZED
*/
public const UNAUTHORIZED = 401;
/**
* @deprecated
* @see ResponseStatus::STATUS_PAYMENT_REQUIRED
*/
public const PAYMENT_REQUIRED = 402;
/**
* @deprecated
* @see ResponseStatus::STATUS_FORBIDDEN
*/
public const FORBIDDEN = 403;
/**
* @deprecated
* @see ResponseStatus::STATUS_NOT_FOUND
*/
public const NOT_FOUND = 404;
/**
* @deprecated
* @see ResponseStatus::STATUS_METHOD_NOT_ALLOWED
*/
public const METHOD_NOT_ALLOWED = 405;
/**
* @deprecated
* @see ResponseStatus::STATUS_NOT_ACCEPTABLE
*/
public const NOT_ACCEPTABLE = 406;
/**
* @deprecated
* @see ResponseStatus::STATUS_PROXY_AUTHENTICATION_REQUIRED
*/
public const PROXY_AUTHENTICATION_REQUIRED = 407;
/**
* @deprecated
* @see ResponseStatus::STATUS_REQUEST_TIMEOUT
*/
public const REQUEST_TIMEOUT = 408;
/**
* @deprecated
* @see ResponseStatus::STATUS_CONFLICT
*/
public const CONFLICT = 409;
/**
* @deprecated
* @see ResponseStatus::STATUS_GONE
*/
public const GONE = 410;
/**
* @deprecated
* @see ResponseStatus::STATUS_LENGTH_REQUIRED
*/
public const LENGTH_REQUIRED = 411;
/**
* @deprecated
* @see ResponseStatus::STATUS_PRECONDITION_FAILED
*/
public const PRECONDITION_FAILED = 412;
/**
* @deprecated
* @see ResponseStatus::STATUS_REQUEST_ENTITY_TOO_LARGE
*/
public const REQUEST_ENTITY_TOO_LARGE = 413;
/**
* @deprecated
* @see ResponseStatus::STATUS_REQUEST_URI_TOO_LONG
*/
public const REQUEST_URI_TOO_LONG = 414;
/**
* @deprecated
* @see ResponseStatus::STATUS_UNSUPPORTED_MEDIA_TYPE
*/
public const UNSUPPORTED_MEDIA_TYPE = 415;
/**
* @deprecated
* @see ResponseStatus::STATUS_REQUESTED_RANGE_NOT_SATISFIABLE
*/
public const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
/**
* @deprecated
* @see ResponseStatus::STATUS_EXPECTATION_FAILED
*/
public const EXPECTATION_FAILED = 417;
/**
* @deprecated
* @see ResponseStatus::STATUS_I_M_A_TEAPOT
*/
public const I_M_A_TEAPOT = 418;
/**
* @deprecated
* @see ResponseStatus::STATUS_INTERNAL_SERVER_ERROR
*/
public const INTERNAL_SERVER_ERROR = 500;
/**
* @deprecated
* @see ResponseStatus::STATUS_NOT_IMPLEMENTED
*/
public const NOT_IMPLEMENTED = 501;
/**
* @deprecated
* @see ResponseStatus::STATUS_BAD_GATEWAY
*/
public const BAD_GATEWAY = 502;
/**
* @deprecated
* @see ResponseStatus::STATUS_SERVICE_UNAVAILABLE
*/
public const SERVICE_UNAVAILABLE = 503;
/**
* @deprecated
* @see ResponseStatus::STATUS_GATEWAY_TIMEOUT
*/
public const GATEWAY_TIMEOUT = 504;
/**
* @deprecated
* @see ResponseStatus::STATUS_HTTP_VERSION_NOT_SUPPORTED
*/
public const HTTP_VERSION_NOT_SUPPORTED = 505;
/**
* HTTP status codes and messages.
*
* @var array<int, string>
*/
public const CODES_AND_MESSAGES = [
100 => "Continue",
101 => "Switching Protocols",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Requested Range Not Satisfiable",
417 => "Expectation Failed",
418 => "I'm a teapot",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported",
];
/**
* Creates a new instance from the provided status.
*
* @param array{ 0: int, 1: string }|int|string|self $status
*
* @return Status
*
* @throws InvalidArgumentException When the HTTP status code is not valid.
*/
public static function from(array|int|string|Status $status): self
{
if ($status instanceof self) {
return $status;
}
$message = null;
if (is_array($status)) {
[ $code, $message ] = $status;
} elseif (is_numeric($status)) {
$code = (int) $status;
} else {
if (!preg_match('/^(\d{3})\s+(.+)$/', $status, $matches)) {
throw new InvalidArgumentException("Invalid status: $status.");
}
[ , $code, $message ] = $matches;
}
return new self($code, $message);
}
/**
* Asserts that a status code is valid.
*
* @throws StatusCodeNotValid if the status code is not valid.
*/
private static function assert_code_is_valid(int $code): void
{
if ($code >= 100 && $code < 600) {
return;
}
throw new StatusCodeNotValid($code);
}
/**
* Status code.
*/
private int $code;
protected function set_code(int $code): void
{
self::assert_code_is_valid($code);
$this->code = $code;
}
protected function get_code(): int
{
return $this->code;
}
/**
* Whether the status is valid.
*
* A status is considered valid when its code is between 100 and 600, 100 included.
*/
protected function get_is_valid(): bool
{
return $this->code >= 100 && $this->code < 600;
}
/**
* Whether the status is informational.
*
* A status is considered informational when its code is between 100 and 200, 100 included.
*/
protected function get_is_informational(): bool
{
return $this->code >= 100 && $this->code < 200;
}
/**
* Whether the status is successful.
*
* A status is considered successful when its code is between 200 and 300, 200 included.
*/
protected function get_is_successful(): bool
{
return $this->code >= 200 && $this->code < 300;
}
/**
* Whether the status is a redirection.
*
* A status is considered to be a redirection when its code is between 300 and 400, 300
* included.
*/
protected function get_is_redirect(): bool
{
return $this->code >= 300 && $this->code < 400;
}
/**
* Whether the status is a client error.
*
* A status is considered a client error when its code is between 400 and 500, 400
* included.
*/
protected function get_is_client_error(): bool
{
return $this->code >= 400 && $this->code < 500;
}
/**
* Whether the status is a server error.
*
* A status is considered a server error when its code is between 500 and 600, 500
* included.
*/
protected function get_is_server_error(): bool
{
return $this->code >= 500 && $this->code < 600;
}
/**
* Whether the status is ok.
*
* A status is considered ok when its code is {@see ResponseStatus::STATUS_OK}.
*/
protected function get_is_ok(): bool
{
return $this->code == ResponseStatus::STATUS_OK;
}
/**
* Whether the status is forbidden.
*
* A status is considered forbidden ok when its code is {@see ResponseStatus::STATUS_FORBIDDEN}.
*/
protected function get_is_forbidden(): bool
{
return $this->code == ResponseStatus::STATUS_FORBIDDEN;
}
/**
* Whether the status is not found.
*
* A status is considered not found when its code is {@see ResponseStatus::STATUS_NOT_FOUND}.
*/
protected function get_is_not_found(): bool
{
return $this->code == ResponseStatus::STATUS_NOT_FOUND;
}
/**
* Whether the status is empty.
*
* A status is considered empty when its code is {@see ResponseStatus::CREATED},
* {@see ResponseStatus::NO_CONTENT} or {@see ResponseStatus::NOT_MODIFIED}.
*/
protected function get_is_empty(): bool
{
static $range = [
ResponseStatus::STATUS_CREATED,
ResponseStatus::STATUS_NO_CONTENT,
ResponseStatus::STATUS_NOT_MODIFIED,
];
return in_array($this->code, $range);
}
/**
* Whether the status is cacheable.
*/
protected function get_is_cacheable(): bool
{
static $range = [
ResponseStatus::STATUS_OK,
ResponseStatus::STATUS_NON_AUTHORITATIVE_INFORMATION,
ResponseStatus::STATUS_MULTIPLE_CHOICES,
ResponseStatus::STATUS_MOVED_PERMANENTLY,
ResponseStatus::STATUS_NOT_FOUND,
ResponseStatus::STATUS_NOT_FOUND,
ResponseStatus::STATUS_GONE,
];
return in_array($this->code, $range);
}
/**
* Message describing the status code.
*
* @var string|null
*/
private $message;
protected function set_message(?string $message): void
{
$this->message = $message;
}
protected function get_message(): string
{
$message = $this->message;
$code = $this->code;
if (!$message && $code) {
$message = self::CODES_AND_MESSAGES[$code];
}
return $message;
}
public function __construct(int $code = ResponseStatus::STATUS_OK, ?string $message = null)
{
self::assert_code_is_valid($code);
$this->code = $code;
$this->message = $message ?: self::CODES_AND_MESSAGES[$code];
}
public function __toString()
{
return "$this->code " . $this->get_message();
}
}