EmailParser.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Egulias\EmailValidator;
  3. use Egulias\EmailValidator\Exception\ExpectingATEXT;
  4. use Egulias\EmailValidator\Exception\NoLocalPart;
  5. use Egulias\EmailValidator\Parser\DomainPart;
  6. use Egulias\EmailValidator\Parser\LocalPart;
  7. use Egulias\EmailValidator\Warning\EmailTooLong;
  8. /**
  9. * EmailParser
  10. *
  11. * @author Eduardo Gulias Davis <me@egulias.com>
  12. */
  13. class EmailParser
  14. {
  15. const EMAIL_MAX_LENGTH = 254;
  16. /**
  17. * @var array
  18. */
  19. protected $warnings = [];
  20. /**
  21. * @var string
  22. */
  23. protected $domainPart = '';
  24. /**
  25. * @var string
  26. */
  27. protected $localPart = '';
  28. /**
  29. * @var EmailLexer
  30. */
  31. protected $lexer;
  32. /**
  33. * @var LocalPart
  34. */
  35. protected $localPartParser;
  36. /**
  37. * @var DomainPart
  38. */
  39. protected $domainPartParser;
  40. public function __construct(EmailLexer $lexer)
  41. {
  42. $this->lexer = $lexer;
  43. $this->localPartParser = new LocalPart($this->lexer);
  44. $this->domainPartParser = new DomainPart($this->lexer);
  45. }
  46. /**
  47. * @param string $str
  48. * @return array
  49. */
  50. public function parse($str)
  51. {
  52. $this->lexer->setInput($str);
  53. if (!$this->hasAtToken()) {
  54. throw new NoLocalPart();
  55. }
  56. $this->localPartParser->parse($str);
  57. $this->domainPartParser->parse($str);
  58. $this->setParts($str);
  59. if ($this->lexer->hasInvalidTokens()) {
  60. throw new ExpectingATEXT();
  61. }
  62. return array('local' => $this->localPart, 'domain' => $this->domainPart);
  63. }
  64. /**
  65. * @return Warning\Warning[]
  66. */
  67. public function getWarnings()
  68. {
  69. $localPartWarnings = $this->localPartParser->getWarnings();
  70. $domainPartWarnings = $this->domainPartParser->getWarnings();
  71. $this->warnings = array_merge($localPartWarnings, $domainPartWarnings);
  72. $this->addLongEmailWarning($this->localPart, $this->domainPart);
  73. return $this->warnings;
  74. }
  75. /**
  76. * @return string
  77. */
  78. public function getParsedDomainPart()
  79. {
  80. return $this->domainPart;
  81. }
  82. /**
  83. * @param string $email
  84. */
  85. protected function setParts($email)
  86. {
  87. $parts = explode('@', $email);
  88. $this->domainPart = $this->domainPartParser->getDomainPart();
  89. $this->localPart = $parts[0];
  90. }
  91. /**
  92. * @return bool
  93. */
  94. protected function hasAtToken()
  95. {
  96. $this->lexer->moveNext();
  97. $this->lexer->moveNext();
  98. if ($this->lexer->token['type'] === EmailLexer::S_AT) {
  99. return false;
  100. }
  101. return true;
  102. }
  103. /**
  104. * @param string $localPart
  105. * @param string $parsedDomainPart
  106. */
  107. protected function addLongEmailWarning($localPart, $parsedDomainPart)
  108. {
  109. if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAIL_MAX_LENGTH) {
  110. $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
  111. }
  112. }
  113. }