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
|
122 |
|
public function __construct($host) |
64
|
|
|
{ |
65
|
122 |
|
if (is_string($host)) { |
66
|
110 |
|
$host = strtolower($host); |
67
|
110 |
|
} |
68
|
|
|
|
69
|
122 |
|
parent::__construct($host, "Host"); |
70
|
110 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Compiles validation patterns for a URI host. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
137 |
|
protected static function compileValidPattern() |
78
|
|
|
{ |
79
|
137 |
|
self::compileUnencodedCharacters(); |
80
|
|
|
|
81
|
137 |
|
$valid_characters = self::$unreserved_pattern . self::$sub_delims_pattern; |
82
|
|
|
|
83
|
137 |
|
$ip_v_future_pattern = '\[[\:' . $valid_characters . ']+\]'; |
84
|
|
|
|
85
|
137 |
|
$reg_name_pattern = '([' . $valid_characters . ']|' . self::$pct_encoded_pattern . ')*'; |
86
|
|
|
|
87
|
137 |
|
self::$part_pattern = $ip_v_future_pattern . '|'. $reg_name_pattern; |
88
|
|
|
|
89
|
137 |
|
self::$valid_pattern = '/^$|^' . //allows part to be empty |
90
|
137 |
|
$ip_v_future_pattern . '$|^' . $reg_name_pattern . '$/'; |
91
|
137 |
|
} |
92
|
|
|
} |
93
|
|
|
|