Domain part validations

This commit is contained in:
Eduardo Gulias Davis
2020-08-01 23:03:58 +02:00
parent c8e7f01dfb
commit 990ca1c1e3
2 changed files with 13 additions and 8 deletions
+10 -5
View File
@@ -9,12 +9,12 @@ use Egulias\EmailValidator\Exception\ConsecutiveAt;
use Egulias\EmailValidator\Exception\CRLFAtTheEnd;
use Egulias\EmailValidator\Exception\CRNoLF;
use Egulias\EmailValidator\Exception\DomainHyphened;
use Egulias\EmailValidator\Exception\DotAtEnd;
use Egulias\EmailValidator\Exception\ExpectingATEXT;
use Egulias\EmailValidator\Exception\ExpectingDomainLiteralClose;
use Egulias\EmailValidator\Exception\ExpectingDTEXT;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\DomainHyphened as ReasonDomainHyphened;
use Egulias\EmailValidator\Result\Reason\DotAtEnd as ReasonDotAtEnd;
use Egulias\EmailValidator\Result\Reason\DotAtStart;
use Egulias\EmailValidator\Result\Reason\NoDomainPart as ReasonNoDomainPart;
use Egulias\EmailValidator\Result\Result;
@@ -62,10 +62,10 @@ class DomainPart extends Parser
$length = strlen($this->domainPart);
if ($prev['type'] === EmailLexer::S_DOT) {
throw new DotAtEnd();
return new InvalidEmail(new ReasonDotAtEnd(), $this->lexer->token['value']);
}
if ($prev['type'] === EmailLexer::S_HYPHEN) {
throw new DomainHyphened();
return new InvalidEmail(new ReasonDomainHyphened('Hypen found at the end of the domain'), $prev['value']);
}
if ($length > self::DOMAIN_MAX_LENGTH) {
$this->warnings[DomainTooLong::CODE] = new DomainTooLong();
@@ -205,7 +205,10 @@ class DomainPart extends Parser
}
$this->checkConsecutiveDots();
$this->checkDomainPartExceptions($prev);
$result = $this->checkDomainPartExceptions($prev);
if ($result->isInvalid()) {
return $result;
}
if ($this->hasBrackets()) {
$this->parseDomainLiteral();
@@ -376,13 +379,15 @@ class DomainPart extends Parser
}
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
throw new DomainHyphened();
return new InvalidEmail(new ReasonDomainHyphened('Hypen found near DOT'), $this->lexer->token['value']);
}
if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH
&& $this->lexer->isNextToken(EmailLexer::GENERIC)) {
throw new ExpectingATEXT();
}
return new ValidEmail();
}
/**
@@ -189,8 +189,8 @@ class RFCValidationTest extends TestCase
return [
[new InvalidEmail(new NoLocalPart(), "@"), '@example.co.uk'],
[new InvalidEmail(new ReasonNoDomainPart(), ''), 'example@'],
[new DomainHyphened(), 'example@example-.co.uk'],
[new DomainHyphened(), 'example@example-'],
[new InvalidEmail(new ReasonDomainHyphened('Hypen found near DOT'), '-'), 'example@example-.co.uk'],
[new InvalidEmail(new ReasonDomainHyphened('Hypen found at the end of the domain'), '-'), 'example@example-'],
[new ConsecutiveAt(), 'example@@example.co.uk'],
[new InvalidEmail(new ReasonConsecutiveDot(), '.'), 'example..example@example.co.uk'],
[new ConsecutiveDot(), 'example@example..co.uk'],
@@ -198,7 +198,7 @@ class RFCValidationTest extends TestCase
[new InvalidEmail(new ReasonDotAtStart(), '.'), '.example@localhost'],
[new InvalidEmail(new ReasonDotAtStart(), '.'), 'example@.localhost'],
[new InvalidEmail(new ReasonDomainHyphened('After AT'), '-'), 'example@-localhost'],
[new DotAtEnd(), 'example@localhost.'],
[new InvalidEmail(new ReasonDotAtEnd(), ''), 'example@localhost.'],
[new InvalidEmail(new ReasonDotAtEnd(), '.'), 'example.@example.co.uk'],
[new InvalidEmail(new ReasonUnclosedComment(), '('), '(example@localhost'],
[new InvalidEmail(new UnclosedQuotedString(), '"'), '"example@localhost'],