Fix Space after end of domain. #217

This commit is contained in:
Eduardo Gulias Davis
2020-09-26 16:30:00 +02:00
parent e272a25e47
commit ce879d7d10
2 changed files with 28 additions and 6 deletions
+24 -4
View File
@@ -21,6 +21,7 @@ use Egulias\EmailValidator\Warning\LabelTooLong;
use Egulias\EmailValidator\Warning\TLD;
use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser;
use Egulias\EmailValidator\Result\Reason\CommaInDomain;
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd;
class DomainPart extends Parser
{
@@ -45,20 +46,35 @@ class DomainPart extends Parser
return $domain;
}
$prev = $this->lexer->getPrevious();
$length = strlen($this->domainPart);
$end = $this->checkEndOfDomain();
if ($end->isInvalid()) {
return $end;
}
if ($length > self::DOMAIN_MAX_LENGTH) {
$this->warnings[DomainTooLong::CODE] = new DomainTooLong();
}
return new ValidEmail();
}
private function checkEndOfDomain() : Result
{
$prev = $this->lexer->getPrevious();
if ($prev['type'] === EmailLexer::S_DOT) {
return new InvalidEmail(new DotAtEnd(), $this->lexer->token['value']);
}
if ($prev['type'] === EmailLexer::S_HYPHEN) {
return new InvalidEmail(new DomainHyphened('Hypen found at the end of the domain'), $prev['value']);
}
if ($length > self::DOMAIN_MAX_LENGTH) {
$this->warnings[DomainTooLong::CODE] = new DomainTooLong();
}
if ($this->lexer->token['type'] === EmailLexer::S_SP) {
return new InvalidEmail(new CRLFAtTheEnd(), $prev['value']);
}
return new ValidEmail();
}
private function performDomainStartChecks() : Result
@@ -167,6 +183,10 @@ class DomainPart extends Parser
$domain .= $this->lexer->token['value'];
$this->lexer->moveNext();
if ($this->lexer->token['type'] === EmailLexer::S_SP) {
return new InvalidEmail(new CharNotAllowed(), $this->lexer->token['type']);
}
} while (null !== $this->lexer->token['type']);
$this->domainPart = $domain;
@@ -190,6 +190,9 @@ class RFCValidationTest extends TestCase
['username@examp,le.com'],
['test@ '],
['validipv4@[127.\0.0.0]'],
['test@example.com []'],
['test@example.com. []'],
['test@test. example.com'],
];
}
@@ -207,8 +210,7 @@ class RFCValidationTest extends TestCase
return [
[new InvalidEmail(new NoLocalPart(), "@"), '@example.co.uk'],
[new InvalidEmail(new NoDomainPart(), ''), 'example@'],
[new InvalidEmail(new DomainHyphened('Hypen found near DOT'), '-'), 'example@example-.co.uk'],
[new InvalidEmail(new CRNoLF(), "\r"), "example@example\r.com"],
[new InvalidEmail(new DomainHyphened('Hypen found near DOT'), '-'), 'example@example-.co.uk'], [new InvalidEmail(new CRNoLF(), "\r"), "example@example\r.com"],
[new InvalidEmail(new DomainHyphened('Hypen found at the end of the domain'), '-'), 'example@example-'],
[new InvalidEmail(new ConsecutiveAt(), '@'), 'example@@example.co.uk'],
[new InvalidEmail(new ConsecutiveDot(), '.'), 'example..example@example.co.uk'],