Completed
Push — master ( 43515f...3205a5 )
by Derek
02:22
created

Host   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 96
ccs 24
cts 24
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B compileValidPattern() 0 26 1
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
/**
5
 * Class Host
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8
class Host extends AbstractUriPart
9
{
10
    protected static $unencoded_characters = array();
11
12
    protected static $compositions = array(
13
        "unreserved_characters",
14
        "sub_delims_characters",
15
    );
16
17
    protected static $part_pattern;
18
19
    /**
20
     * Host constructor. Accepts a string representing a URI host component. Construction will throw an exception if
21
     * the host is not a string.
22
     *
23
     * Construction accepts strings that have been percent-encoded as well as strings that have not been percent-encoded
24
     * and will encode invalid characters.
25
     *
26
     * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded
27
     * string, resulting in double-encoding.
28
     *
29
     * host          = IP-literal / IPv4address / reg-name
30
     *
31
     * IP-literal    = "[" ( IPv6address / IPvFuture  ) "]"
32
     *
33
     * IPvFuture     = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" )
34
     *
35
     * IPv6address   =                            6( h16 ":" ) ls32
36
     * /                       "::" 5( h16 ":" ) ls32
37
     * / [               h16 ] "::" 4( h16 ":" ) ls32
38
     * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
39
     * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
40
     * / [ *3( h16 ":" ) h16 ] "::"    h16 ":"   ls32
41
     * / [ *4( h16 ":" ) h16 ] "::"              ls32
42
     * / [ *5( h16 ":" ) h16 ] "::"              h16
43
     * / [ *6( h16 ":" ) h16 ] "::"
44
     *
45
     * h16           = 1*4HEXDIG
46
     * ls32          = ( h16 ":" h16 ) / IPv4address
47
     * IPv4address   = dec-octet "." dec-octet "." dec-octet "." dec-octet
48
     *
49
     * dec-octet     = DIGIT                 ; 0-9
50
     * / %x31-39 DIGIT         ; 10-99
51
     * / "1" 2DIGIT            ; 100-199
52
     * / "2" %x30-34 DIGIT     ; 200-249
53
     * / "25" %x30-35          ; 250-255
54
     *
55
     * reg-name      = *( unreserved / pct-encoded / sub-delims )
56
     *
57
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
58
     *
59
     * @throws \InvalidArgumentException given a non-string
60
     *
61
     * @param string $host     A string representing a URI host
62
     */
63 117
    public function __construct($host)
64
    {
65 117
        if (is_string($host)) {
66 111
            $host = strtolower($host);
67 111
        }
68
69 117
        parent::__construct($host, "Host");
70 111
    }
71
72
    /**
73
     * Compiles validation patterns for a URI host.
74
     *
75
     * @return void
76
     */
77 138
    protected static function compileValidPattern()
78
    {
79 138
        self::compileUnencodedCharacters();
80
81 138
        $valid_character_string = self::$unreserved_pattern . self::$sub_delims_pattern;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $valid_character_string exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
82
83
        $ip_v_future_pattern = '\[' .
84 138
            '[' .
85 138
            '\:' . $valid_character_string .
86 138
            ']+' .
87 138
            '\]';
88
89
        $reg_name_pattern = '([' .
90 138
            $valid_character_string .
91 138
            ']|' .
92 138
            self::$pct_encoded_pattern .
93 138
            ')*';
94
95 138
        self::$part_pattern = $ip_v_future_pattern . '|'. $reg_name_pattern;
96
97 138
        self::$valid_pattern = '/^$|^' . //allows part to be empty
98 138
            $ip_v_future_pattern .
99 138
            '$|^' .
100 138
            $reg_name_pattern .
101 138
            '$/';
102 138
    }
103
}
104