Starting to change local part parser

This commit is contained in:
Eduardo Gulias Davis
2020-04-23 23:52:32 +02:00
parent 830b061103
commit 4e72769520
4 changed files with 37 additions and 6 deletions
+5 -1
View File
@@ -69,7 +69,11 @@ class EmailParser
return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]);
}
$this->localPartParser->parse($str);
$localPartResult = $this->localPartParser->parse($str);
if (!$localPartResult->isValid()) {
return $localPartResult;
}
$this->domainPartParser->parse($str);
$this->setParts($str);
+14 -4
View File
@@ -3,18 +3,21 @@
namespace Egulias\EmailValidator\Parser;
use Egulias\EmailValidator\Exception\DotAtEnd;
use Egulias\EmailValidator\Exception\DotAtStart;
use Egulias\EmailValidator\EmailLexer;
use Egulias\EmailValidator\Exception\ExpectingAT;
use Egulias\EmailValidator\Exception\ExpectingATEXT;
use Egulias\EmailValidator\Exception\UnclosedQuotedString;
use Egulias\EmailValidator\Exception\UnopenedComment;
use Egulias\EmailValidator\Result\InvalidEmail;
use Egulias\EmailValidator\Result\Reason\DotAtStart;
use Egulias\EmailValidator\Result\Result;
use Egulias\EmailValidator\Result\ValidEmail;
use Egulias\EmailValidator\Warning\CFWSWithFWS;
use Egulias\EmailValidator\Warning\LocalTooLong;
class LocalPart extends Parser
{
public function parse($localPart)
public function parse($localPart) : Result
{
$parseDQuote = true;
$closingQuote = false;
@@ -22,8 +25,8 @@ class LocalPart extends Parser
$totalLength = 0;
while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) {
throw new DotAtStart();
if ($this->hasDotAtStart()) {
return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
}
$closingQuote = $this->checkDQUOTE($closingQuote);
@@ -66,6 +69,13 @@ class LocalPart extends Parser
if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) {
$this->warnings[LocalTooLong::CODE] = new LocalTooLong();
}
return new ValidEmail();
}
protected function hasDotAtStart() : bool
{
return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
}
/**
@@ -0,0 +1,16 @@
<?php
namespace Egulias\EmailValidator\Result\Reason;
class DotAtStart implements Reason
{
public function code() : int
{
return 141;
}
public function description() : string
{
return "Starts with a DOT";
}
}
@@ -38,6 +38,7 @@ use Egulias\EmailValidator\Exception\UnclosedComment;
use Egulias\EmailValidator\Exception\UnopenedComment;
use Egulias\EmailValidator\Result\Reason\NoLocalPart;
use Egulias\EmailValidator\Exception\UnclosedQuotedString;
use Egulias\EmailValidator\Result\Reason\DotAtStart as ReasonDotAtStart;
class RFCValidationTest extends TestCase
{
@@ -192,7 +193,7 @@ class RFCValidationTest extends TestCase
[new ConsecutiveDot(), 'example..example@example.co.uk'],
[new ConsecutiveDot(), 'example@example..co.uk'],
[new ExpectingATEXT(), '<example_example>@example.fr'],
[new DotAtStart(), '.example@localhost'],
[new InvalidEmail(new ReasonDotAtStart(), '.'), '.example@localhost'],
[new DotAtStart(), 'example@.localhost'],
[new DotAtEnd(), 'example@localhost.'],
[new DotAtEnd(), 'example.@example.co.uk'],