Completed
Pull Request — master (#25)
by Alexander
01:37
created

Http::contentTypeFromFileEnding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l\util;
4
5
/**
6
 * Class Http
7
 *
8
 * Holds HTTP Spec constants,
9
 *
10
 * @package alkemann\h2l\util
11
 */
12
class Http
13
{
14
    const TRACE = 'TRACE';
15
    const HEAD = 'HEAD';
16
    const POST = 'POST';
17
    const CONNECT = 'CONNECT';
18
    const OPTIONS = 'OPTIONS';
19
    const PUT = 'PUT';
20
    const PATCH = 'PATCH';
21
    const DELETE = 'DELETE';
22
    const GET = 'GET';
23
24
    const CONTENT_HTML = 'text/html';
25
    const CONTENT_TEXT_XML = 'text/xml';
26
    const CONTENT_XML = 'application/xml';
27
    const CONTENT_TEXT = 'text/plain';
28
    const CONTENT_FORM = 'application/x-www-form-urlencoded';
29
    const CONTENT_JSON = 'application/json';
30
31
    const CODE_CONTINUE = 100;
32
    const CODE_SWITCHING_PROTOCOLS = 101;
33
34
    const CODE_OK = 200;
35
    const CODE_CREATED = 201;
36
    const CODE_ACCEPTED = 202;
37
    const CODE_NON_AUTHORITATIVE_INFORMATION = 203;
38
    const CODE_NO_CONTENT = 204;
39
    const CODE_RESET_CONTENT = 205;
40
    const CODE_PARTIAL_CONTENT = 206;
41
42
    const CODE_MULTIPLE_CHOICES = 300;
43
    const CODE_MOVED_PERMANENTLY = 301;
44
    const CODE_FOUND = 302;
45
    const CODE_SEE_OTHER = 303;
46
    const CODE_NOT_MODIFIED = 304;
47
    const CODE_USE_PROXY = 305;
48
    const CODE_TEMPORARY_REDIRECT = 307;
49
50
    const CODE_BAD_REQUEST = 400;
51
    const CODE_UNAUTHORIZED = 401;
52
    const CODE_PAYMENT_REQUIRED = 402;
53
    const CODE_FORBIDDEN = 403;
54
    const CODE_NOT_FOUND = 404;
55
    const CODE_METHOD_NOT_ALLOWED = 405;
56
    const CODE_NOT_ACCEPTABLE = 406;
57
    const CODE_PROXY_AUTHENTICATION_REQUIRED = 407;
58
    const CODE_REQUEST_TIMEOUT = 408;
59
    const CODE_CONFLICT = 409;
60
    const CODE_GONE = 410;
61
    const CODE_LENGTH_REQUIRED = 411;
62
    const CODE_PRECONDITION_FAILED = 412;
63
    const CODE_REQUEST_ENTITY_TOO_LARGE = 413;
64
    const CODE_REQUEST_URI_TOO_LONG = 414;
65
    const CODE_UNSUPPORTED_MEDIA_TYPE = 415;
66
    const CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
67
    const CODE_EXPECTATION_FAILED = 417;
68
69
    const CODE_INTERNAL_SERVER_ERROR = 500;
70
    const CODE_NOT_IMPLEMENTED = 501;
71
    const CODE_BAD_GATEWAY = 502;
72
    const CODE_SERVICE_UNAVAILABLE = 503;
73
    const CODE_GATEWAY_TIMEOUT = 504;
74
    const CODE_HTTP_VERSION_NOT_SUPPORTED = 505;
75
76
    protected static $contentTypeToFileEnding = [
77
        'text/html' => 'html',
78
        'application/json' => 'json',
79
        'application/xml' => 'xml',
80
        'text/xml' => 'xml',
81
    ];
82
83
    private static $code_to_message = [
84
        // Informational 1xx
85
        100 => 'Continue',
86
        101 => 'Switching Protocols',
87
        // Successful 2xx
88
        200 => 'OK',
89
        201 => 'Created',
90
        202 => 'Accepted',
91
        203 => 'Non-Authoritative Information',
92
        204 => 'No Content',
93
        205 => 'Reset Content',
94
        206 => 'Partial Content',
95
        // Redirection 3xx
96
        300 => 'Multiple Choices',
97
        301 => 'Moved Permanently',
98
        302 => 'Found',
99
        303 => 'See Other',
100
        304 => 'Not Modified',
101
        305 => 'Use Proxy',
102
        307 => 'Temporary Redirect',
103
        // Client Error 4xx
104
        400 => 'Bad Request',
105
        401 => 'Unauthorized',
106
        402 => 'Payment Required',
107
        403 => 'Forbidden',
108
        404 => 'Not Found',
109
        405 => 'Method Not Allowed',
110
        406 => 'Not Acceptable',
111
        407 => 'Proxy Authentication Required',
112
        408 => 'Request Timeout',
113
        409 => 'Conflict',
114
        410 => 'Gone',
115
        411 => 'Length Required',
116
        412 => 'Precondition Failed',
117
        413 => 'Request Entity Too Large',
118
        414 => 'Request-URI Too Long',
119
        415 => 'Unsupported Media Type',
120
        416 => 'Requested Range Not Satisfiable',
121
        417 => 'Expectation Failed',
122
        // Server Error 5xx
123
        500 => 'Internal Server Error',
124
        501 => 'Not Implemented',
125
        502 => 'Bad Gateway',
126
        503 => 'Service Unavailable',
127
        504 => 'Gateway Timeout',
128
        505 => 'HTTP Version Not Supported',
129
    ];
130
131
    public static function fileEndingFromType(string $type): string
132
    {
133
        foreach (self::$contentTypeToFileEnding as $type_key => $ending) {
134
            if ($type === $type_key) {
135
                return $ending;
136
            }
137
        }
138
        return 'html';
139
    }
140
    public static function contentTypeFromFileEnding(string $ending): string
141
    {
142
        return array_search($ending, static::$contentTypeToFileEnding);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($end...ontentTypeToFileEnding) returns the type mixed which includes types incompatible with the type-hinted return string.
Loading history...
143
    }
144
145
    public static function httpCodeToMessage(int $code): string
146
    {
147
        return self::$code_to_message[$code] ?? "Unknown";
148
    }
149
150
    public static function getRequestHeadersFromServerArray(array $server_array)
151
    {
152
        $out = [];
153
        foreach ($server_array as $name => $value) {
154
            if (substr($name, 0, 5) == "HTTP_") {
155
                $name = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($name, 5)))));
156
                $out[$name] = $value;
157
            }
158
        }
159
        if (array_key_exists("CONTENT_TYPE", $server_array)) {
160
            $out["Content-Type"] = $server_array['CONTENT_TYPE'];
161
        }
162
        if (array_key_exists("CONTENT_LENGTH", $server_array)) {
163
            $out["Content-Length"] = $server_array['CONTENT_LENGTH'];
164
        }
165
        return $out;
166
    }
167
}
168