-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* scafolding for MessageID validation * Improve domain valid tokens * Improved EmailParser to remove leaked logic. User of lexer recorder within parsers. * MessageIDParser passing tests. * change left for right, which is the right one * psaml errors * Better naming * comments are not allowed in IDLeft for message-id * Suppress psalm inheritance over tokens and dependencies * Update src/Parser.php Co-authored-by: Alexander M. Turek <me@derrabus.de> * Update src/Parser.php Co-authored-by: Alexander M. Turek <me@derrabus.de> * improve parser from comments Co-authored-by: Alexander M. Turek <me@derrabus.de>
- Loading branch information
Showing
17 changed files
with
557 additions
and
240 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator; | ||
|
||
use Egulias\EmailValidator\Parser; | ||
use Egulias\EmailValidator\EmailLexer; | ||
use Egulias\EmailValidator\Result\Result; | ||
use Egulias\EmailValidator\Parser\IDLeftPart; | ||
use Egulias\EmailValidator\Parser\IDRightPart; | ||
use Egulias\EmailValidator\Result\ValidEmail; | ||
use Egulias\EmailValidator\Result\InvalidEmail; | ||
use Egulias\EmailValidator\Warning\EmailTooLong; | ||
use Egulias\EmailValidator\Result\Reason\NoLocalPart; | ||
|
||
class MessageIDParser extends Parser | ||
{ | ||
|
||
const EMAILID_MAX_LENGTH = 254; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $idLeft = ''; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $idRight = ''; | ||
|
||
public function parse(string $str) : Result | ||
{ | ||
$result = parent::parse($str); | ||
|
||
$this->addLongEmailWarning($this->idLeft, $this->idRight); | ||
|
||
return $result; | ||
} | ||
|
||
protected function preLeftParsing(): Result | ||
{ | ||
if (!$this->hasAtToken()) { | ||
return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]); | ||
} | ||
return new ValidEmail(); | ||
} | ||
|
||
protected function parseLeftFromAt(): Result | ||
{ | ||
return $this->processIDLeft(); | ||
} | ||
|
||
protected function parseRightFromAt(): Result | ||
{ | ||
return $this->processIDRight(); | ||
} | ||
|
||
private function processIDLeft() : Result | ||
{ | ||
$localPartParser = new IDLeftPart($this->lexer); | ||
$localPartResult = $localPartParser->parse(); | ||
$this->idLeft = $localPartParser->localPart(); | ||
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings); | ||
|
||
return $localPartResult; | ||
} | ||
|
||
private function processIDRight() : Result | ||
{ | ||
$domainPartParser = new IDRightPart($this->lexer); | ||
$domainPartResult = $domainPartParser->parse(); | ||
$this->idRight = $domainPartParser->domainPart(); | ||
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings); | ||
|
||
return $domainPartResult; | ||
} | ||
|
||
public function getLeftPart() : string | ||
{ | ||
return $this->idLeft; | ||
} | ||
|
||
public function getRightPart() : string | ||
{ | ||
return $this->idRight; | ||
} | ||
|
||
private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void | ||
{ | ||
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) { | ||
$this->warnings[EmailTooLong::CODE] = new EmailTooLong(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Egulias\EmailValidator; | ||
|
||
use Egulias\EmailValidator\Result\Result; | ||
use Egulias\EmailValidator\Result\ValidEmail; | ||
use Egulias\EmailValidator\Result\InvalidEmail; | ||
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT; | ||
|
||
abstract class Parser | ||
{ | ||
/** | ||
* @var Warning\Warning[] | ||
*/ | ||
protected $warnings = []; | ||
|
||
/** | ||
* @var EmailLexer | ||
*/ | ||
protected $lexer; | ||
|
||
/** | ||
* id-left "@" id-right | ||
*/ | ||
abstract protected function parseRightFromAt() : Result; | ||
abstract protected function parseLeftFromAt() : Result; | ||
abstract protected function preLeftParsing() : Result; | ||
|
||
|
||
public function __construct(EmailLexer $lexer) | ||
{ | ||
$this->lexer = $lexer; | ||
} | ||
|
||
public function parse(string $str) : Result | ||
{ | ||
$this->lexer->setInput($str); | ||
|
||
if ($this->lexer->hasInvalidTokens()) { | ||
return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->token["value"]); | ||
} | ||
|
||
$preParsingResult = $this->preLeftParsing(); | ||
if ($preParsingResult->isInvalid()) { | ||
return $preParsingResult; | ||
} | ||
|
||
$localPartResult = $this->parseLeftFromAt(); | ||
|
||
if ($localPartResult->isInvalid()) { | ||
return $localPartResult; | ||
} | ||
|
||
$domainPartResult = $this->parseRightFromAt(); | ||
|
||
if ($domainPartResult->isInvalid()) { | ||
return $domainPartResult; | ||
} | ||
|
||
return new ValidEmail(); | ||
} | ||
|
||
/** | ||
* @return Warning\Warning[] | ||
*/ | ||
public function getWarnings() : array | ||
{ | ||
return $this->warnings; | ||
} | ||
|
||
protected function hasAtToken() : bool | ||
{ | ||
$this->lexer->moveNext(); | ||
$this->lexer->moveNext(); | ||
|
||
return $this->lexer->token['type'] !== EmailLexer::S_AT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.