forked from jeremykendall/php-domain-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolvedDomain.php
More file actions
239 lines (193 loc) · 6.23 KB
/
Copy pathResolvedDomain.php
File metadata and controls
239 lines (193 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
declare(strict_types=1);
namespace Pdp;
use function count;
final class ResolvedDomain implements ResolvedDomainName
{
private DomainName $domain;
private EffectiveTopLevelDomain $suffix;
private DomainName $secondLevelDomain;
private DomainName $registrableDomain;
private DomainName $subDomain;
private function __construct(DomainName $domain, EffectiveTopLevelDomain $suffix)
{
$this->domain = $domain;
$this->suffix = $suffix;
$this->validateState();
}
/**
* @param mixed $domain the domain to be resolved
*/
public static function fromICANN($domain, int $suffixLength): self
{
$domain = self::setDomainName($domain);
return new self($domain, Suffix::fromICANN($domain->slice(0, $suffixLength)));
}
/**
* @param mixed $domain the domain to be resolved
*/
public static function fromPrivate($domain, int $suffixLength): self
{
$domain = self::setDomainName($domain);
return new self($domain, Suffix::fromPrivate($domain->slice(0, $suffixLength)));
}
/**
* @param mixed $domain the domain to be resolved
*/
public static function fromIANA($domain): self
{
$domain = self::setDomainName($domain);
return new self($domain, Suffix::fromIANA($domain->label(0)));
}
/**
* @param mixed $domain the domain to be resolved
*/
public static function fromUnknown($domain, int $suffixLength = 0): self
{
$domain = self::setDomainName($domain);
return new self($domain, Suffix::fromUnknown($domain->slice(0, $suffixLength)));
}
/**
* @param array{domain:DomainName, suffix:EffectiveTopLevelDomain} $properties
*/
public static function __set_state(array $properties): self
{
return new self($properties['domain'], $properties['suffix']);
}
/**
* @param mixed $domain The domain to be resolved
*/
private static function setDomainName($domain): DomainName
{
if ($domain instanceof DomainNameProvider) {
return $domain->domain();
}
if ($domain instanceof DomainName) {
return $domain;
}
return Domain::fromIDNA2008($domain);
}
/**
* Make sure the Value Object is always in a valid state.
*
* @throws UnableToResolveDomain If the suffix can not be attached to the domain
*/
private function validateState(): void
{
$suffixValue = $this->suffix->value();
if (null === $suffixValue) {
$nullDomain = $this->domain->clear();
$this->registrableDomain = $nullDomain;
$this->secondLevelDomain = $nullDomain;
$this->subDomain = $nullDomain;
return;
}
if (2 > count($this->domain)) {
throw UnableToResolveDomain::dueToUnresolvableDomain($this->domain);
}
if ($this->domain->value() === $suffixValue) {
throw UnableToResolveDomain::dueToIdenticalValue($this->domain);
}
$length = count($this->suffix);
$this->registrableDomain = $this->domain->slice(0, $length + 1);
$this->secondLevelDomain = $this->domain->slice($length, 1);
$this->subDomain = $this->domain->slice($length + 1);
}
public function count(): int
{
return count($this->domain);
}
public function domain(): DomainName
{
return $this->domain;
}
public function jsonSerialize(): ?string
{
return $this->domain->jsonSerialize();
}
public function value(): ?string
{
return $this->domain->value();
}
public function toString(): string
{
return $this->domain->toString();
}
public function registrableDomain(): DomainName
{
return $this->registrableDomain;
}
public function secondLevelDomain(): DomainName
{
return $this->secondLevelDomain;
}
public function subDomain(): DomainName
{
return $this->subDomain;
}
public function suffix(): EffectiveTopLevelDomain
{
return $this->suffix;
}
/**
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*/
public function toAscii(): self
{
return new self($this->domain->toAscii(), $this->suffix->toAscii());
}
/**
* @psalm-suppress MoreSpecificReturnType
* @psalm-suppress LessSpecificReturnStatement
*/
public function toUnicode(): self
{
return new self($this->domain->toUnicode(), $this->suffix->toUnicode());
}
/**
* @param mixed $suffix the suffix
*/
public function withSuffix($suffix): self
{
if (!$suffix instanceof EffectiveTopLevelDomain) {
$suffix = Suffix::fromUnknown($suffix);
}
return new self(
$this->domain->slice(count($this->suffix))->append($suffix),
$suffix->normalize($this->domain)
);
}
/**
* @param mixed $subDomain the sub domain
*/
public function withSubDomain($subDomain): self
{
if (null === $this->suffix->value()) {
throw UnableToResolveDomain::dueToMissingRegistrableDomain($this->domain);
}
$subDomain = $this->domain->clear()->append(self::setDomainName($subDomain));
if ($this->subDomain->value() === $subDomain->value()) {
return $this;
}
return new self($this->registrableDomain->prepend($subDomain), $this->suffix);
}
/**
* @param mixed $label the second level domain
*/
public function withSecondLevelDomain($label): self
{
if (null === $this->suffix->value()) {
throw UnableToResolveDomain::dueToMissingRegistrableDomain($this->domain);
}
$label = self::setDomainName($label);
if (1 !== count($label)) {
throw UnableToResolveDomain::dueToInvalidSecondLevelDomain($label);
}
$newRegistrableDomain = $this->registrableDomain->withoutLabel(-1)->prepend($label);
if ($newRegistrableDomain->value() === $this->registrableDomain->value()) {
return $this;
}
return new self($newRegistrableDomain->prepend($this->subDomain), $this->suffix);
}
}