Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions src/Domains/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Domain
*/
protected $sub = '';

/**
* PSL rule matching suffix
*
* @var string
*/
protected $rule = '';

/**
* Domain Parts
*
Expand Down Expand Up @@ -105,11 +112,29 @@ public function getSuffix(): string
return $this->suffix;
}

for ($i = 3; $i > 0; $i--) {
$joined = \implode('.', \array_slice($this->parts, $i * -1));
for ($i = 0; $i < count($this->parts); $i++) {
$joined = \implode('.', \array_slice($this->parts, $i));
$next = \implode('.', \array_slice($this->parts, $i + 1));
$exception = '!'.$joined;
$wildcard = '*.'.$next;

if (\array_key_exists($exception, self::$list)) {
$this->suffix = $next;
$this->rule = $exception;

return $next;
}

if (\array_key_exists($joined, self::$list)) {
$this->suffix = $joined;
$this->rule = $joined;

return $joined;
}

if (\array_key_exists($wildcard, self::$list)) {
$this->suffix = $joined;
$this->rule = $wildcard;

return $joined;
}
Expand All @@ -118,6 +143,14 @@ public function getSuffix(): string
return '';
}

public function getRule(): string
{
if (! $this->rule) {
$this->getSuffix();
}
return $this->rule;
}

/**
* Returns registerable domain name
*/
Expand Down Expand Up @@ -176,7 +209,7 @@ public function getSub(): string
*/
public function isKnown(): bool
{
if (\array_key_exists($this->getSuffix(), self::$list)) {
if (\array_key_exists($this->getRule(), self::$list)) {
return true;
}

Expand All @@ -188,7 +221,7 @@ public function isKnown(): bool
*/
public function isICANN(): bool
{
if (isset(self::$list[$this->getSuffix()]) && self::$list[$this->getSuffix()]['type'] === 'ICANN') {
if (isset(self::$list[$this->getRule()]) && self::$list[$this->getRule()]['type'] === 'ICANN') {
return true;
}

Expand All @@ -200,7 +233,7 @@ public function isICANN(): bool
*/
public function isPrivate(): bool
{
if (isset(self::$list[$this->getSuffix()]) && self::$list[$this->getSuffix()]['type'] === 'PRIVATE') {
if (isset(self::$list[$this->getRule()]) && self::$list[$this->getRule()]['type'] === 'PRIVATE') {
return true;
}

Expand Down
64 changes: 64 additions & 0 deletions tests/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,68 @@ public function testHTTPSException2(): void

new Domain('https://facbook.com');
}

public function testExampleExampleCk(): void
{
$domain = new Domain('example.example.ck');

$this->assertEquals('example.example.ck', $domain->get());
$this->assertEquals('ck', $domain->getTLD());
$this->assertEquals('example.ck', $domain->getSuffix());
$this->assertEquals('example.example.ck', $domain->getRegisterable());
$this->assertEquals('example', $domain->getName());
$this->assertEquals('', $domain->getSub());
$this->assertEquals(true, $domain->isKnown());
$this->assertEquals(true, $domain->isICANN());
$this->assertEquals(false, $domain->isPrivate());
$this->assertEquals(false, $domain->isTest());
}

public function testSubSubExampleExampleCk(): void
{
$domain = new Domain('subsub.demo.example.example.ck');

$this->assertEquals('subsub.demo.example.example.ck', $domain->get());
$this->assertEquals('ck', $domain->getTLD());
$this->assertEquals('example.ck', $domain->getSuffix());
$this->assertEquals('example.example.ck', $domain->getRegisterable());
$this->assertEquals('example', $domain->getName());
$this->assertEquals('subsub.demo', $domain->getSub());
$this->assertEquals(true, $domain->isKnown());
$this->assertEquals(true, $domain->isICANN());
$this->assertEquals(false, $domain->isPrivate());
$this->assertEquals(false, $domain->isTest());
}

public function testWwwCk(): void
{
$domain = new Domain('www.ck');

$this->assertEquals('www.ck', $domain->get());
$this->assertEquals('ck', $domain->getTLD());
$this->assertEquals('ck', $domain->getSuffix());
$this->assertEquals('www.ck', $domain->getRegisterable());
$this->assertEquals('www', $domain->getName());
$this->assertEquals('', $domain->getSub());
$this->assertEquals(true, $domain->isKnown());
$this->assertEquals(true, $domain->isICANN());
$this->assertEquals(false, $domain->isPrivate());
$this->assertEquals(false, $domain->isTest());
}

public function testSubSubWwwCk(): void
{
$domain = new Domain('subsub.demo.www.ck');

$this->assertEquals('subsub.demo.www.ck', $domain->get());
$this->assertEquals('ck', $domain->getTLD());
$this->assertEquals('ck', $domain->getSuffix());
$this->assertEquals('www.ck', $domain->getRegisterable());
$this->assertEquals('www', $domain->getName());
$this->assertEquals('subsub.demo', $domain->getSub());
$this->assertEquals(true, $domain->isKnown());
$this->assertEquals(true, $domain->isICANN());
$this->assertEquals(false, $domain->isPrivate());
$this->assertEquals(false, $domain->isTest());
}
}