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 | 1018 | 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 | 844 | public function isIp() |
|
76 | { |
||
77 | 844 | 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 | 844 | protected function setLiteral() |
|
116 | { |
||
117 | 844 | $this->host = !$this->isIp() ? $this->__toString() : $this->data[0]; |
|
118 | 844 | } |
|
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 | 901 | protected function validate($str) |
|
139 | { |
||
140 | 901 | $res = $this->validateIpHost($str); |
|
141 | 901 | if (!empty($res)) { |
|
142 | 120 | return $res; |
|
143 | } |
||
144 | |||
145 | 814 | 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 | 748 | public function toArray() |
|
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 874 | public function getComponent() |
|
182 | { |
||
183 | 874 | if ([] === $this->data) { |
|
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 2 | public function __debugInfo() |
|
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | 15 | public static function __set_state(array $properties) |
|
213 | |||
214 | /** |
||
215 | * Returns a host in his punycode encoded form |
||
216 | * |
||
217 | * This method MUST retain the state of the current instance, and return |
||
218 | * an instance with the host transcoded using to ascii the RFC 3492 rules |
||
219 | * |
||
220 | * @see http://tools.ietf.org/html/rfc3492 |
||
221 | * |
||
222 | * @return static |
||
223 | */ |
||
224 | 108 | public function toAscii() |
|
235 | |||
236 | /** |
||
237 | * Returns a host in his IDN form |
||
238 | * |
||
239 | * This method MUST retain the state of the current instance, and return |
||
240 | * an instance with the host in its IDN form using RFC 3492 rules |
||
241 | * |
||
242 | * @see http://tools.ietf.org/html/rfc3492 |
||
243 | * |
||
244 | * @return static |
||
245 | */ |
||
246 | 84 | public function toUnicode() |
|
254 | |||
255 | /** |
||
256 | * @inheritdoc |
||
257 | */ |
||
258 | 781 | protected static function formatComponentString($data, $type) |
|
267 | |||
268 | /** |
||
269 | * Return an host without its zone identifier according to RFC6874 |
||
270 | * |
||
271 | * This method MUST retain the state of the current instance, and return |
||
272 | * an instance without the host zone identifier according to RFC6874 |
||
273 | * |
||
274 | * @see http://tools.ietf.org/html/rfc6874#section-4 |
||
275 | * |
||
276 | * @return static |
||
277 | */ |
||
278 | 18 | public function withoutZoneIdentifier() |
|
286 | |||
287 | /** |
||
288 | * Validated the Host Label Count |
||
289 | * |
||
290 | * @param array $labels Host labels |
||
291 | * |
||
292 | * @throws InvalidArgumentException If the validation fails |
||
293 | */ |
||
294 | 781 | protected function assertLabelsCount(array $labels) |
|
300 | |||
301 | /** |
||
302 | * set the FQDN property |
||
303 | * |
||
304 | * @param string $str |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 796 | protected function setIsAbsolute($str) |
|
318 | |||
319 | /** |
||
320 | * @inheritdoc |
||
321 | */ |
||
322 | 60 | public function append($component) |
|
329 | } |
||
330 |