1 | <?php |
||
24 | class Host extends AbstractHierarchicalComponent implements HostInterface |
||
25 | { |
||
26 | use HostIpTrait; |
||
27 | |||
28 | use HostnameInfoTrait; |
||
29 | |||
30 | use HostnameTrait; |
||
31 | |||
32 | /** |
||
33 | * HierarchicalComponent delimiter |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected static $separator = '.'; |
||
38 | |||
39 | /** |
||
40 | * Host literal representation |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $host; |
||
45 | |||
46 | /** |
||
47 | * New instance |
||
48 | * |
||
49 | * @param null|string $host |
||
50 | */ |
||
51 | 1009 | public function __construct($host = null) |
|
59 | |||
60 | /** |
||
61 | * Returns whether or not the host is an IDN |
||
62 | * |
||
63 | * @return bool |
||
64 | */ |
||
65 | 9 | public function isIdn() |
|
69 | |||
70 | /** |
||
71 | * Returns whether or not the host is an IP address |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 835 | public function isIp() |
|
76 | { |
||
77 | 835 | return $this->hostAsIpv4 || $this->hostAsIpv6; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Returns whether or not the host is an IPv4 address |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | 36 | public function isIpv4() |
|
86 | { |
||
87 | 36 | return $this->hostAsIpv4; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Returns whether or not the host is an IPv6 address |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | 36 | public function isIpv6() |
|
99 | |||
100 | /** |
||
101 | * Returns whether or not the host has a ZoneIdentifier |
||
102 | * |
||
103 | * @return bool |
||
104 | * |
||
105 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
106 | */ |
||
107 | 12 | public function hasZoneIdentifier() |
|
111 | |||
112 | /** |
||
113 | * Host literal setter |
||
114 | */ |
||
115 | 835 | protected function setLiteral() |
|
116 | { |
||
117 | 835 | $this->host = !$this->isIp() ? $this->__toString() : $this->data[0]; |
|
118 | 835 | } |
|
119 | |||
120 | /** |
||
121 | * Returns the instance literal representation |
||
122 | * without encoding |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 21 | public function getLiteral() |
|
127 | { |
||
128 | 21 | return $this->host; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * validate the submitted data |
||
133 | * |
||
134 | * @param string $str |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | 892 | protected function validate($str) |
|
139 | { |
||
140 | 892 | $res = $this->validateIpHost($str); |
|
141 | 892 | if (!empty($res)) { |
|
142 | 120 | return $res; |
|
143 | } |
||
144 | |||
145 | 805 | return $this->validateStringHost($str); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Retrieves a single host label. |
||
150 | * |
||
151 | * Retrieves a single host label. If the label offset has not been set, |
||
152 | * returns the default value provided. |
||
153 | * |
||
154 | * @param string $offset the label offset |
||
155 | * @param mixed $default Default value to return if the offset does not exist. |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | 3 | public function getLabel($offset, $default = null) |
|
160 | { |
||
161 | 3 | if (isset($this->data[$offset])) { |
|
162 | 3 | return $this->isIdn ? $this->data[$offset] : idn_to_ascii($this->data[$offset]); |
|
163 | } |
||
164 | |||
165 | 3 | return $default; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns an array representation of the host |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | 739 | public function toArray() |
|
177 | |||
178 | /** |
||
179 | * Returns the instance string representation; If the |
||
180 | * instance is not defined an empty string is returned |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | 865 | public function __toString() |
|
196 | |||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | 2 | public function __debugInfo() |
|
204 | |||
205 | /** |
||
206 | * @inheritdoc |
||
207 | */ |
||
208 | 12 | public static function __set_state(array $properties) |
|
212 | |||
213 | /** |
||
214 | * Returns a host in his punycode encoded form |
||
215 | * |
||
216 | * This method MUST retain the state of the current instance, and return |
||
217 | * an instance with the host transcoded using to ascii the RFC 3492 rules |
||
218 | * |
||
219 | * @see http://tools.ietf.org/html/rfc3492 |
||
220 | * |
||
221 | * @return static |
||
222 | */ |
||
223 | 108 | public function toAscii() |
|
234 | |||
235 | /** |
||
236 | * Returns a host in his IDN form |
||
237 | * |
||
238 | * This method MUST retain the state of the current instance, and return |
||
239 | * an instance with the host in its IDN form using RFC 3492 rules |
||
240 | * |
||
241 | * @see http://tools.ietf.org/html/rfc3492 |
||
242 | * |
||
243 | * @return static |
||
244 | */ |
||
245 | 81 | public function toUnicode() |
|
253 | |||
254 | /** |
||
255 | * @inheritdoc |
||
256 | */ |
||
257 | 772 | protected static function formatComponentString($data, $type) |
|
266 | |||
267 | /** |
||
268 | * Return an host without its zone identifier according to RFC6874 |
||
269 | * |
||
270 | * This method MUST retain the state of the current instance, and return |
||
271 | * an instance without the host zone identifier according to RFC6874 |
||
272 | * |
||
273 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
274 | * |
||
275 | * @return static |
||
276 | */ |
||
277 | 18 | public function withoutZoneIdentifier() |
|
285 | |||
286 | /** |
||
287 | * Validated the Host Label Count |
||
288 | * |
||
289 | * @param array $labels Host labels |
||
290 | * |
||
291 | * @throws InvalidArgumentException If the validation fails |
||
292 | */ |
||
293 | 772 | protected function assertLabelsCount(array $labels) |
|
299 | |||
300 | /** |
||
301 | * set the FQDN property |
||
302 | * |
||
303 | * @param string $str |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 787 | protected function setIsAbsolute($str) |
|
317 | |||
318 | /** |
||
319 | * @inheritdoc |
||
320 | */ |
||
321 | 60 | public function append($component) |
|
328 | } |
||
329 |