Completed
Pull Request — master (#20)
by Alexander
01:36
created

Response::fileEndingFromType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace alkemann\h2l;
4
5
/**
6
 * Abstract class Response
7
 *
8
 * @package alkemann\h2l
9
 */
10
abstract class Response
11
{
12
    public static $code_to_message = [
13
        // Informational 1xx
14
        100 => 'Continue',
15
        101 => 'Switching Protocols',
16
        // Successful 2xx
17
        200 => 'OK',
18
        201 => 'Created',
19
        202 => 'Accepted',
20
        203 => 'Non-Authoritative Information',
21
        204 => 'No Content',
22
        205 => 'Reset Content',
23
        206 => 'Partial Content',
24
        // Redirection 3xx
25
        300 => 'Multiple Choices',
26
        301 => 'Moved Permanently',
27
        302 => 'Found',
28
        303 => 'See Other',
29
        304 => 'Not Modified',
30
        305 => 'Use Proxy',
31
        307 => 'Temporary Redirect',
32
        // Client Error 4xx
33
        400 => 'Bad Request',
34
        401 => 'Unauthorized',
35
        402 => 'Payment Required',
36
        403 => 'Forbidden',
37
        404 => 'Not Found',
38
        405 => 'Method Not Allowed',
39
        406 => 'Not Acceptable',
40
        407 => 'Proxy Authentication Required',
41
        408 => 'Request Timeout',
42
        409 => 'Conflict',
43
        410 => 'Gone',
44
        411 => 'Length Required',
45
        412 => 'Precondition Failed',
46
        413 => 'Request Entity Too Large',
47
        414 => 'Request-URI Too Long',
48
        415 => 'Unsupported Media Type',
49
        416 => 'Requested Range Not Satisfiable',
50
        417 => 'Expectation Failed',
51
        // Server Error 5xx
52
        500 => 'Internal Server Error',
53
        501 => 'Not Implemented',
54
        502 => 'Bad Gateway',
55
        503 => 'Service Unavailable',
56
        504 => 'Gateway Timeout',
57
        505 => 'HTTP Version Not Supported',
58
    ];
59
60
    protected static $contentTypes = [
61
        'text/html' => 'html',
62
        'application/json' => 'json',
63
        'application/xml' => 'xml'
64
    ];
65
66
    protected $content_type = 'text/html';
67
    protected $code = 200;
68
69
    public function fileEndingFromType(string $type): string
70
    {
71
        foreach (static::$contentTypes as $type_key => $ending) {
72
            if ($type === $type_key) {
73
                return $ending;
74
            }
75
        }
76
        return 'html';
77
    }
78
79
    public function contentType(): string
80
    {
81
        return $this->content_type;
82
    }
83
84
    public function code(): int
85
    {
86
        return $this->code;
87
    }
88
89
    abstract public function render(): string;
90
}
91