Skip to content

Commit e946a42

Browse files
committed
fix: Get rid of non standar characters for a domain.
1 parent b3b6be3 commit e946a42

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

src/Host/Host.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function tld(?string $tld = null): string|self
4747
$root = str_replace(strval($this->tld), '', strval($this->host));
4848
$root = explode('.', trim(strval($root), '.'));
4949

50-
$this->domain = end($root) . '.' . $this->tld;
50+
$this->domain(end($root) . '.' . $this->tld);
5151

5252
return $this;
5353
}
@@ -63,6 +63,11 @@ public function domain(?string $domain = null): string|self
6363
return $this;
6464
}
6565

66+
public function isValid(): bool
67+
{
68+
return !empty($this->tld());
69+
}
70+
6671
public function toString(): string
6772
{
6873
return strval($this->host);

src/Parse/PublicSuffixListParser.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ public static function parse(string $publicSuffixListPath): array
1515
unset($list[0]);
1616
unset($list[count($list)]);
1717

18-
return array_values($list);
18+
$list = array_values($list);
19+
20+
$fn = function (string $tld) {
21+
return boolval(
22+
preg_match(
23+
"/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i",
24+
$tld
25+
)
26+
);
27+
};
28+
29+
foreach ($list as $key => $tld) {
30+
if ($fn($tld) === false) {
31+
unset($list[$key]);
32+
}
33+
}
34+
35+
return $list;
1936
}
2037
}

src/Validator.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ public function validate(string $host): Host
2525
{
2626
$host = new Host($host);
2727

28-
$parts = $host->exploded();
29-
$host->tld($this->getTld($parts));
28+
$tld = $this->getTld($host->exploded());
29+
30+
if ($this->verifyTld($tld)) {
31+
$host->tld($tld);
32+
}
3033

3134
return $host;
3235
}
@@ -54,11 +57,22 @@ protected function getTld(array $parts, ?string $tld = null): string
5457
unset($parts[count($parts) - 1]);
5558

5659
foreach ($this->publicSuffixList as $key => $item) {
57-
if (strpos($item, $current) !== false) {
60+
if (preg_match('/\b' . preg_quote($current) . '\b/i', $item) === 1) {
5861
return $this->getTld($parts, $current);
5962
}
6063
}
6164

6265
return strval($tld);
6366
}
67+
68+
protected function verifyTld(string $tld): bool
69+
{
70+
foreach ($this->publicSuffixList as $key => $item) {
71+
if (preg_match('/\b' . preg_quote($tld) . '\b/i', $item) === 1) {
72+
return true;
73+
}
74+
}
75+
76+
return false;
77+
}
6478
}

tests/Unit/FactoryTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66

77
test('factory make', function () {
8-
$validator = Factory::make(
9-
getPublicSuffixListPath()
10-
);
8+
$validator = getInstance();
119

1210
expect($validator)->toBeInstanceOf(Validator::class);
1311
});

tests/Unit/ValidatorTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@
88
$host = $validator->validate($url);
99

1010
it('is instance of host', fn () => expect($host)->toBeInstanceOf(Host::class));
11+
it('is a valid domain', fn () => expect($host->isValid())->toBeTrue());
1112
it("is same as $url", fn () => expect($host->original())->toBe($url));
1213
it('com.mx is the TLD', fn () => expect($host->tld())->toBe('com.mx'));
1314
it('adro.com.mx is the domain', fn () => expect($host->domain())->toBe('adro.com.mx'));
1415
it('www.adro.com.mx is the host', fn () => expect($host->toString())->toBe('www.adro.com.mx'));
1516
});
17+
18+
describe('Validator fail', function () {
19+
$validator = getInstance();
20+
$url = 'https://adro.is.a.rocker.and/he-rocks?or=not';
21+
$host = $validator->validate($url);
22+
23+
it('is not a valid domain', fn () => expect($host->isValid())->toBeFalse());
24+
});

0 commit comments

Comments
 (0)