HttpCodes   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 74
dl 0
loc 97
rs 10
c 1
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 6 2
A reasonPhraseFor() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of slick/http
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slick\Http\Message;
13
14
use Slick\Http\Message\Exception\InvalidArgumentException;
15
16
/**
17
 * HTTP Codes
18
 *
19
 * @package Slick\Http\Message
20
 */
21
final class HttpCodes
22
{
23
    /** @var array<int, string> */
24
    private static array $codes = [
25
        100 => "Continue",
26
        101 => "Switching Protocols",
27
        102 => "Processing",
28
        200 => "OK",
29
        201 => "Created",
30
        202 => "Accepted",
31
        203 => "Non-Authoritative Information",
32
        204 => "No Content",
33
        205 => "Reset Content",
34
        206 => "Partial Content",
35
        207 => "Multi-Status",
36
        208 => "Already Reported",
37
        226 => "IM Used",
38
        300 => "Multiple Choices",
39
        301 => "Moved Permanently",
40
        302 => "Found",
41
        303 => "See Other",
42
        304 => "Not Modified",
43
        305 => "Use Proxy",
44
        306 => "(Unused)",
45
        307 => "Temporary Redirect",
46
        308 => "Permanent Redirect",
47
        400 => "Bad Request",
48
        401 => "Unauthorized",
49
        402 => "Payment Required",
50
        403 => "Forbidden",
51
        404 => "Not Found",
52
        405 => "Method Not Allowed",
53
        406 => "Not Acceptable",
54
        407 => "Proxy Authentication Required",
55
        408 => "Request Timeout",
56
        409 => "Conflict",
57
        410 => "Gone",
58
        411 => "Length Required",
59
        412 => "Precondition Failed",
60
        413 => "Payload Too Large",
61
        414 => "URI Too Long",
62
        415 => "Unsupported Media Type",
63
        416 => "Range Not Satisfiable",
64
        417 => "Expectation Failed",
65
        421 => "Misdirected Request",
66
        422 => "Unprocessable Entity",
67
        423 => "Locked",
68
        424 => "Failed Dependency",
69
        425 => "Unassigned",
70
        426 => "Upgrade Required",
71
        427 => "Unassigned",
72
        428 => "Precondition Required",
73
        429 => "Too Many Requests",
74
        430 => "Unassigned",
75
        431 => "Request Header Fields Too Large",
76
        451 => "Unavailable For Legal Reasons",
77
        500 => "Internal Server Error",
78
        501 => "Not Implemented",
79
        502 => "Bad Gateway",
80
        503 => "Service Unavailable",
81
        504 => "Gateway Timeout",
82
        505 => "HTTP Version Not Supported",
83
        506 => "Variant Also Negotiates",
84
        507 => "Insufficient Storage",
85
        508 => "Loop Detected",
86
        509 => "Unassigned",
87
        510 => "Not Extended",
88
        511 => "Network Authentication Required"
89
    ];
90
91
    /**
92
     * Get the Reason Phrase for provide code
93
     *
94
     * @param int $code
95
     *
96
     * @return string
97
     */
98
    public function reasonPhraseFor($code)
99
    {
100
        $status = '';
101
        if (\array_key_exists($code, self::$codes)) {
102
            $status = self::$codes[$code];
103
        }
104
        return $status;
105
    }
106
107
    /**
108
     * Check if the provided code is a valid HTTP status code
109
     *
110
     * @param int $code
111
     */
112
    public function check(int $code): void
113
    {
114
        $regex = '/^([12345])[0-9]{2}$/i';
115
        if (!preg_match($regex, (string) $code)) {
116
            throw new InvalidArgumentException(
117
                "Invalid HTTP response status code."
118
            );
119
        }
120
    }
121
}
122