Parser.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Egulias\EmailValidator\Parser;
  3. use Egulias\EmailValidator\EmailLexer;
  4. use Egulias\EmailValidator\Exception\AtextAfterCFWS;
  5. use Egulias\EmailValidator\Exception\ConsecutiveDot;
  6. use Egulias\EmailValidator\Exception\CRLFAtTheEnd;
  7. use Egulias\EmailValidator\Exception\CRLFX2;
  8. use Egulias\EmailValidator\Exception\CRNoLF;
  9. use Egulias\EmailValidator\Exception\ExpectedQPair;
  10. use Egulias\EmailValidator\Exception\ExpectingATEXT;
  11. use Egulias\EmailValidator\Exception\ExpectingCTEXT;
  12. use Egulias\EmailValidator\Exception\UnclosedComment;
  13. use Egulias\EmailValidator\Exception\UnclosedQuotedString;
  14. use Egulias\EmailValidator\Warning\CFWSNearAt;
  15. use Egulias\EmailValidator\Warning\CFWSWithFWS;
  16. use Egulias\EmailValidator\Warning\Comment;
  17. use Egulias\EmailValidator\Warning\QuotedPart;
  18. use Egulias\EmailValidator\Warning\QuotedString;
  19. abstract class Parser
  20. {
  21. protected $warnings = [];
  22. protected $lexer;
  23. protected $openedParenthesis = 0;
  24. public function __construct(EmailLexer $lexer)
  25. {
  26. $this->lexer = $lexer;
  27. }
  28. public function getWarnings()
  29. {
  30. return $this->warnings;
  31. }
  32. abstract public function parse($str);
  33. /** @return int */
  34. public function getOpenedParenthesis()
  35. {
  36. return $this->openedParenthesis;
  37. }
  38. /**
  39. * validateQuotedPair
  40. */
  41. protected function validateQuotedPair()
  42. {
  43. if (!($this->lexer->token['type'] === EmailLexer::INVALID
  44. || $this->lexer->token['type'] === EmailLexer::C_DEL)) {
  45. throw new ExpectedQPair();
  46. }
  47. $this->warnings[QuotedPart::CODE] =
  48. new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
  49. }
  50. protected function parseComments()
  51. {
  52. $this->openedParenthesis = 1;
  53. $this->isUnclosedComment();
  54. $this->warnings[Comment::CODE] = new Comment();
  55. while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
  56. if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
  57. $this->openedParenthesis++;
  58. }
  59. $this->warnEscaping();
  60. $this->lexer->moveNext();
  61. }
  62. $this->lexer->moveNext();
  63. if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) {
  64. throw new ExpectingATEXT();
  65. }
  66. if ($this->lexer->isNextToken(EmailLexer::S_AT)) {
  67. $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
  68. }
  69. }
  70. protected function isUnclosedComment()
  71. {
  72. try {
  73. $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
  74. return true;
  75. } catch (\RuntimeException $e) {
  76. throw new UnclosedComment();
  77. }
  78. }
  79. protected function parseFWS()
  80. {
  81. $previous = $this->lexer->getPrevious();
  82. $this->checkCRLFInFWS();
  83. if ($this->lexer->token['type'] === EmailLexer::S_CR) {
  84. throw new CRNoLF();
  85. }
  86. if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) {
  87. throw new AtextAfterCFWS();
  88. }
  89. if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
  90. throw new ExpectingCTEXT();
  91. }
  92. if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) {
  93. $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
  94. } else {
  95. $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
  96. }
  97. }
  98. protected function checkConsecutiveDots()
  99. {
  100. if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
  101. throw new ConsecutiveDot();
  102. }
  103. }
  104. protected function isFWS()
  105. {
  106. if ($this->escaped()) {
  107. return false;
  108. }
  109. if ($this->lexer->token['type'] === EmailLexer::S_SP ||
  110. $this->lexer->token['type'] === EmailLexer::S_HTAB ||
  111. $this->lexer->token['type'] === EmailLexer::S_CR ||
  112. $this->lexer->token['type'] === EmailLexer::S_LF ||
  113. $this->lexer->token['type'] === EmailLexer::CRLF
  114. ) {
  115. return true;
  116. }
  117. return false;
  118. }
  119. protected function escaped()
  120. {
  121. $previous = $this->lexer->getPrevious();
  122. if ($previous['type'] === EmailLexer::S_BACKSLASH
  123. &&
  124. $this->lexer->token['type'] !== EmailLexer::GENERIC
  125. ) {
  126. return true;
  127. }
  128. return false;
  129. }
  130. protected function warnEscaping()
  131. {
  132. if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
  133. return false;
  134. }
  135. if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
  136. throw new ExpectingATEXT();
  137. }
  138. if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
  139. return false;
  140. }
  141. $this->warnings[QuotedPart::CODE] =
  142. new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
  143. return true;
  144. }
  145. protected function checkDQUOTE($hasClosingQuote)
  146. {
  147. if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) {
  148. return $hasClosingQuote;
  149. }
  150. if ($hasClosingQuote) {
  151. return $hasClosingQuote;
  152. }
  153. $previous = $this->lexer->getPrevious();
  154. if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) {
  155. throw new ExpectingATEXT();
  156. }
  157. try {
  158. $this->lexer->find(EmailLexer::S_DQUOTE);
  159. $hasClosingQuote = true;
  160. } catch (\Exception $e) {
  161. throw new UnclosedQuotedString();
  162. }
  163. $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']);
  164. return $hasClosingQuote;
  165. }
  166. protected function checkCRLFInFWS()
  167. {
  168. if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
  169. return;
  170. }
  171. if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
  172. throw new CRLFX2();
  173. }
  174. if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
  175. throw new CRLFAtTheEnd();
  176. }
  177. }
  178. }