Skip to content

Commit 8ae5ef6

Browse files
committed
Allow resolving absolute host against IANA list
1 parent 2d73471 commit 8ae5ef6

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

src/TopLevelDomains.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Stringable;
1212

1313
use function count;
14-
use function in_array;
1514
use function preg_match;
1615
use function trim;
1716

@@ -172,7 +171,7 @@ public function resolve($host): ResolvedDomainName
172171
{
173172
try {
174173
$domain = $this->validateDomain($host);
175-
if ($this->containsTopLevelDomain($domain)) {
174+
if ($this->containsTopLevelDomain($domain->withoutRootLabel())) {
176175
return ResolvedDomain::fromIANA($domain);
177176
}
178177
return ResolvedDomain::fromUnknown($domain);
@@ -199,8 +198,7 @@ private function validateDomain(int|DomainNameProvider|Host|string|Stringable|nu
199198
$domain = Domain::fromIDNA2008($domain);
200199
}
201200

202-
$label = $domain->label(0);
203-
if (in_array($label, [null, ''], true)) {
201+
if (null === $domain->label(0)) {
204202
throw UnableToResolveDomain::dueToUnresolvableDomain($domain);
205203
}
206204

@@ -212,13 +210,10 @@ private function containsTopLevelDomain(DomainName $domain): bool
212210
return isset($this->records[$domain->toAscii()->label(0)]);
213211
}
214212

215-
/**
216-
* @param int|DomainNameProvider|Host|string|Stringable|null $host a domain in a type that can be converted into a DomainInterface instance
217-
*/
218-
public function getIANADomain($host): ResolvedDomainName
213+
public function getIANADomain(DomainNameProvider|Host|Stringable|string|int|null $host): ResolvedDomainName
219214
{
220215
$domain = $this->validateDomain($host);
221-
if (!$this->containsTopLevelDomain($domain)) {
216+
if (!$this->containsTopLevelDomain($domain->withoutRootLabel())) {
222217
throw UnableToResolveDomain::dueToMissingSuffix($domain, 'IANA');
223218
}
224219

src/TopLevelDomainsTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PHPUnit\Framework\Attributes\DataProvider;
1010
use PHPUnit\Framework\TestCase;
1111
use Stringable;
12-
use TypeError;
1312

1413
use function dirname;
1514

@@ -187,13 +186,6 @@ public function __toString(): string
187186
];
188187
}
189188

190-
public function testTopLevelDomainThrowsTypeError(): void
191-
{
192-
$this->expectException(TypeError::class);
193-
194-
self::$topLevelDomains->getIANADomain(new DateTimeImmutable()); /* @phpstan-ignore-line */
195-
}
196-
197189
public function testTopLevelDomainWithInvalidDomain(): void
198190
{
199191
$this->expectException(SyntaxError::class);
@@ -212,8 +204,8 @@ public function testResolveWithAbsoluteDomainName(): void
212204
{
213205
$result = self::$topLevelDomains->resolve('example.com.');
214206
self::assertSame('example.com.', $result->value());
215-
self::assertFalse($result->suffix()->isIANA());
216-
self::assertNull($result->suffix()->value());
207+
self::assertTrue($result->suffix()->isIANA());
208+
self::assertSame('com', $result->suffix()->value());
217209
}
218210

219211
public function testTopLevelDomainWithUnResolvableDomain(): void
@@ -223,6 +215,13 @@ public function testTopLevelDomainWithUnResolvableDomain(): void
223215
self::$topLevelDomains->getIANADomain('localhost');
224216
}
225217

218+
public function testTopLevelDomainWithUnResolvableDomain2(): void
219+
{
220+
$this->expectException(UnableToResolveDomain::class);
221+
222+
self::$topLevelDomains->getIANADomain('localhost.');
223+
}
224+
226225
public function testResolveWithUnResolvableDomain(): void
227226
{
228227
$result = self::$topLevelDomains->resolve('localhost');

0 commit comments

Comments
 (0)