Skip to content

Commit

Permalink
Merge branch 'hotmeteor-swap-parser'
Browse files Browse the repository at this point in the history
  • Loading branch information
devnix committed Feb 23, 2021
2 parents a04b2c3 + 00346f1 commit 63bafa8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor/
composer.lock
.phpunit.result.cache
.php_cs.cache
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand All @@ -39,6 +38,7 @@
"@composer run lint",
"@composer run phpstan",
"@composer run phpunit"
]
],
"import": "@php ./vendor/utopia-php/domains/data/import.php"
}
}
29 changes: 16 additions & 13 deletions src/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -31,20 +26,28 @@ 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();
}

/**
* @return string
*/
public function getHostname()
{
return $this->domain->getHostname();
return $this->domain->getName();
}

/**
Expand All @@ -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;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Mailcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions tests/DomainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit 63bafa8

Please sign in to comment.