|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* PHP Domain Parser: Public Suffix List based URL parsing. |
|
7
|
|
|
* |
|
8
|
|
|
* @link http://github.com/jeremykendall/php-domain-parser for the canonical source repository |
|
9
|
|
|
* |
|
10
|
|
|
* @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall) |
|
11
|
|
|
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Pdp\Uri\Url; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Represents the host portion of a Url. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Host |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string Subdomain |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $subdomain; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string Registrable domain |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $registrableDomain; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string Public suffix |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $publicSuffix; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string host Entire host part |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $host = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Public constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|null $subdomain Subdomain portion of host |
|
45
|
|
|
* @param string|null $registrableDomain Registrable domain portion of host |
|
46
|
|
|
* @param string|null $publicSuffix Public suffix portion of host |
|
47
|
|
|
* @param string $host OPTIONAL Entire host part |
|
48
|
|
|
*/ |
|
49
|
24 |
|
public function __construct($subdomain, $registrableDomain, $publicSuffix, $host = '') |
|
50
|
|
|
{ |
|
51
|
24 |
|
$this->subdomain = $subdomain; |
|
52
|
24 |
|
$this->registrableDomain = $registrableDomain; |
|
53
|
24 |
|
$this->publicSuffix = $publicSuffix; |
|
54
|
24 |
|
$this->host = $host; |
|
55
|
24 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get Subdomain. |
|
59
|
|
|
* |
|
60
|
|
|
* @return string|null |
|
61
|
|
|
*/ |
|
62
|
7 |
|
public function getSubdomain() |
|
63
|
|
|
{ |
|
64
|
7 |
|
return $this->subdomain; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string|null |
|
69
|
|
|
*/ |
|
70
|
7 |
|
public function getRegistrableDomain() |
|
71
|
|
|
{ |
|
72
|
7 |
|
return $this->registrableDomain; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get Public suffix. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string|null |
|
79
|
|
|
*/ |
|
80
|
8 |
|
public function getPublicSuffix() |
|
81
|
|
|
{ |
|
82
|
8 |
|
return $this->publicSuffix; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get Entire host part. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
6 |
|
public function getHost() |
|
91
|
|
|
{ |
|
92
|
6 |
|
return $this->host; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get string representation of host. |
|
97
|
|
|
* |
|
98
|
|
|
* @return string String representation of host |
|
99
|
|
|
*/ |
|
100
|
15 |
|
public function __toString() |
|
101
|
|
|
{ |
|
102
|
15 |
|
if ($this->host) { |
|
103
|
14 |
|
return $this->host; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// retain only the elements that are not empty |
|
107
|
1 |
|
$str = \array_filter( |
|
108
|
1 |
|
[$this->subdomain, $this->registrableDomain], |
|
109
|
1 |
|
'\strlen' |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
1 |
|
return \implode('.', $str); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get array representation of host. |
|
117
|
|
|
* |
|
118
|
|
|
* @return array Array representation of host |
|
119
|
|
|
*/ |
|
120
|
6 |
|
public function toArray(): array |
|
121
|
|
|
{ |
|
122
|
|
|
return [ |
|
123
|
6 |
|
'subdomain' => $this->getSubdomain(), |
|
124
|
6 |
|
'registrableDomain' => $this->getRegistrableDomain(), |
|
125
|
6 |
|
'publicSuffix' => $this->getPublicSuffix(), |
|
126
|
6 |
|
'host' => $this->getHost(), |
|
127
|
|
|
]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|