Parser.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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\ExpectingQPair;
  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. /**
  22. * @var \Egulias\EmailValidator\Warning\Warning[]
  23. */
  24. protected $warnings = [];
  25. /**
  26. * @var EmailLexer
  27. */
  28. protected $lexer;
  29. /**
  30. * @var int
  31. */
  32. protected $openedParenthesis = 0;
  33. public function __construct(EmailLexer $lexer)
  34. {
  35. $this->lexer = $lexer;
  36. }
  37. /**
  38. * @return \Egulias\EmailValidator\Warning\Warning[]
  39. */
  40. public function getWarnings()
  41. {
  42. return $this->warnings;
  43. }
  44. /**
  45. * @param string $str
  46. */
  47. abstract public function parse($str);
  48. /** @return int */
  49. public function getOpenedParenthesis()
  50. {
  51. return $this->openedParenthesis;
  52. }
  53. /**
  54. * validateQuotedPair
  55. */
  56. protected function validateQuotedPair()
  57. {
  58. if (!($this->lexer->token['type'] === EmailLexer::INVALID
  59. || $this->lexer->token['type'] === EmailLexer::C_DEL)) {
  60. throw new ExpectingQPair();
  61. }
  62. $this->warnings[QuotedPart::CODE] =
  63. new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
  64. }
  65. protected function parseComments()
  66. {
  67. $this->openedParenthesis = 1;
  68. $this->isUnclosedComment();
  69. $this->warnings[Comment::CODE] = new Comment();
  70. while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
  71. if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
  72. $this->openedParenthesis++;
  73. }
  74. $this->warnEscaping();
  75. $this->lexer->moveNext();
  76. }
  77. $this->lexer->moveNext();
  78. if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) {
  79. throw new ExpectingATEXT();
  80. }
  81. if ($this->lexer->isNextToken(EmailLexer::S_AT)) {
  82. $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
  83. }
  84. }
  85. /**
  86. * @return bool
  87. */
  88. protected function isUnclosedComment()
  89. {
  90. try {
  91. $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
  92. return true;
  93. } catch (\RuntimeException $e) {
  94. throw new UnclosedComment();
  95. }
  96. }
  97. protected function parseFWS()
  98. {
  99. $previous = $this->lexer->getPrevious();
  100. $this->checkCRLFInFWS();
  101. if ($this->lexer->token['type'] === EmailLexer::S_CR) {
  102. throw new CRNoLF();
  103. }
  104. if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) {
  105. throw new AtextAfterCFWS();
  106. }
  107. if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) {
  108. throw new ExpectingCTEXT();
  109. }
  110. if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) {
  111. $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
  112. } else {
  113. $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
  114. }
  115. }
  116. protected function checkConsecutiveDots()
  117. {
  118. if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
  119. throw new ConsecutiveDot();
  120. }
  121. }
  122. /**
  123. * @return bool
  124. */
  125. protected function isFWS()
  126. {
  127. if ($this->escaped()) {
  128. return false;
  129. }
  130. if ($this->lexer->token['type'] === EmailLexer::S_SP ||
  131. $this->lexer->token['type'] === EmailLexer::S_HTAB ||
  132. $this->lexer->token['type'] === EmailLexer::S_CR ||
  133. $this->lexer->token['type'] === EmailLexer::S_LF ||
  134. $this->lexer->token['type'] === EmailLexer::CRLF
  135. ) {
  136. return true;
  137. }
  138. return false;
  139. }
  140. /**
  141. * @return bool
  142. */
  143. protected function escaped()
  144. {
  145. $previous = $this->lexer->getPrevious();
  146. if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH
  147. &&
  148. $this->lexer->token['type'] !== EmailLexer::GENERIC
  149. ) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * @return bool
  156. */
  157. protected function warnEscaping()
  158. {
  159. if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
  160. return false;
  161. }
  162. if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
  163. throw new ExpectingATEXT();
  164. }
  165. if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
  166. return false;
  167. }
  168. $this->warnings[QuotedPart::CODE] =
  169. new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
  170. return true;
  171. }
  172. /**
  173. * @param bool $hasClosingQuote
  174. *
  175. * @return bool
  176. */
  177. protected function checkDQUOTE($hasClosingQuote)
  178. {
  179. if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) {
  180. return $hasClosingQuote;
  181. }
  182. if ($hasClosingQuote) {
  183. return $hasClosingQuote;
  184. }
  185. $previous = $this->lexer->getPrevious();
  186. if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) {
  187. throw new ExpectingATEXT();
  188. }
  189. try {
  190. $this->lexer->find(EmailLexer::S_DQUOTE);
  191. $hasClosingQuote = true;
  192. } catch (\Exception $e) {
  193. throw new UnclosedQuotedString();
  194. }
  195. $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']);
  196. return $hasClosingQuote;
  197. }
  198. protected function checkCRLFInFWS()
  199. {
  200. if ($this->lexer->token['type'] !== EmailLexer::CRLF) {
  201. return;
  202. }
  203. if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
  204. throw new CRLFX2();
  205. }
  206. if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
  207. throw new CRLFAtTheEnd();
  208. }
  209. }
  210. }