Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
Protocol | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
validate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework HTTP Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\HTTP; |
11 | |
12 | use InvalidArgumentException; |
13 | |
14 | /** |
15 | * Class Protocol. |
16 | * |
17 | * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview |
18 | * |
19 | * @package http |
20 | */ |
21 | class Protocol |
22 | { |
23 | /** |
24 | * @see https://en.wikipedia.org/wiki/HTTP/1.0 |
25 | * |
26 | * @var string |
27 | */ |
28 | public const HTTP_1_0 = 'HTTP/1.0'; |
29 | /** |
30 | * @see https://en.wikipedia.org/wiki/HTTP/1.1 |
31 | * |
32 | * @var string |
33 | */ |
34 | public const HTTP_1_1 = 'HTTP/1.1'; |
35 | /** |
36 | * @see https://en.wikipedia.org/wiki/HTTP/2.0 |
37 | * |
38 | * @var string |
39 | */ |
40 | public const HTTP_2_0 = 'HTTP/2.0'; |
41 | /** |
42 | * @see https://en.wikipedia.org/wiki/HTTP/2 |
43 | * |
44 | * @var string |
45 | */ |
46 | public const HTTP_2 = 'HTTP/2'; |
47 | /** |
48 | * @see https://en.wikipedia.org/wiki/HTTP/3 |
49 | * |
50 | * @var string |
51 | */ |
52 | public const HTTP_3 = 'HTTP/3'; |
53 | /** |
54 | * @var array<string> |
55 | */ |
56 | protected static array $protocols = [ |
57 | 'HTTP/1.0', |
58 | 'HTTP/1.1', |
59 | 'HTTP/2.0', |
60 | 'HTTP/2', |
61 | 'HTTP/3', |
62 | ]; |
63 | |
64 | /** |
65 | * @param string $protocol |
66 | * |
67 | * @throws InvalidArgumentException for invalid protocol |
68 | * |
69 | * @return string |
70 | */ |
71 | public static function validate(string $protocol) : string |
72 | { |
73 | $valid = \strtoupper($protocol); |
74 | if (\in_array($valid, static::$protocols, true)) { |
75 | return $valid; |
76 | } |
77 | throw new InvalidArgumentException('Invalid protocol: ' . $protocol); |
78 | } |
79 | } |