diff --git a/.gitignore b/.gitignore index 8a34ea0..57de874 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/ +composer.lock .phpunit.result.cache .php_cs.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 8fd4b96..ce31fe1 100644 --- a/composer.json +++ b/composer.json @@ -10,9 +10,9 @@ } ], "require": { - "ext-curl": "*", + "php": ">=7.1", "egulias/email-validator": "^2.1", - "layershifter/tld-extract": "^2.0" + "utopia-php/domains": "^1.1" }, "autoload": { "psr-4": { @@ -25,10 +25,9 @@ } }, "require-dev": { - "phpunit/phpunit": "^8.4", - "symfony/var-dumper": "^4.3", - "phpstan/phpstan": "^0.11.19", - "friendsofphp/php-cs-fixer": "^2.16" + "phpstan/phpstan": "^0.12.78", + "friendsofphp/php-cs-fixer": "^2.16", + "symfony/phpunit-bridge": "^5.2" }, "scripts": { "lint": "./vendor/bin/php-cs-fixer fix --diff --dry-run", @@ -39,6 +38,7 @@ "@composer run lint", "@composer run phpstan", "@composer run phpunit" - ] + ], + "import": "@php ./vendor/utopia-php/domains/data/import.php" } } diff --git a/src/Domain.php b/src/Domain.php index fafc67c..e000f76 100644 --- a/src/Domain.php +++ b/src/Domain.php @@ -2,25 +2,20 @@ namespace Devnix\Mailcheck; -use LayerShifter\TLDExtract\Extract; -use LayerShifter\TLDExtract\ResultInterface; +use Utopia\Domains\Domain as DomainParser; class Domain { - /** - * @var ResultInterface - */ protected $domain; public function __construct(string $domain) { - $extract = new Extract(null, null, Extract::MODE_ALLOW_ICANN | Extract::MODE_ALLOW_PRIVATE | Extract::MODE_ALLOW_NOT_EXISTING_SUFFIXES); - $this->domain = $extract->parse($domain); + $this->domain = new DomainParser($domain); } public function __toString() { - return $this->domain->getFullHost(); + return $this->domain->get(); } /** @@ -31,12 +26,20 @@ public function getSuffix() return $this->domain->getSuffix(); } + /** + * @return string + */ + public function getTld() + { + return $this->domain->getTld(); + } + /** * @return string */ public function getSubdomain() { - return $this->domain->getSubdomain(); + return $this->domain->getSub(); } /** @@ -44,7 +47,7 @@ public function getSubdomain() */ public function getHostname() { - return $this->domain->getHostname(); + return $this->domain->getName(); } /** @@ -54,11 +57,11 @@ public function getDomainWithoutSuffix() { $result = ''; - if (!empty($this->domain->getSubdomain())) { - $result .= $this->domain->getSubdomain().'.'; + if ('' !== $this->getSubdomain()) { + $result .= $this->getSubdomain().'.'; } - $result .= $this->domain->getHostname(); + $result .= $this->getHostname(); return $result; } diff --git a/src/Mailcheck.php b/src/Mailcheck.php index bc98e51..4ddb370 100644 --- a/src/Mailcheck.php +++ b/src/Mailcheck.php @@ -65,12 +65,14 @@ public function suggest(string $email, int $limit = 5, int $threshold = 3) // If not, try to suggest a TLD $parsedDomain = $parsedEmail->getDomain(); + $suffix = $parsedDomain->getSuffix() ?: $parsedDomain->getTld(); + // If there is not any suffix don't return suggestions - if (empty($parsedDomain->getSuffix())) { + if ('' === $suffix) { return []; } - $suggestionTld = $this->findSuggestions($parsedDomain->getSuffix(), $this->getDictionary()->getTlds(), $threshold); + $suggestionTld = $this->findSuggestions($suffix, $this->getDictionary()->getTlds(), $threshold); if (false !== $suggestionTld) { foreach ($suggestionTld as $tld) { diff --git a/tests/DomainTest.php b/tests/DomainTest.php index a13df17..3115187 100644 --- a/tests/DomainTest.php +++ b/tests/DomainTest.php @@ -7,6 +7,24 @@ class DomainTest extends TestCase { + public function testToString() + { + $domain = new Domain('example.com'); + $this->assertEquals('example.com', (string) $domain); + + $domain = new Domain('www.example.com'); + $this->assertEquals('www.example.com', (string) $domain); + + $domain = new Domain('example.co.uk'); + $this->assertEquals('example.co.uk', (string) $domain); + + $domain = new Domain('www.example.co.uk'); + $this->assertEquals('www.example.co.uk', (string) $domain); + + $domain = new Domain('eXaMPle.Com'); + $this->assertEquals('example.com', (string) $domain); + } + public function testGetSuffix() { $domain = new Domain('example.com'); @@ -22,6 +40,21 @@ public function testGetSuffix() $this->assertEquals('co.uk', $domain->getSuffix()); } + public function testGetTld() + { + $domain = new Domain('example.com'); + $this->assertEquals('com', $domain->getTld()); + + $domain = new Domain('www.example.com'); + $this->assertEquals('com', $domain->getTld()); + + $domain = new Domain('example.co.uk'); + $this->assertEquals('uk', $domain->getTld()); + + $domain = new Domain('www.example.co.uk'); + $this->assertEquals('uk', $domain->getTld()); + } + public function testGetSubdomain() { $domain = new Domain('example.com');