Skip to content

Commit 0070d9c

Browse files
committed
New class HttpCodes
1 parent d481958 commit 0070d9c

File tree

3 files changed

+275
-1
lines changed

3 files changed

+275
-1
lines changed

src/Driver/Rmccue.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace JBZoo\HttpClient\Driver;
1717

1818
use JBZoo\HttpClient\Exception;
19+
use JBZoo\HttpClient\HttpCodes;
1920
use JBZoo\HttpClient\Options;
2021
use JBZoo\HttpClient\Request;
2122
use JBZoo\HttpClient\Response;
@@ -27,7 +28,7 @@
2728
*/
2829
class Rmccue extends AbstractDriver
2930
{
30-
private const INVALID_CODE_LINE = 400;
31+
private const INVALID_CODE_LINE = HttpCodes::BAD_REQUEST;
3132

3233
/**
3334
* @inheritDoc

src/HttpCodes.php

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
<?php
2+
3+
/**
4+
* JBZoo Toolbox - Http-Client
5+
*
6+
* This file is part of the JBZoo Toolbox project.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @package Http-Client
11+
* @license MIT
12+
* @copyright Copyright (C) JBZoo.com, All rights reserved.
13+
* @link https://github.com/JBZoo/Http-Client
14+
*/
15+
16+
namespace JBZoo\HttpClient;
17+
18+
/**
19+
* Class HttpCodes
20+
* @package JBZoo\HttpClient
21+
*/
22+
class HttpCodes
23+
{
24+
/**
25+
* @var string[]
26+
*/
27+
private static $phrases = [
28+
self::CONTINUE => 'Continue',
29+
self::SWITCHING_PROTOCOLS => 'Switching Protocols',
30+
self::PROCESSING => 'Processing',
31+
self::EARLY_HINTS => 'Early Hints',
32+
33+
self::OK => 'OK',
34+
self::CREATED => 'Created',
35+
self::ACCEPTED => 'Accepted',
36+
self::NON_AUTHORITATIVE_INFO => 'Non-Authoritative Information',
37+
self::NO_CONTENT => 'No Content',
38+
self::RESET_CONTENT => 'Reset Content',
39+
self::PARTIAL_CONTENT => 'Partial Content',
40+
self::MULTI_STATUS => 'Multi-status',
41+
self::ALREADY_REPORTED => 'Already Reported',
42+
self::IM_USED => 'IM Used',
43+
44+
self::MULTIPLE_CHOICES => 'Multiple Choices',
45+
self::MOVED_PERMANENTLY => 'Moved Permanently',
46+
self::FOUND => 'Found',
47+
self::SEE_OTHER => 'See Other',
48+
self::NOT_MODIFIED => 'Not Modified',
49+
self::USE_PROXY => 'Use Proxy',
50+
self::SWITCH_PROXY => 'Switch Proxy',
51+
self::TEMPORARY_REDIRECT => 'Temporary Redirect',
52+
53+
self::BAD_REQUEST => 'Bad Request',
54+
self::UNAUTHORIZED => 'Unauthorized',
55+
self::PAYMENT_REQUIRED => 'Payment Required',
56+
self::FORBIDDEN => 'Forbidden',
57+
self::NOT_FOUND => 'Not Found',
58+
self::METHOD_NOT_ALLOWED => 'Method Not Allowed',
59+
self::NOT_ACCEPTABLE => 'Not Acceptable',
60+
self::PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
61+
self::REQUEST_TIMEOUT => 'Request Time-out',
62+
self::CONFLICT => 'Conflict',
63+
self::GONE => 'Gone',
64+
self::LENGTH_REQUIRED => 'Length Required',
65+
self::PRECONDITION_FAILED => 'Precondition Failed',
66+
self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large',
67+
self::REQUEST_URI_TOO_LONG => 'Request-URI Too Large',
68+
self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
69+
self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested range not satisfiable',
70+
self::EXPECTATION_FAILED => 'Expectation Failed',
71+
self::IM_A_TEAPOT => "I'm a teapot",
72+
self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity',
73+
self::LOCKED => 'Locked',
74+
self::FAILED_DEPENDENCY => 'Failed Dependency',
75+
self::UNORDERED_COLLECTION => 'Unordered Collection',
76+
self::UPGRADE_REQUIRED => 'Upgrade Required',
77+
self::PRECONDITION_REQUIRED => 'Precondition Required',
78+
self::TOO_MANY_REQUESTS => 'Too Many Requests',
79+
self::REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large',
80+
self::UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
81+
82+
self::INTERNAL_SERVER_ERROR => 'Internal Server Error',
83+
self::NOT_IMPLEMENTED => 'Not Implemented',
84+
self::BAD_GATEWAY => 'Bad Gateway',
85+
self::SERVICE_UNAVAILABLE => 'Service Unavailable',
86+
self::GATEWAY_TIMEOUT => 'Gateway Time-out',
87+
self::VERSION_NOT_SUPPORTED => 'HTTP Version not supported',
88+
self::VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
89+
self::INSUFFICIENT_STORAGE => 'Insufficient Storage',
90+
self::LOOP_DETECTED => 'Loop Detected',
91+
self::NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required',
92+
self::UNKNOWN_ERROR => 'Unknown Error',
93+
self::WEB_SERVER_IS_DOWN => 'Web Server Is Down',
94+
self::CONNECTION_TIMED_OUT => 'Connection Timed Out',
95+
self::ORIGIN_IS_UNREACHABLE => 'Origin Is Unreachable',
96+
self::TIMEOUT_OCCURRED => 'A Timeout Occurred',
97+
self::SSL_HANDSHAKE_FAILED => 'SSL Handshake Failed',
98+
self::INVALID_SSL_CERTIFICATE => 'Invalid SSL Certificate',
99+
];
100+
101+
// 1xx informational response – the request was received, continuing process
102+
public const CONTINUE = 100;
103+
public const SWITCHING_PROTOCOLS = 101;
104+
public const PROCESSING = 102;
105+
public const EARLY_HINTS = 103;
106+
107+
// 2xx successful – the request was successfully received, understood, and accepted
108+
public const OK = 200;
109+
public const CREATED = 201;
110+
public const ACCEPTED = 202;
111+
public const NON_AUTHORITATIVE_INFO = 203;
112+
public const NO_CONTENT = 204;
113+
public const RESET_CONTENT = 205;
114+
public const PARTIAL_CONTENT = 206;
115+
public const MULTI_STATUS = 207;
116+
public const ALREADY_REPORTED = 208;
117+
public const IM_USED = 226;
118+
119+
// 3xx redirection – further action needs to be taken in order to complete the request
120+
public const MULTIPLE_CHOICES = 300;
121+
public const MOVED_PERMANENTLY = 301;
122+
public const FOUND = 302;
123+
public const SEE_OTHER = 303;
124+
public const NOT_MODIFIED = 304;
125+
public const USE_PROXY = 305;
126+
public const SWITCH_PROXY = 306;
127+
public const TEMPORARY_REDIRECT = 307;
128+
129+
// 4xx client error – the request contains bad syntax or cannot be fulfilled
130+
public const BAD_REQUEST = 400;
131+
public const UNAUTHORIZED = 401;
132+
public const PAYMENT_REQUIRED = 402;
133+
public const FORBIDDEN = 403;
134+
public const NOT_FOUND = 404;
135+
public const METHOD_NOT_ALLOWED = 405;
136+
public const NOT_ACCEPTABLE = 406;
137+
public const PROXY_AUTHENTICATION_REQUIRED = 407;
138+
public const REQUEST_TIMEOUT = 408;
139+
public const CONFLICT = 409;
140+
public const GONE = 410;
141+
public const LENGTH_REQUIRED = 411;
142+
public const PRECONDITION_FAILED = 412;
143+
public const REQUEST_ENTITY_TOO_LARGE = 413;
144+
public const REQUEST_URI_TOO_LONG = 414;
145+
public const UNSUPPORTED_MEDIA_TYPE = 415;
146+
public const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
147+
public const EXPECTATION_FAILED = 417;
148+
public const IM_A_TEAPOT = 418;
149+
public const UNPROCESSABLE_ENTITY = 422;
150+
public const LOCKED = 423;
151+
public const FAILED_DEPENDENCY = 424;
152+
public const UNORDERED_COLLECTION = 425;
153+
public const UPGRADE_REQUIRED = 426;
154+
public const PRECONDITION_REQUIRED = 428;
155+
public const TOO_MANY_REQUESTS = 429;
156+
public const REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
157+
public const UNAVAILABLE_FOR_LEGAL_REASONS = 451;
158+
public const CLIENT_CLOSED_REQUEST = 499;
159+
160+
// 5xx server error – the server failed to fulfil an apparently valid request
161+
public const INTERNAL_SERVER_ERROR = 500;
162+
public const NOT_IMPLEMENTED = 501;
163+
public const BAD_GATEWAY = 502;
164+
public const SERVICE_UNAVAILABLE = 503;
165+
public const GATEWAY_TIMEOUT = 504;
166+
public const VERSION_NOT_SUPPORTED = 505;
167+
public const VARIANT_ALSO_NEGOTIATES = 506;
168+
public const INSUFFICIENT_STORAGE = 507;
169+
public const LOOP_DETECTED = 508;
170+
public const NETWORK_AUTHENTICATION_REQUIRED = 511;
171+
public const UNKNOWN_ERROR = 520;
172+
public const WEB_SERVER_IS_DOWN = 521;
173+
public const CONNECTION_TIMED_OUT = 522;
174+
public const ORIGIN_IS_UNREACHABLE = 523;
175+
public const TIMEOUT_OCCURRED = 524;
176+
public const SSL_HANDSHAKE_FAILED = 525;
177+
public const INVALID_SSL_CERTIFICATE = 526;
178+
179+
/**
180+
* @param int $code
181+
* @return bool
182+
*/
183+
public static function isSuccessful(int $code): bool
184+
{
185+
return $code >= self::OK && $code < self::MULTIPLE_CHOICES;
186+
}
187+
188+
/**
189+
* @param int $code
190+
* @return bool
191+
*/
192+
public static function isRedirect(int $code): bool
193+
{
194+
return $code >= self::MULTIPLE_CHOICES && $code < self::BAD_REQUEST;
195+
}
196+
197+
/**
198+
* @param int $code
199+
* @return bool
200+
*/
201+
public static function isForbidden(int $code): bool
202+
{
203+
return $code === self::FORBIDDEN;
204+
}
205+
206+
/**
207+
* @param int $code
208+
* @return bool
209+
*/
210+
public static function isNotFound(int $code): bool
211+
{
212+
return $code === self::NOT_FOUND;
213+
}
214+
215+
/**
216+
* @param int $code
217+
* @return bool
218+
*/
219+
public static function isUnauthorized(int $code): bool
220+
{
221+
return $code === self::UNAUTHORIZED;
222+
}
223+
224+
/**
225+
* @param int $code
226+
* @return bool
227+
*/
228+
public static function hasAccess(int $code): bool
229+
{
230+
return !self::isForbidden($code) && !self::isUnauthorized($code);
231+
}
232+
233+
/**
234+
* @param int $code
235+
* @return bool
236+
*/
237+
public static function isError(int $code): bool
238+
{
239+
return $code >= self::BAD_REQUEST;
240+
}
241+
242+
/**
243+
* @param int $code
244+
* @return bool
245+
*/
246+
public static function isFatalError(int $code): bool
247+
{
248+
return $code >= self::INTERNAL_SERVER_ERROR;
249+
}
250+
251+
/**
252+
* @param int $code
253+
* @return string|null
254+
*/
255+
public static function getDescriptionByCode(int $code): ?string
256+
{
257+
return self::$phrases[$code] ?? null;
258+
}
259+
}

tests/OtherTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use JBZoo\Event\EventManager;
1919
use JBZoo\HttpClient\HttpClient;
20+
use JBZoo\HttpClient\HttpCodes;
2021
use JBZoo\HttpClient\Request;
2122
use JBZoo\HttpClient\Response;
2223

@@ -260,4 +261,17 @@ public function testEventManagerException()
260261

261262
isSame(1, $counter);
262263
}
264+
265+
public function testHttpCodes()
266+
{
267+
isSame(true, HttpCodes::isSuccessful(200));
268+
isSame(true, HttpCodes::isRedirect(301));
269+
isSame(true, HttpCodes::isError(404));
270+
isSame(true, HttpCodes::isFatalError(500));
271+
isSame(true, HttpCodes::isForbidden(403));
272+
isSame(true, HttpCodes::isUnauthorized(401));
273+
isSame(true, HttpCodes::hasAccess(200));
274+
isSame('OK', HttpCodes::getDescriptionByCode(200));
275+
isSame(null, HttpCodes::getDescriptionByCode(2000));
276+
}
263277
}

0 commit comments

Comments
 (0)