mirror of
https://github.com/egulias/EmailValidator.git
synced 2026-11-25 21:42:23 +00:00
261 lines
8.6 KiB
PHP
261 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace Egulias\EmailValidator\Parser;
|
|
|
|
use Egulias\EmailValidator\EmailLexer;
|
|
use Egulias\EmailValidator\Exception\CommaInDomain;
|
|
use Egulias\EmailValidator\Result\InvalidEmail;
|
|
use Egulias\EmailValidator\Result\Reason\CharNotAllowed;
|
|
use Egulias\EmailValidator\Result\Reason\DomainHyphened;
|
|
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
|
|
use Egulias\EmailValidator\Result\Reason\DotAtStart;
|
|
use Egulias\EmailValidator\Result\Reason\NoDomainPart;
|
|
use Egulias\EmailValidator\Result\Reason\ConsecutiveAt;
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingDomainLiteralClose;
|
|
use Egulias\EmailValidator\Result\Result;
|
|
use Egulias\EmailValidator\Result\ValidEmail;
|
|
use Egulias\EmailValidator\Warning\DeprecatedComment;
|
|
use Egulias\EmailValidator\Warning\DomainLiteral;
|
|
use Egulias\EmailValidator\Warning\DomainTooLong;
|
|
use Egulias\EmailValidator\Warning\LabelTooLong;
|
|
use Egulias\EmailValidator\Warning\TLD;
|
|
use Egulias\EmailValidator\Parser\DomainLiteral as DomainLiteralParser;
|
|
|
|
class DomainPart extends Parser
|
|
{
|
|
const DOMAIN_MAX_LENGTH = 254;
|
|
|
|
/**
|
|
* @var string
|
|
*/
|
|
protected $domainPart = '';
|
|
|
|
public function parse($domainPart)
|
|
{
|
|
$this->lexer->moveNext();
|
|
|
|
$domainChecks = $this->performDomainStartChecks();
|
|
if ($domainChecks->isInvalid()) {
|
|
return $domainChecks;
|
|
}
|
|
|
|
$domain = $this->doParseDomainPart();
|
|
if ($domain->isInvalid()) {
|
|
return $domain;
|
|
}
|
|
|
|
$prev = $this->lexer->getPrevious();
|
|
$length = strlen($this->domainPart);
|
|
|
|
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();
|
|
}
|
|
|
|
return new ValidEmail();
|
|
}
|
|
|
|
private function performDomainStartChecks() : Result
|
|
{
|
|
$invalidTokens = $this->checkInvalidTokensAfterAT();
|
|
if ($invalidTokens->isInvalid()) {
|
|
return $invalidTokens;
|
|
}
|
|
|
|
$missingDomain = $this->checkEmptyDomain();
|
|
if ($missingDomain->isInvalid()) {
|
|
return $missingDomain;
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
|
$this->warnings[DeprecatedComment::CODE] = new DeprecatedComment();
|
|
}
|
|
return new ValidEmail();
|
|
}
|
|
|
|
private function checkEmptyDomain() : Result
|
|
{
|
|
$thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY ||
|
|
($this->lexer->token['type'] === EmailLexer::S_SP &&
|
|
!$this->lexer->isNextToken(EmailLexer::GENERIC));
|
|
|
|
if ($thereIsNoDomain) {
|
|
return new InvalidEmail(new NoDomainPart(), $this->lexer->token['value']);
|
|
}
|
|
|
|
return new ValidEmail();
|
|
}
|
|
|
|
private function checkInvalidTokensAfterAT() : Result
|
|
{
|
|
if ($this->lexer->token['type'] === EmailLexer::S_DOT) {
|
|
return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
|
|
}
|
|
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) {
|
|
return new InvalidEmail(new DomainHyphened('After AT'), $this->lexer->token['value']);
|
|
}
|
|
return new ValidEmail();
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getDomainPart()
|
|
{
|
|
return $this->domainPart;
|
|
}
|
|
|
|
protected function parseComments()
|
|
{
|
|
$commentParser = new Comment($this->lexer, new DomainComment());
|
|
$result = $commentParser->parse('remove');
|
|
if($result->isInvalid()) {
|
|
return $result;
|
|
}
|
|
|
|
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
|
|
return $result;
|
|
}
|
|
|
|
protected function doParseDomainPart() : Result
|
|
{
|
|
$domain = '';
|
|
do {
|
|
$prev = $this->lexer->getPrevious();
|
|
|
|
$notAllowedChars = $this->checkNotAllowedChars($this->lexer->token);
|
|
if ($notAllowedChars->isInvalid()) {
|
|
return $notAllowedChars;
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS ||
|
|
$this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) {
|
|
$commentsResult = $this->parseComments();
|
|
|
|
//Invalid comment parsing
|
|
if($commentsResult->isInvalid()) {
|
|
return $commentsResult;
|
|
}
|
|
}
|
|
|
|
$dotsResult = $this->checkConsecutiveDots();
|
|
if ($dotsResult->isInvalid()) {
|
|
return $dotsResult;
|
|
}
|
|
$exceptionsResult = $this->checkDomainPartExceptions($prev);
|
|
if ($exceptionsResult->isInvalid()) {
|
|
return $exceptionsResult;
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENBRACKET) {
|
|
$literalResult = $this->parseDomainLiteral();
|
|
//Invalid literal parsing
|
|
if($literalResult->isInvalid()) {
|
|
return $literalResult;
|
|
}
|
|
}
|
|
|
|
$this->checkLabelLength($prev);
|
|
|
|
$FwsResult = $this->parseFWS();
|
|
if($FwsResult->isInvalid()) {
|
|
return $FwsResult;
|
|
}
|
|
|
|
$domain .= $this->lexer->token['value'];
|
|
$this->lexer->moveNext();
|
|
} while (null !== $this->lexer->token['type']);
|
|
|
|
$this->domainPart = $domain;
|
|
return new ValidEmail();
|
|
}
|
|
|
|
private function checkNotAllowedChars(array $token) : Result
|
|
{
|
|
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
|
|
if (isset($notAllowed[$token['type']])) {
|
|
return new InvalidEmail(new CharNotAllowed(), $token['value']);
|
|
}
|
|
return new ValidEmail();
|
|
}
|
|
|
|
/**
|
|
* @return string|false
|
|
*/
|
|
protected function parseDomainLiteral() : Result
|
|
{
|
|
|
|
try {
|
|
$this->lexer->find(EmailLexer::S_CLOSEBRACKET);
|
|
} catch (\RuntimeException $e) {
|
|
return new InvalidEmail(new ExpectingDomainLiteralClose(), $this->lexer->token['value']);
|
|
}
|
|
|
|
$domainLiteralParser = new DomainLiteralParser($this->lexer);
|
|
$result = $domainLiteralParser->parse('remove');
|
|
$this->warnings = array_merge($this->warnings, $domainLiteralParser->getWarnings());
|
|
return $result;
|
|
|
|
//return $this->doParseDomainLiteral();
|
|
}
|
|
|
|
protected function checkDomainPartExceptions(array $prev)
|
|
{
|
|
$invalidDomainTokens = array(
|
|
EmailLexer::S_DQUOTE => true,
|
|
EmailLexer::S_SEMICOLON => true,
|
|
EmailLexer::S_GREATERTHAN => true,
|
|
EmailLexer::S_LOWERTHAN => true,
|
|
);
|
|
|
|
if (isset($invalidDomainTokens[$this->lexer->token['type']])) {
|
|
return new InvalidEmail(new ExpectingATEXT('Invalid token in domain'), $this->lexer->token['value']);
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_COMMA) {
|
|
throw new CommaInDomain();
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_AT) {
|
|
return new InvalidEmail(new ConsecutiveAt(), $this->lexer->token['value']);
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) {
|
|
return new InvalidEmail(new ExpectingATEXT('OPENBRACKET not after AT'), $this->lexer->token['value']);
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
|
|
return new InvalidEmail(new DomainHyphened('Hypen found near DOT'), $this->lexer->token['value']);
|
|
}
|
|
|
|
if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH
|
|
&& $this->lexer->isNextToken(EmailLexer::GENERIC)) {
|
|
return new InvalidEmail(new ExpectingATEXT('Escaping following "ATOM"'), $this->lexer->token['value']);
|
|
}
|
|
|
|
return new ValidEmail();
|
|
}
|
|
|
|
protected function checkLabelLength(array $prev)
|
|
{
|
|
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
|
|
$prev['type'] === EmailLexer::GENERIC &&
|
|
strlen($prev['value']) > 63
|
|
) {
|
|
$this->warnings[LabelTooLong::CODE] = new LabelTooLong();
|
|
}
|
|
}
|
|
|
|
protected function addTLDWarnings()
|
|
{
|
|
if ($this->warnings[DomainLiteral::CODE]) {
|
|
$this->warnings[TLD::CODE] = new TLD();
|
|
}
|
|
}
|
|
} |