PartParser.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Egulias\EmailValidator\Parser;
  3. use Egulias\EmailValidator\EmailLexer;
  4. use Egulias\EmailValidator\Result\InvalidEmail;
  5. use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
  6. use Egulias\EmailValidator\Result\Result;
  7. use Egulias\EmailValidator\Result\ValidEmail;
  8. abstract class PartParser
  9. {
  10. /**
  11. * @var array
  12. */
  13. protected $warnings = [];
  14. /**
  15. * @var EmailLexer
  16. */
  17. protected $lexer;
  18. public function __construct(EmailLexer $lexer)
  19. {
  20. $this->lexer = $lexer;
  21. }
  22. abstract public function parse() : Result;
  23. /**
  24. * @return \Egulias\EmailValidator\Warning\Warning[]
  25. */
  26. public function getWarnings()
  27. {
  28. return $this->warnings;
  29. }
  30. protected function parseFWS() : Result
  31. {
  32. $foldingWS = new FoldingWhiteSpace($this->lexer);
  33. $resultFWS = $foldingWS->parse();
  34. $this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
  35. return $resultFWS;
  36. }
  37. protected function checkConsecutiveDots() : Result
  38. {
  39. if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
  40. return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
  41. }
  42. return new ValidEmail();
  43. }
  44. protected function escaped() : bool
  45. {
  46. $previous = $this->lexer->getPrevious();
  47. return $previous && $previous['type'] === EmailLexer::S_BACKSLASH
  48. &&
  49. $this->lexer->token['type'] !== EmailLexer::GENERIC;
  50. }
  51. }