1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
9
|
|
|
* @version 4.1.1 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use League\Uri\Interfaces\Host as HostInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Value object representing a URI host component. |
19
|
|
|
* |
20
|
|
|
* @package League.uri |
21
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
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) |
52
|
|
|
{ |
53
|
1009 |
|
if (null !== $host) { |
54
|
901 |
|
$host = $this->validateString($host); |
55
|
892 |
|
$this->data = $this->validate($host); |
56
|
835 |
|
$this->setLiteral(); |
57
|
556 |
|
} |
58
|
943 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns whether or not the host is an IDN |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
9 |
|
public function isIdn() |
66
|
|
|
{ |
67
|
9 |
|
return $this->isIdn; |
68
|
|
|
} |
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() |
96
|
|
|
{ |
97
|
36 |
|
return $this->hostAsIpv6; |
98
|
|
|
} |
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() |
108
|
|
|
{ |
109
|
12 |
|
return $this->hasZoneIdentifier; |
110
|
|
|
} |
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() |
174
|
|
|
{ |
175
|
739 |
|
return $this->convertToAscii($this->data, !$this->isIdn); |
176
|
|
|
} |
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() |
185
|
|
|
{ |
186
|
865 |
|
if (empty($this->data)) { |
187
|
207 |
|
return ''; |
188
|
|
|
} |
189
|
|
|
|
190
|
799 |
|
if ($this->isIp()) { |
191
|
75 |
|
return $this->formatIp($this->data[0]); |
192
|
|
|
} |
193
|
|
|
|
194
|
730 |
|
return $this->formatComponentString($this->toArray(), $this->isAbsolute); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @inheritdoc |
199
|
|
|
*/ |
200
|
2 |
|
public function __debugInfo() |
201
|
|
|
{ |
202
|
2 |
|
return ['host' => $this->__toString()]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @inheritdoc |
207
|
|
|
*/ |
208
|
12 |
|
public static function __set_state(array $properties) |
209
|
|
|
{ |
210
|
12 |
|
return static::createFromArray($properties['data'], $properties['isAbsolute']); |
211
|
|
|
} |
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() |
224
|
|
|
{ |
225
|
108 |
|
if ($this->isIp() || !$this->isIdn) { |
226
|
36 |
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
75 |
|
return $this->modify($this->formatComponentString( |
230
|
75 |
|
$this->convertToAscii($this->data, $this->isIdn), |
231
|
75 |
|
$this->isAbsolute |
232
|
50 |
|
)); |
233
|
|
|
} |
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() |
246
|
|
|
{ |
247
|
81 |
|
if ($this->isIp() || $this->isIdn) { |
248
|
69 |
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
72 |
|
return $this->modify($this->formatComponentString($this->data, $this->isAbsolute)); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @inheritdoc |
256
|
|
|
*/ |
257
|
772 |
|
protected static function formatComponentString($data, $type) |
258
|
|
|
{ |
259
|
772 |
|
$hostname = implode(static::$separator, array_reverse(static::validateIterator($data))); |
260
|
760 |
|
if (self::IS_ABSOLUTE == $type) { |
261
|
36 |
|
return $hostname.static::$separator; |
262
|
|
|
} |
263
|
|
|
|
264
|
736 |
|
return $hostname; |
265
|
|
|
} |
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() |
278
|
|
|
{ |
279
|
18 |
|
if ($this->hasZoneIdentifier) { |
280
|
9 |
|
return $this->modify(substr($this->data[0], 0, strpos($this->data[0], '%'))); |
281
|
|
|
} |
282
|
|
|
|
283
|
9 |
|
return $this; |
284
|
|
|
} |
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) |
294
|
|
|
{ |
295
|
772 |
|
if (127 <= count(array_merge($this->data, $labels))) { |
296
|
3 |
|
throw new InvalidArgumentException('Invalid Hostname, verify labels count'); |
297
|
|
|
} |
298
|
769 |
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* set the FQDN property |
302
|
|
|
* |
303
|
|
|
* @param string $str |
304
|
|
|
* |
305
|
|
|
* @return string |
306
|
|
|
*/ |
307
|
787 |
|
protected function setIsAbsolute($str) |
308
|
|
|
{ |
309
|
787 |
|
$this->isAbsolute = self::IS_RELATIVE; |
310
|
787 |
|
if ('.' == mb_substr($str, -1, 1, 'UTF-8')) { |
311
|
42 |
|
$this->isAbsolute = self::IS_ABSOLUTE; |
312
|
42 |
|
$str = mb_substr($str, 0, -1, 'UTF-8'); |
313
|
28 |
|
} |
314
|
|
|
|
315
|
787 |
|
return $str; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @inheritdoc |
320
|
|
|
*/ |
321
|
60 |
|
public function append($component) |
322
|
|
|
{ |
323
|
60 |
|
return $this->newCollectionInstance(array_merge( |
324
|
60 |
|
$this->validateComponent($component)->toArray(), |
325
|
60 |
|
$this->toArray() |
326
|
40 |
|
)); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|