|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.8 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Http\Traits\Response; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Http\Constants\StatusCode; |
|
18
|
|
|
use InvalidArgumentException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Trait Status |
|
22
|
|
|
* @package Quantum\Http\Response |
|
23
|
|
|
*/ |
|
24
|
|
|
trait Status |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Status code |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $__statusCode = StatusCode::OK; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Status texts |
|
35
|
|
|
* @var string[] |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $texts = [ |
|
38
|
|
|
StatusCode::CONTINUE => 'Continue', |
|
39
|
|
|
StatusCode::SWITCHING_PROTOCOLS => 'Switching Protocols', |
|
40
|
|
|
StatusCode::PROCESSING => 'Processing', |
|
41
|
|
|
StatusCode::EARLY_HINTS => 'Early Hints', |
|
42
|
|
|
|
|
43
|
|
|
StatusCode::OK => 'OK', |
|
44
|
|
|
StatusCode::CREATED => 'Created', |
|
45
|
|
|
StatusCode::ACCEPTED => 'Accepted', |
|
46
|
|
|
StatusCode::NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information', |
|
47
|
|
|
StatusCode::NO_CONTENT => 'No Content', |
|
48
|
|
|
StatusCode::RESET_CONTENT => 'Reset Content', |
|
49
|
|
|
StatusCode::PARTIAL_CONTENT => 'Partial Content', |
|
50
|
|
|
StatusCode::MULTI_STATUS => 'Multi-Status', |
|
51
|
|
|
StatusCode::ALREADY_REPORTED => 'Already Reported', |
|
52
|
|
|
StatusCode::IM_USED => 'IM Used', |
|
53
|
|
|
|
|
54
|
|
|
StatusCode::MULTIPLE_CHOICES => 'Multiple Choices', |
|
55
|
|
|
StatusCode::MOVED_PERMANENTLY => 'Moved Permanently', |
|
56
|
|
|
StatusCode::FOUND => 'Found', |
|
57
|
|
|
StatusCode::SEE_OTHER => 'See Other', |
|
58
|
|
|
StatusCode::NOT_MODIFIED => 'Not Modified', |
|
59
|
|
|
StatusCode::USE_PROXY => 'Use Proxy', |
|
60
|
|
|
StatusCode::SWITCH_PROXY => 'Switch Proxy', |
|
61
|
|
|
StatusCode::TEMPORARY_REDIRECT => 'Temporary Redirect', |
|
62
|
|
|
StatusCode::PERMANENT_REDIRECT => 'Permanent Redirect', |
|
63
|
|
|
|
|
64
|
|
|
StatusCode::BAD_REQUEST => 'Bad Request', |
|
65
|
|
|
StatusCode::UNAUTHORIZED => 'Unauthorized', |
|
66
|
|
|
StatusCode::PAYMENT_REQUIRED => 'Payment Required', |
|
67
|
|
|
StatusCode::FORBIDDEN => 'Forbidden', |
|
68
|
|
|
StatusCode::NOT_FOUND => 'Not Found', |
|
69
|
|
|
StatusCode::METHOD_NOT_ALLOWED => 'Method Not Allowed', |
|
70
|
|
|
StatusCode::NOT_ACCEPTABLE => 'Not Acceptable', |
|
71
|
|
|
StatusCode::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required', |
|
72
|
|
|
StatusCode::REQUEST_TIMEOUT => 'Request Timeout', |
|
73
|
|
|
StatusCode::CONFLICT => 'Conflict', |
|
74
|
|
|
StatusCode::GONE => 'Gone', |
|
75
|
|
|
StatusCode::LENGTH_REQUIRED => 'Length Required', |
|
76
|
|
|
StatusCode::PRECONDITION_FAILED => 'Precondition Failed', |
|
77
|
|
|
StatusCode::PAYLOAD_TOO_LARGE => 'Payload Too Large', |
|
78
|
|
|
StatusCode::URI_TOO_LONG => 'URI Too Long', |
|
79
|
|
|
StatusCode::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', |
|
80
|
|
|
StatusCode::RANGE_NOT_SATISFIABLE => 'Range Not Satisfiable', |
|
81
|
|
|
StatusCode::EXPECTATION_FAILED => 'Expectation Failed', |
|
82
|
|
|
StatusCode::MISDIRECTED_REQUEST => 'Misdirected Request', |
|
83
|
|
|
StatusCode::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', |
|
84
|
|
|
StatusCode::LOCKED => 'Locked', |
|
85
|
|
|
StatusCode::FAILED_DEPENDENCY => 'Failed Dependency', |
|
86
|
|
|
StatusCode::TOO_EARLY => 'Too Early', |
|
87
|
|
|
StatusCode::UPGRADE_REQUIRED => 'Upgrade Required', |
|
88
|
|
|
StatusCode::PRECONDITION_REQUIRED => 'Precondition Required', |
|
89
|
|
|
StatusCode::TOO_MANY_REQUESTS => 'Too Many Requests', |
|
90
|
|
|
StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large', |
|
91
|
|
|
StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons', |
|
92
|
|
|
|
|
93
|
|
|
StatusCode::INTERNAL_SERVER_ERROR => 'Internal Server Error', |
|
94
|
|
|
StatusCode::NOT_IMPLEMENTED => 'Not Implemented', |
|
95
|
|
|
StatusCode::BAD_GATEWAY => 'Bad Gateway', |
|
96
|
|
|
StatusCode::SERVICE_UNAVAILABLE => 'Service Unavailable', |
|
97
|
|
|
StatusCode::GATEWAY_TIMEOUT => 'Gateway Timeout', |
|
98
|
|
|
StatusCode::HTTP_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported', |
|
99
|
|
|
StatusCode::VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates', |
|
100
|
|
|
StatusCode::INSUFFICIENT_STORAGE => 'Insufficient Storage', |
|
101
|
|
|
StatusCode::LOOP_DETECTED => 'Loop Detected', |
|
102
|
|
|
StatusCode::NOT_EXTENDED => 'Not Extended', |
|
103
|
|
|
StatusCode::NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required', |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Gets the reason phrase for a given HTTP status code. |
|
108
|
|
|
* @param int $code |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public static function getText(int $code): string |
|
112
|
|
|
{ |
|
113
|
|
|
if (!isset(self::$texts[$code])) { |
|
114
|
|
|
throw new InvalidArgumentException("Unknown HTTP status code: {$code}"); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return self::$texts[$code]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Sets the status code |
|
122
|
|
|
* @param int $code |
|
123
|
|
|
*/ |
|
124
|
|
|
public static function setStatusCode(int $code) |
|
125
|
|
|
{ |
|
126
|
|
|
if (!isset(self::$texts[$code])) { |
|
127
|
|
|
throw new InvalidArgumentException(sprintf('The HTTP status code "%s" is not valid.', $code)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
self::$__statusCode = $code; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Gets the status code |
|
135
|
|
|
* @return int |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function getStatusCode(): int |
|
138
|
|
|
{ |
|
139
|
|
|
return self::$__statusCode; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Gets the status text |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function getStatusText(): string |
|
147
|
|
|
{ |
|
148
|
|
|
return self::getText(self::$__statusCode); |
|
149
|
|
|
} |
|
150
|
|
|
} |