Skip to content

Commit

Permalink
Add support for wildcard and exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Suven-p committed Apr 12, 2023
1 parent 7ea655f commit 741bd5e
Showing 1 changed file with 38 additions and 5 deletions.
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

0 comments on commit 741bd5e

Please sign in to comment.