Passed
Push — master ( e5b7b4...ec2be8 )
by Julien
14:08
created

StatusCode::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Http;
12
13
/**
14
 * Class StatusCode
15
 * According to Wikipedia List of HTTP status codes
16
 *
17
 * Example:
18
 * ```php
19
 *  StatusCode::getMessage[StatusCode::OK] // 'OK'
20
 *  StatusCode::getMessage[200] // 'OK'
21
 *  StatusCode::CODE[200] // 'OK'
22
 *  StatusCode::OK // 200
23
 * ```
24
 *
25
 * @link https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
26
 *
27
 * @author Julien Turbide <[email protected]>
28
 * @copyright Zemit Team <[email protected]>
29
 *
30
 * @since 1.0
31
 * @version 1.0
32
 *
33
 * @package Zemit\Http
34
 */
35
class StatusCode
36
{
37
    const CODE = [
38
        100 => 'Continue',
39
        101 => 'Switching Protocols',
40
        102 => 'Processing',
41
        200 => 'OK',
42
        201 => 'Created',
43
        202 => 'Accepted',
44
        203 => 'Non-Authoritative Information',
45
        204 => 'No Content',
46
        205 => 'Reset Content',
47
        206 => 'Partial Content',
48
        207 => 'Multi-Status',
49
        208 => 'Already Reported',
50
        226 => 'IM Used',
51
        300 => 'Multiple Choices',
52
        301 => 'Moved Permanently',
53
        302 => 'Found',
54
        303 => 'See Other',
55
        304 => 'Not Modified',
56
        305 => 'Use Proxy',
57
        306 => 'Switch Proxy',
58
        307 => 'Temporary Redirect',
59
        308 => 'Permanent Redirect',
60
        400 => 'Bad Request',
61
        401 => 'Unauthorized',
62
        402 => 'Payment Required',
63
        403 => 'Forbidden',
64
        404 => 'Not Found',
65
        405 => 'Method Not Allowed',
66
        406 => 'Not Acceptable',
67
        407 => 'Proxy Authentication Required',
68
        408 => 'Request Timeout',
69
        409 => 'Conflict',
70
        410 => 'Gone',
71
        411 => 'Length Required',
72
        412 => 'Precondition Failed',
73
        413 => 'Request Entity Too Large',
74
        414 => 'Request-URI Too Long',
75
        415 => 'Unsupported Media Type',
76
        416 => 'Requested Range Not Satisfiable',
77
        417 => 'Expectation Failed',
78
        418 => 'I\'m a teapot',
79
        419 => 'Authentication Timeout',
80
        420 => 'Method Failure',
81
        422 => 'Unprocessable Entity',
82
        423 => 'Locked',
83
        424 => 'Failed Dependency',
84
        426 => 'Upgrade Required',
85
        428 => 'Precondition Required',
86
        429 => 'Too Many Requests',
87
        431 => 'Request Header Fields Too Large',
88
        440 => 'Login Timeout',
89
        444 => 'No Response',
90
        449 => 'Retry With',
91
        450 => 'Blocked by Windows Parental Controls',
92
        451 => 'Unavailable For Legal Reasons',
93
        494 => 'Request Header Too Large',
94
        495 => 'Cert Error',
95
        496 => 'No Cert',
96
        497 => 'HTTP to HTTPS',
97
        498 => 'Token expired/invalid',
98
        499 => 'Client Closed Request',
99
        500 => 'Internal Server Error',
100
        501 => 'Not Implemented',
101
        502 => 'Bad Gateway',
102
        503 => 'Service Unavailable',
103
        504 => 'Gateway Timeout',
104
        505 => 'HTTP Version Not Supported',
105
        506 => 'Variant Also Negotiates',
106
        507 => 'Insufficient Storage',
107
        508 => 'Loop Detected',
108
        509 => 'Bandwidth Limit Exceeded',
109
        510 => 'Not Extended',
110
        511 => 'Network Authentication Required',
111
        598 => 'Network read timeout error',
112
        599 => 'Network connect timeout error',
113
    ];
114
    
115
    const CONTINUE = 100;
116
    const SWITCHING_PROTOCOLS = 101;
117
    const PROCESSING = 102;
118
    const OK = 200;
119
    const CREATED = 201;
120
    const ACCEPTED = 202;
121
    const NON_AUTHORITATIVE_INFORMATION = 203;
122
    const NO_CONTENT = 204;
123
    const RESET_CONTENT = 205;
124
    const PARTIAL_CONTENT = 206;
125
    const MULTI_STATUS = 207;
126
    const ALREADY_REPORTED = 208;
127
    const IM_USED = 226;
128
    const MULTIPLE_CHOICES = 300;
129
    const MOVED_PERMANENTLY = 301;
130
    const FOUND = 302;
131
    const SEE_OTHER = 303;
132
    const NOT_MODIFIED = 304;
133
    const USE_PROXY = 305;
134
    const SWITCH_PROXY = 306;
135
    const TEMPORARY_REDIRECT = 307;
136
    const PERMANENT_REDIRECT = 308;
137
    const BAD_REQUEST = 400;
138
    const UNAUTHORIZED = 401;
139
    const PAYMENT_REQUIRED = 402;
140
    const FORBIDDEN = 403;
141
    const NOT_FOUND = 404;
142
    const METHOD_NOT_ALLOWED = 405;
143
    const NOT_ACCEPTABLE = 406;
144
    const PROXY_AUTHENTICATION_REQUIRED = 407;
145
    const REQUEST_TIMEOUT = 408;
146
    const CONFLICT = 409;
147
    const GONE = 410;
148
    const LENGTH_REQUIRED = 411;
149
    const PRECONDITION_FAILED = 412;
150
    const REQUEST_ENTITY_TOO_LARGE = 413;
151
    const REQUEST_URI_TOO_LONG = 414;
152
    const UNSUPPORTED_MEDIA_TYPE = 415;
153
    const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
154
    const EXPECTATION_FAILED = 417;
155
    const IM_A_TEAPOT = 418;
156
    const AUTHENTICATION_TIMEOUT = 419;
157
    const METHOD_FAILURE = 420;
158
    const UNPROCESSABLE_ENTITY = 422;
159
    const LOCKED = 423;
160
    const FAILED_DEPENDENCY = 424;
161
    const UPGRADE_REQUIRED = 426;
162
    const PRECONDITION_REQUIRED = 428;
163
    const TOO_MANY_REQUESTS = 429;
164
    const REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
165
    const LOGIN_TIMEOUT = 440;
166
    const NO_RESPONSE = 444;
167
    const RETRY_WITH = 449;
168
    const BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS = 450;
169
    const UNAVAILABLE_FOR_LEGAL_REASONS = 451;
170
    const REQUEST_HEADER_TOO_LARGE = 494;
171
    const CERT_ERROR = 495;
172
    const NO_CERT = 496;
173
    const HTTP_TO_HTTPS = 497;
174
    const TOKEN_EXPIREDINVALID = 498;
175
    const CLIENT_CLOSED_REQUEST = 499;
176
    const INTERNAL_SERVER_ERROR = 500;
177
    const NOT_IMPLEMENTED = 501;
178
    const BAD_GATEWAY = 502;
179
    const SERVICE_UNAVAILABLE = 503;
180
    const GATEWAY_TIMEOUT = 504;
181
    const HTTP_VERSION_NOT_SUPPORTED = 505;
182
    const VARIANT_ALSO_NEGOTIATES = 506;
183
    const INSUFFICIENT_STORAGE = 507;
184
    const LOOP_DETECTED = 508;
185
    const BANDWIDTH_LIMIT_EXCEEDED = 509;
186
    const NOT_EXTENDED = 510;
187
    const NETWORK_AUTHENTICATION_REQUIRED = 511;
188
    const NETWORK_READ_TIMEOUT_ERROR = 598;
189
    const NETWORK_CONNECT_TIMEOUT_ERROR = 599;
190
    
191
    /**
192
     * Developer friendly constants
193
     */
194
    const FATAL_ERROR = 500;
195
    const MAINTENANCE = 503;
196
    const OVERLOADED = 503;
197
    const BUSY = 503;
198
    
199
    /**
200
     * Get the HTTP status message for the specified HTTP status code
201
     * getMessage(200) -> 'OK'
202
     */
203 1
    public static function getMessage(int $code): ?string
204
    {
205 1
        return self::CODE[$code] ?? null;
206
    }
207
    
208
    /**
209
     * Get the HTTP code from the specified HTTP status message
210
     * getCode('OK') -> 200
211
     */
212 1
    public static function getCode(string $status): ?int
213
    {
214 1
        return array_flip(self::CODE)[$status] ?? null;
215
    }
216
    
217
    /**
218
     * Get the HTTP code from the specified HTTP status message
219
     * getStatus(200) -> '200 OK'
220
     */
221 1
    public static function getStatus(int $code): ?string
222
    {
223 1
        return trim($code . ' ' . self::getMessage($code));
224
    }
225
}
226