Skip to content
This repository has been archived by the owner on Mar 6, 2021. It is now read-only.

Commit

Permalink
0.1.0 release
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 21, 2017
1 parent 06f5b48 commit 7ce1136
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 55 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

All Notable changes to `League Uri Nostname parser` will be documented in this file

## 0.1.0 - 2016-10-17
## 0.1.0 - 2016-11-21

### Added

Expand Down
162 changes: 108 additions & 54 deletions src/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ final class Domain
*/
private $publicSuffix;

/**
* @var string|null
*/
private $registrableDomain;

/**
* @var string|null
*/
private $subDomain;

/**
* @var bool
*/
private $isValid;
private $isValid = false;

/**
* New instance.
Expand All @@ -52,8 +62,101 @@ final class Domain
public function __construct($domain = null, $publicSuffix = null, bool $isValid = false)
{
$this->domain = $domain;
$this->publicSuffix = null !== $this->domain ? $publicSuffix : null;
$this->isValid = null !== $this->publicSuffix ? $isValid : false;
$this->setPublicSuffix($publicSuffix);
$this->setValidity($isValid);
$this->setRegistrableDomain();
$this->setSubDomain();
}

/**
* Compute the public suffix part
*
* @param string|null $publicSuffix
*/
private function setPublicSuffix($publicSuffix)
{
if (null === $this->domain) {
return;
}

$this->publicSuffix = $publicSuffix;
}

/**
* Compute the domain validity
*
* @param bool $isValid
*/
private function setValidity(bool $isValid)
{
if (null === $this->publicSuffix) {
return;
}

$this->isValid = $isValid;
}

/**
* Compute the registrable domain part
*/
private function setRegistrableDomain()
{
if (!$this->hasRegistrableDomain()) {
return;
}

$countLabelsToRemove = count(explode('.', $this->publicSuffix)) + 1;
$domainLabels = explode('.', $this->domain);
$domain = implode('.', array_slice($domainLabels, count($domainLabels) - $countLabelsToRemove));
$this->registrableDomain = $this->normalize($domain);
}

/**
* Tells whether the domain has a registrable domain part.
*
* @return bool
*/
private function hasRegistrableDomain(): bool
{
return null !== $this->publicSuffix
&& strpos($this->domain, '.') > 0
&& $this->publicSuffix !== $this->domain;
}

/**
* Normalize the domain according to its representation.
*
* @param string $domain
*
* @return string
*/
private function normalize(string $domain): string
{
if (strpos($domain, 'xn--') !== false) {
return strtolower(idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46));
}

return idn_to_utf8($domain, 0, INTL_IDNA_VARIANT_UTS46);
}

/**
* Compute the sub domain part
*/
private function setSubDomain()
{
if (!$this->hasRegistrableDomain()) {
return;
}

$domainLabels = explode('.', $this->domain);
$countLabels = count($domainLabels);
$countLabelsToRemove = count(explode('.', $this->publicSuffix)) + 1;
if ($countLabels === $countLabelsToRemove) {
return;
}

$domain = implode('.', array_slice($domainLabels, 0, $countLabels - $countLabelsToRemove));
$this->subDomain = $this->normalize($domain);
}

/**
Expand Down Expand Up @@ -108,43 +211,7 @@ public function isValid(): bool
*/
public function getRegistrableDomain()
{
if (!$this->hasRegistrableDomain()) {
return null;
}

$countLabelsToRemove = count(explode('.', $this->publicSuffix)) + 1;
$domainLabels = explode('.', $this->domain);
$domain = implode('.', array_slice($domainLabels, count($domainLabels) - $countLabelsToRemove));

return $this->normalize($domain);
}

/**
* Tells whether the domain has a registrable domain part.
*
* @return bool
*/
private function hasRegistrableDomain(): bool
{
return null !== $this->publicSuffix
&& strpos($this->domain, '.') > 0
&& $this->publicSuffix !== $this->domain;
}

/**
* Normalize the domain according to its representation.
*
* @param string $domain
*
* @return string
*/
private function normalize(string $domain): string
{
if (strpos($domain, 'xn--') !== false) {
return strtolower(idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46));
}

return idn_to_utf8($domain, 0, INTL_IDNA_VARIANT_UTS46);
return $this->registrableDomain;
}

/**
Expand All @@ -159,19 +226,6 @@ private function normalize(string $domain): string
*/
public function getSubDomain()
{
if (!$this->hasRegistrableDomain()) {
return null;
}

$domainLabels = explode('.', $this->domain);
$countLabels = count($domainLabels);
$countLabelsToRemove = count(explode('.', $this->publicSuffix)) + 1;
if ($countLabels === $countLabelsToRemove) {
return null;
}

$domain = implode('.', array_slice($domainLabels, 0, $countLabels - $countLabelsToRemove));

return $this->normalize($domain);
return $this->subDomain;
}
}
1 change: 1 addition & 0 deletions src/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @author Ignace Nyamagana Butera <[email protected]>
* @license https://github.com/thephpleague/uri-hostname-parser/blob/master/LICENSE (MIT License)
* @version 1.0.0
* @link https://github.com/thephpleague/uri-hostname-parser
*
* For the full copyright and license information, please view the LICENSE
Expand Down

0 comments on commit 7ce1136

Please sign in to comment.