egulias /
EmailValidator
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Egulias\EmailValidator; |
||
| 4 | |||
| 5 | use Egulias\EmailValidator\Result\Result; |
||
| 6 | use Egulias\EmailValidator\Result\ValidEmail; |
||
| 7 | use Egulias\EmailValidator\Result\InvalidEmail; |
||
| 8 | use Egulias\EmailValidator\Result\Reason\ExpectingATEXT; |
||
| 9 | |||
| 10 | abstract class Parser |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var Warning\Warning[] |
||
| 14 | */ |
||
| 15 | protected $warnings = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var EmailLexer |
||
| 19 | */ |
||
| 20 | protected $lexer; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * id-left "@" id-right |
||
| 24 | */ |
||
| 25 | abstract protected function parseRightFromAt(): Result; |
||
| 26 | abstract protected function parseLeftFromAt(): Result; |
||
| 27 | abstract protected function preLeftParsing(): Result; |
||
| 28 | |||
| 29 | |||
| 30 | 191 | public function __construct(EmailLexer $lexer) |
|
| 31 | { |
||
| 32 | 191 | $this->lexer = $lexer; |
|
| 33 | } |
||
| 34 | |||
| 35 | 191 | public function parse(string $str): Result |
|
| 36 | { |
||
| 37 | 191 | $this->lexer->setInput($str); |
|
| 38 | |||
| 39 | 191 | if ($this->lexer->hasInvalidTokens()) { |
|
| 40 | 4 | return new InvalidEmail(new ExpectingATEXT("Invalid tokens found"), $this->lexer->current->value); |
|
| 41 | } |
||
| 42 | |||
| 43 | 187 | $preParsingResult = $this->preLeftParsing(); |
|
| 44 | 187 | if ($preParsingResult->isInvalid()) { |
|
| 45 | 1 | return $preParsingResult; |
|
| 46 | } |
||
| 47 | |||
| 48 | 186 | $localPartResult = $this->parseLeftFromAt(); |
|
| 49 | |||
| 50 | 186 | if ($localPartResult->isInvalid()) { |
|
| 51 | 38 | return $localPartResult; |
|
| 52 | } |
||
| 53 | |||
| 54 | 148 | $domainPartResult = $this->parseRightFromAt(); |
|
| 55 | |||
| 56 | 148 | if ($domainPartResult->isInvalid()) { |
|
| 57 | 84 | return $domainPartResult; |
|
| 58 | } |
||
| 59 | |||
| 60 | 64 | return new ValidEmail(); |
|
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @return Warning\Warning[] |
||
| 65 | */ |
||
| 66 | 187 | public function getWarnings(): array |
|
| 67 | { |
||
| 68 | 187 | return $this->warnings; |
|
| 69 | } |
||
| 70 | |||
| 71 | 187 | protected function hasAtToken(): bool |
|
| 72 | { |
||
| 73 | 187 | $this->lexer->moveNext(); |
|
| 74 | 187 | $this->lexer->moveNext(); |
|
| 75 | |||
| 76 | 187 | return !$this->lexer->current->isA(EmailLexer::S_AT); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 77 | } |
||
| 78 | } |
||
| 79 |