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(); } } }