A domain name value object. Works with IDN and punycode syntax, validates and easily retrieves the level, zone and TLD (top-level domain).
<?php
use \Rincler\Domain\Domain;
$domain = new Domain('sub.example.com');
echo $domain; // sub.example.com
echo $domain->asIdn(); // sub.example.com
echo $domain->asPunycode(); // sub.example.com
echo $domain->getZone(); // example.com
echo $domain->getWithoutZone(); // sub
echo $domain->getTld(); // com
echo $domain->getWithoutTld(); // sub.example
echo $domain->getLevel(); // 3
echo $domain->getLabels(); // [new Domain('com'), new Domain('example'), new Domain('sub')]
echo $domain->getLabel(2); // example
echo $domain->sliceToLevel(2); // example.com
$domain = new Domain('пример.рф');
echo $domain->asIdn(); // пример.рф
echo $domain->asPunycode(); // xn--e1afmkfd.xn--p1ai
$domain = new Domain('xn--e1afmkfd.xn--p1ai');
echo $domain->asIdn(); // пример.рф
echo $domain->asPunycode(); // xn--e1afmkfd.xn--p1ai
var_dump($domain->equals(new Domain('xn--e1afmkfd.xn--p1ai'))) // true
var_dump($domain->equals(new Domain('пример.рф'))) // true
var_dump($domain->equals(new Domain('суб.пример.рф'))) // false
var_dump(Domain::isValid('example.com')) // true
var_dump(Domain::isValid('пример.рф')) // true
var_dump(Domain::isValid('exam_ple.com')) // false
var_dump(Domain::isValid('.example.com')) // false
composer require rincler/domain
- static
isValid(): bool
- Returnstrue
if the domain is valid, returnsfalse
otherwise __constructor(string $domain)
- The constructor validates the domain (throwsInvalidDomainException
if the domain is not valid) and creates the value objectasIdn(): string
- Returns the domain in IDN syntaxasPunycode(): string
- Returns the domain in Punycode syntaxequals(Domain $domain): bool
- Returnstrue
if the current domain equals the specified domain, returnsfalse
otherwisegetLevel(): int
- Returns the number of domain levelsgetZone(): Domain
- Returns the domain zonegetWithoutZone(): Domain
- Returns the domain without a zonegetTld(?TldRetrievalMode $mode = null): Domain
- Returns the top-level domaingetWithoutTld(?TldRetrievalMode $mode = null): Domain
- Returns the domain without a top-level domaingetLabels(): array
- Returns an array of the domain labelsgetLabel(int $level): array
- Returns the label of the domain with number $levelsliceToLevel(int $level): array
- Returns the domain to level with number $level__toString(): string
- An equivalent for theasIdn
method
You can use custom TLDs, e.g.: net.ru
or org.ru
, using the setCustomTlds
method:
<?php
use \Rincler\Domain\Domain;
$domain = new Domain('example.net.ru');
echo $domain->getTld(); // ru
Domain::setCustomTlds(static function () {
return ['net.ru', 'org.ru'];
});
echo $domain->getTld(); // net.ru
You can specify the TLD retriever mode:
TldRetrievalMode::TRUE()
- the mode to retrieving only true TLDTldRetrievalMode::CUSTOM()
- the mode to retrieving only custom TLDTldRetrievalMode::MIX()
- the mode to retrieving custom TLD and true TLD if custom is not found (this mode is used by default)
<?php
use \Rincler\Domain\Domain;
use \Rincler\Domain\TldRetrievalMode
$domain = new Domain('example.net.ru');
Domain::setCustomTlds(static function () {
return ['net.ru', 'org.ru'];
});
echo $domain->getTld(TldRetrievalMode::TRUE()); // ru
echo $domain->getTld(TldRetrievalMode::CUSTOM()); // net.ru
echo $domain->getTld(TldRetrievalMode::MIX()); // net.ru
$domain = new Domain('example.ru');
echo $domain->getTld(TldRetrievalMode::TRUE()); // ru
echo $domain->getTld(TldRetrievalMode::CUSTOM()); // <null>
echo $domain->getTld(TldRetrievalMode::MIX()); // ru
Also, you can specify the TLD retriever mode globally:
<?php
use \Rincler\Domain\Domain;
use \Rincler\Domain\TldRetrievalMode
$domain = new Domain('example.net.ru');
Domain::setDefaultTldRetrievalMode(TldRetrievalMode::TRUE());
Domain::setCustomTlds(static function () {
return ['net.ru', 'org.ru'];
});
echo $domain->getTld(); // ru
// You can still pass the mode as argument:
echo $domain->getTld(TldRetrievalMode::CUSTOM()); // net.ru
Validation of domain in intl extension fixed in 7.3.0. See http://bugs.php.net/76829
This library is released under the MIT license.