mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-09-05 06:58:21 +00:00
Starting to transform domain part
This commit is contained in:
@@ -70,11 +70,14 @@ class EmailParser
|
||||
}
|
||||
|
||||
$localPartResult = $this->localPartParser->parse($str);
|
||||
if (!$localPartResult->isValid()) {
|
||||
if ($localPartResult->isInvalid()) {
|
||||
return $localPartResult;
|
||||
}
|
||||
|
||||
$this->domainPartParser->parse($str);
|
||||
$domainPartResult = $this->domainPartParser->parse($str);
|
||||
if ($domainPartResult->isInvalid()) {
|
||||
return $domainPartResult;
|
||||
}
|
||||
|
||||
$this->setParts($str);
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@ use Egulias\EmailValidator\Exception\ExpectingDomainLiteralClose;
|
||||
use Egulias\EmailValidator\Exception\ExpectingDTEXT;
|
||||
use Egulias\EmailValidator\Exception\NoDomainPart;
|
||||
use Egulias\EmailValidator\Exception\UnopenedComment;
|
||||
use Egulias\EmailValidator\Result\InvalidEmail;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtStart as ReasonDotAtStart;
|
||||
use Egulias\EmailValidator\Result\Result;
|
||||
use Egulias\EmailValidator\Result\ValidEmail;
|
||||
use Egulias\EmailValidator\Warning\AddressLiteral;
|
||||
use Egulias\EmailValidator\Warning\CFWSWithFWS;
|
||||
use Egulias\EmailValidator\Warning\DeprecatedComment;
|
||||
@@ -45,7 +49,10 @@ class DomainPart extends Parser
|
||||
{
|
||||
$this->lexer->moveNext();
|
||||
|
||||
$this->performDomainStartChecks();
|
||||
$domainChecks = $this->performDomainStartChecks();
|
||||
if ($domainChecks->isInvalid()) {
|
||||
return $domainChecks;
|
||||
}
|
||||
|
||||
$domain = $this->doParseDomainPart();
|
||||
|
||||
@@ -65,17 +72,23 @@ class DomainPart extends Parser
|
||||
throw new CRLFAtTheEnd();
|
||||
}
|
||||
$this->domainPart = $domain;
|
||||
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
private function performDomainStartChecks()
|
||||
private function performDomainStartChecks() : Result
|
||||
{
|
||||
$this->checkInvalidTokensAfterAT();
|
||||
$invalidTokens = $this->checkInvalidTokensAfterAT();
|
||||
if ($invalidTokens->isInvalid()) {
|
||||
return $invalidTokens;
|
||||
}
|
||||
$this->checkEmptyDomain();
|
||||
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
||||
$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
|
||||
$this->parseDomainComments();
|
||||
}
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
private function checkEmptyDomain()
|
||||
@@ -89,14 +102,15 @@ class DomainPart extends Parser
|
||||
}
|
||||
}
|
||||
|
||||
private function checkInvalidTokensAfterAT()
|
||||
private function checkInvalidTokensAfterAT() : Result
|
||||
{
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
|
||||
throw new DotAtStart();
|
||||
return new InvalidEmail(new ReasonDotAtStart(), $this->lexer->token['value']);
|
||||
}
|
||||
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
|
||||
throw new DomainHyphened();
|
||||
}
|
||||
return new ValidEmail();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,7 @@ use Egulias\EmailValidator\Warning\LocalTooLong;
|
||||
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
|
||||
use Egulias\EmailValidator\Result\Reason\DotAtStart;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT;
|
||||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
|
||||
|
||||
class LocalPart extends Parser
|
||||
{
|
||||
@@ -31,7 +31,6 @@ class LocalPart extends Parser
|
||||
public function parse($localPart) : Result
|
||||
{
|
||||
$totalLength = 0;
|
||||
$this->foldingWS = new FoldingWhiteSpace($this->lexer);
|
||||
|
||||
while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
|
||||
if ($this->hasDotAtStart()) {
|
||||
@@ -73,7 +72,7 @@ class LocalPart extends Parser
|
||||
}
|
||||
|
||||
if (isset($this->invalidTokens[$this->lexer->token['type']])) {
|
||||
return new InvalidEmail(new ReasonExpectingATEXT('Invalid token found'), $this->lexer->token['value']);
|
||||
return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->token['value']);
|
||||
}
|
||||
|
||||
$resultFWS = $this->parseLocalFWS();
|
||||
@@ -94,9 +93,11 @@ class LocalPart extends Parser
|
||||
|
||||
protected function parseLocalFWS() : Result
|
||||
{
|
||||
$resultFWS = $this->foldingWS->parse('remove');
|
||||
|
||||
$foldingWS = new FoldingWhiteSpace($this->lexer);
|
||||
$resultFWS = $foldingWS->parse('remove');
|
||||
if ($resultFWS->isValid()) {
|
||||
$warns = $this->foldingWS->getWarnings();
|
||||
$warns = $foldingWS->getWarnings();
|
||||
foreach ($warns as $code => $dWarning) {
|
||||
$this->warnings[$code] = $dWarning;
|
||||
}
|
||||
|
||||
@@ -201,7 +201,7 @@ class RFCValidationTest extends TestCase
|
||||
[new ConsecutiveDot(), 'example@example..co.uk'],
|
||||
[new InvalidEmail(new ReasonExpectingATEXT('Invalid token found'), '<'), '<example_example>@example.fr'],
|
||||
[new InvalidEmail(new ReasonDotAtStart(), '.'), '.example@localhost'],
|
||||
[new DotAtStart(), 'example@.localhost'],
|
||||
[new InvalidEmail(new ReasonDotAtStart(), '.'), 'example@.localhost'],
|
||||
[new DotAtEnd(), 'example@localhost.'],
|
||||
[new InvalidEmail(new ReasonDotAtEnd(), '.'), 'example.@example.co.uk'],
|
||||
[new InvalidEmail(new ReasonUnclosedComment(), '('), '(example@localhost'],
|
||||
|
||||
Reference in New Issue
Block a user