From 86a022cd66d648bea78c3082ae4a82109bbd6c19 Mon Sep 17 00:00:00 2001 From: Eduardo Gulias Davis Date: Thu, 8 May 2014 18:13:17 +0200 Subject: [PATCH] Refactor of domain part exceptions --- src/Egulias/EmailValidator/EmailParser.php | 34 ++++++++++++---------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Egulias/EmailValidator/EmailParser.php b/src/Egulias/EmailValidator/EmailParser.php index 347588f..de13f6a 100644 --- a/src/Egulias/EmailValidator/EmailParser.php +++ b/src/Egulias/EmailValidator/EmailParser.php @@ -308,32 +308,18 @@ class EmailParser do { $prev = $this->lexer->getPrevious(); - if ($this->lexer->token['type'] === EmailLexer::S_AT) { - throw new \InvalidArgumentException('ERR_CONSECUTIVEATS'); - } - if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) { - throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); - } if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { $this->parseComments(); $this->lexer->moveNext(); } $this->checkConsecutiveDots(); - - if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { - throw new \InvalidArgumentException('ERR_DOMAINHYPHENEND'); - } + $this->checkDomainPartExceptions(); if ($this->hasBrackets()) { $this->parseDomainLiteral(); } - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH - && $this->lexer->isNextToken(EmailLexer::GENERIC)) { - throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); - } - $this->checkLabelLength($prev); if ($this->isFWS()) { @@ -503,4 +489,22 @@ class EmailParser return true; } + + private function checkDomainPartExceptions() + { + if ($this->lexer->token['type'] === EmailLexer::S_AT) { + throw new \InvalidArgumentException('ERR_CONSECUTIVEATS'); + } + if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) { + throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); + } + if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { + throw new \InvalidArgumentException('ERR_DOMAINHYPHENEND'); + } + + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH + && $this->lexer->isNextToken(EmailLexer::GENERIC)) { + throw new \InvalidArgumentException('ERR_EXPECTING_ATEXT'); + } + } }