Skip to content

Commit 4687932

Browse files
authored
Merge pull request #3 from rickycheers/fix-domain-parsing
[2.x] Correctly obtain domain when tld substring is in it
2 parents e8f3128 + 60634c8 commit 4687932

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Host/Host.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ public function tld(?string $tld = null): string|self
3838

3939
$this->tld = $tld;
4040

41-
$root = str_replace(strval($this->tld), '', strval($this->host));
41+
$escaped = str_replace('.', '\.', '.' . strval($this->tld));
42+
43+
/** @var string $root */
44+
$root = preg_replace("/$escaped$/", '', strval($this->host));
4245

4346
if (!validate_domain_root(trim($root, '.'))) {
4447
$this->isValid = false;

tests/Unit/ValidatorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,19 @@
6262
it('is not empty domain', fn () => expect($host->domain())->toBe('adro.com'));
6363
it("is same as $url", fn () => expect($host->original())->toBe($url));
6464
});
65+
66+
describe('Root domain is correctly obtained when the tld substring is contained in it', function () {
67+
$validator = getInstance();
68+
$url = 'a.b.c.compass.com';
69+
$host = $validator->validate($url);
70+
71+
it("$url is valid domain", fn () => expect($host->isValid())->toBeTrue());
72+
it("from $url - compass.com is the domain", fn () => expect($host->domain())->toBe('compass.com'));
73+
74+
$validator = getInstance();
75+
$url = 'compass.com';
76+
$host = $validator->validate($url);
77+
78+
it("$url is valid domain", fn () => expect($host->isValid())->toBeTrue());
79+
it("from $url - compass.com is the domain", fn () => expect($host->domain())->toBe('compass.com'));
80+
});

0 commit comments

Comments
 (0)