@@ -9,43 +9,43 @@ |
||
9 | 9 | |
10 | 10 | class SpoofCheckValidation implements EmailValidation |
11 | 11 | { |
12 | - /** |
|
13 | - * @var InvalidEmail|null |
|
14 | - */ |
|
15 | - private $error; |
|
16 | - |
|
17 | - public function __construct() |
|
18 | - { |
|
19 | - if (!extension_loaded('intl')) { |
|
20 | - throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); |
|
21 | - } |
|
22 | - } |
|
23 | - |
|
24 | - /** |
|
25 | - * @psalm-suppress InvalidArgument |
|
26 | - */ |
|
27 | - public function isValid($email, EmailLexer $emailLexer) |
|
28 | - { |
|
29 | - $checker = new Spoofchecker(); |
|
30 | - $checker->setChecks(Spoofchecker::SINGLE_SCRIPT); |
|
31 | - |
|
32 | - if ($checker->isSuspicious($email)) { |
|
33 | - $this->error = new SpoofEmail(); |
|
34 | - } |
|
35 | - |
|
36 | - return $this->error === null; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @return InvalidEmail|null |
|
41 | - */ |
|
42 | - public function getError() |
|
43 | - { |
|
44 | - return $this->error; |
|
45 | - } |
|
46 | - |
|
47 | - public function getWarnings() |
|
48 | - { |
|
49 | - return []; |
|
50 | - } |
|
12 | + /** |
|
13 | + * @var InvalidEmail|null |
|
14 | + */ |
|
15 | + private $error; |
|
16 | + |
|
17 | + public function __construct() |
|
18 | + { |
|
19 | + if (!extension_loaded('intl')) { |
|
20 | + throw new \LogicException(sprintf('The %s class requires the Intl extension.', __CLASS__)); |
|
21 | + } |
|
22 | + } |
|
23 | + |
|
24 | + /** |
|
25 | + * @psalm-suppress InvalidArgument |
|
26 | + */ |
|
27 | + public function isValid($email, EmailLexer $emailLexer) |
|
28 | + { |
|
29 | + $checker = new Spoofchecker(); |
|
30 | + $checker->setChecks(Spoofchecker::SINGLE_SCRIPT); |
|
31 | + |
|
32 | + if ($checker->isSuspicious($email)) { |
|
33 | + $this->error = new SpoofEmail(); |
|
34 | + } |
|
35 | + |
|
36 | + return $this->error === null; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @return InvalidEmail|null |
|
41 | + */ |
|
42 | + public function getError() |
|
43 | + { |
|
44 | + return $this->error; |
|
45 | + } |
|
46 | + |
|
47 | + public function getWarnings() |
|
48 | + { |
|
49 | + return []; |
|
50 | + } |
|
51 | 51 | } |
@@ -8,34 +8,34 @@ |
||
8 | 8 | |
9 | 9 | class NoRFCWarningsValidation extends RFCValidation |
10 | 10 | { |
11 | - /** |
|
12 | - * @var InvalidEmail|null |
|
13 | - */ |
|
14 | - private $error; |
|
15 | - |
|
16 | - /** |
|
17 | - * {@inheritdoc} |
|
18 | - */ |
|
19 | - public function isValid($email, EmailLexer $emailLexer) |
|
20 | - { |
|
21 | - if (!parent::isValid($email, $emailLexer)) { |
|
22 | - return false; |
|
23 | - } |
|
24 | - |
|
25 | - if (empty($this->getWarnings())) { |
|
26 | - return true; |
|
27 | - } |
|
28 | - |
|
29 | - $this->error = new RFCWarnings(); |
|
30 | - |
|
31 | - return false; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function getError() |
|
38 | - { |
|
39 | - return $this->error ?: parent::getError(); |
|
40 | - } |
|
11 | + /** |
|
12 | + * @var InvalidEmail|null |
|
13 | + */ |
|
14 | + private $error; |
|
15 | + |
|
16 | + /** |
|
17 | + * {@inheritdoc} |
|
18 | + */ |
|
19 | + public function isValid($email, EmailLexer $emailLexer) |
|
20 | + { |
|
21 | + if (!parent::isValid($email, $emailLexer)) { |
|
22 | + return false; |
|
23 | + } |
|
24 | + |
|
25 | + if (empty($this->getWarnings())) { |
|
26 | + return true; |
|
27 | + } |
|
28 | + |
|
29 | + $this->error = new RFCWarnings(); |
|
30 | + |
|
31 | + return false; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function getError() |
|
38 | + { |
|
39 | + return $this->error ?: parent::getError(); |
|
40 | + } |
|
41 | 41 | } |
@@ -6,27 +6,27 @@ |
||
6 | 6 | |
7 | 7 | class MultipleErrors extends InvalidEmail |
8 | 8 | { |
9 | - const CODE = 999; |
|
10 | - const REASON = "Accumulated errors for multiple validations"; |
|
11 | - /** |
|
12 | - * @var InvalidEmail[] |
|
13 | - */ |
|
14 | - private $errors = []; |
|
9 | + const CODE = 999; |
|
10 | + const REASON = "Accumulated errors for multiple validations"; |
|
11 | + /** |
|
12 | + * @var InvalidEmail[] |
|
13 | + */ |
|
14 | + private $errors = []; |
|
15 | 15 | |
16 | - /** |
|
17 | - * @param InvalidEmail[] $errors |
|
18 | - */ |
|
19 | - public function __construct(array $errors) |
|
20 | - { |
|
21 | - $this->errors = $errors; |
|
22 | - parent::__construct(); |
|
23 | - } |
|
16 | + /** |
|
17 | + * @param InvalidEmail[] $errors |
|
18 | + */ |
|
19 | + public function __construct(array $errors) |
|
20 | + { |
|
21 | + $this->errors = $errors; |
|
22 | + parent::__construct(); |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * @return InvalidEmail[] |
|
27 | - */ |
|
28 | - public function getErrors() |
|
29 | - { |
|
30 | - return $this->errors; |
|
31 | - } |
|
25 | + /** |
|
26 | + * @return InvalidEmail[] |
|
27 | + */ |
|
28 | + public function getErrors() |
|
29 | + { |
|
30 | + return $this->errors; |
|
31 | + } |
|
32 | 32 | } |
@@ -21,229 +21,229 @@ |
||
21 | 21 | |
22 | 22 | abstract class Parser |
23 | 23 | { |
24 | - /** |
|
25 | - * @var array |
|
26 | - */ |
|
27 | - protected $warnings = []; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var EmailLexer |
|
31 | - */ |
|
32 | - protected $lexer; |
|
33 | - |
|
34 | - /** |
|
35 | - * @var int |
|
36 | - */ |
|
37 | - protected $openedParenthesis = 0; |
|
38 | - |
|
39 | - public function __construct(EmailLexer $lexer) |
|
40 | - { |
|
41 | - $this->lexer = $lexer; |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * @return \Egulias\EmailValidator\Warning\Warning[] |
|
46 | - */ |
|
47 | - public function getWarnings() |
|
48 | - { |
|
49 | - return $this->warnings; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param string $str |
|
54 | - */ |
|
55 | - abstract public function parse($str); |
|
56 | - |
|
57 | - /** @return int */ |
|
58 | - public function getOpenedParenthesis() |
|
59 | - { |
|
60 | - return $this->openedParenthesis; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * validateQuotedPair |
|
65 | - */ |
|
66 | - protected function validateQuotedPair() |
|
67 | - { |
|
68 | - if (!($this->lexer->token['type'] === EmailLexer::INVALID |
|
69 | - || $this->lexer->token['type'] === EmailLexer::C_DEL)) { |
|
70 | - throw new ExpectingQPair(); |
|
71 | - } |
|
72 | - |
|
73 | - $this->warnings[QuotedPart::CODE] = |
|
74 | - new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
75 | - } |
|
76 | - |
|
77 | - protected function parseComments() |
|
78 | - { |
|
79 | - $this->openedParenthesis = 1; |
|
80 | - $this->isUnclosedComment(); |
|
81 | - $this->warnings[Comment::CODE] = new Comment(); |
|
82 | - while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
83 | - if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
|
84 | - $this->openedParenthesis++; |
|
85 | - } |
|
86 | - $this->warnEscaping(); |
|
87 | - $this->lexer->moveNext(); |
|
88 | - } |
|
89 | - |
|
90 | - $this->lexer->moveNext(); |
|
91 | - if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) { |
|
92 | - throw new ExpectingATEXT(); |
|
93 | - } |
|
94 | - |
|
95 | - if ($this->lexer->isNextToken(EmailLexer::S_AT)) { |
|
96 | - $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return bool |
|
102 | - */ |
|
103 | - protected function isUnclosedComment() |
|
104 | - { |
|
105 | - try { |
|
106 | - $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
|
107 | - return true; |
|
108 | - } catch (\RuntimeException $e) { |
|
109 | - throw new UnclosedComment(); |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - protected function parseFWS() |
|
114 | - { |
|
115 | - $previous = $this->lexer->getPrevious(); |
|
116 | - |
|
117 | - $this->checkCRLFInFWS(); |
|
118 | - |
|
119 | - if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
|
120 | - throw new CRNoLF(); |
|
121 | - } |
|
122 | - |
|
123 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
124 | - throw new AtextAfterCFWS(); |
|
125 | - } |
|
126 | - |
|
127 | - if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
128 | - throw new ExpectingCTEXT(); |
|
129 | - } |
|
130 | - |
|
131 | - if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
132 | - $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
133 | - } else { |
|
134 | - $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - protected function checkConsecutiveDots() |
|
139 | - { |
|
140 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
141 | - throw new ConsecutiveDot(); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return bool |
|
147 | - */ |
|
148 | - protected function isFWS() |
|
149 | - { |
|
150 | - if ($this->escaped()) { |
|
151 | - return false; |
|
152 | - } |
|
153 | - |
|
154 | - if ($this->lexer->token['type'] === EmailLexer::S_SP || |
|
155 | - $this->lexer->token['type'] === EmailLexer::S_HTAB || |
|
156 | - $this->lexer->token['type'] === EmailLexer::S_CR || |
|
157 | - $this->lexer->token['type'] === EmailLexer::S_LF || |
|
158 | - $this->lexer->token['type'] === EmailLexer::CRLF |
|
159 | - ) { |
|
160 | - return true; |
|
161 | - } |
|
162 | - |
|
163 | - return false; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * @return bool |
|
168 | - */ |
|
169 | - protected function escaped() |
|
170 | - { |
|
171 | - $previous = $this->lexer->getPrevious(); |
|
172 | - |
|
173 | - if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH |
|
174 | - && |
|
175 | - $this->lexer->token['type'] !== EmailLexer::GENERIC |
|
176 | - ) { |
|
177 | - return true; |
|
178 | - } |
|
179 | - |
|
180 | - return false; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * @return bool |
|
185 | - */ |
|
186 | - protected function warnEscaping() |
|
187 | - { |
|
188 | - if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { |
|
189 | - return false; |
|
190 | - } |
|
191 | - |
|
192 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
193 | - throw new ExpectingATEXT(); |
|
194 | - } |
|
195 | - |
|
196 | - if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
197 | - return false; |
|
198 | - } |
|
199 | - |
|
200 | - $this->warnings[QuotedPart::CODE] = |
|
201 | - new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
202 | - return true; |
|
203 | - |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param bool $hasClosingQuote |
|
208 | - * |
|
209 | - * @return bool |
|
210 | - */ |
|
211 | - protected function checkDQUOTE($hasClosingQuote) |
|
212 | - { |
|
213 | - if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) { |
|
214 | - return $hasClosingQuote; |
|
215 | - } |
|
216 | - if ($hasClosingQuote) { |
|
217 | - return $hasClosingQuote; |
|
218 | - } |
|
219 | - $previous = $this->lexer->getPrevious(); |
|
220 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) { |
|
221 | - throw new ExpectingATEXT(); |
|
222 | - } |
|
223 | - |
|
224 | - try { |
|
225 | - $this->lexer->find(EmailLexer::S_DQUOTE); |
|
226 | - $hasClosingQuote = true; |
|
227 | - } catch (\Exception $e) { |
|
228 | - throw new UnclosedQuotedString(); |
|
229 | - } |
|
230 | - $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']); |
|
231 | - |
|
232 | - return $hasClosingQuote; |
|
233 | - } |
|
234 | - |
|
235 | - protected function checkCRLFInFWS() |
|
236 | - { |
|
237 | - if ($this->lexer->token['type'] !== EmailLexer::CRLF) { |
|
238 | - return; |
|
239 | - } |
|
240 | - |
|
241 | - if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
242 | - throw new CRLFX2(); |
|
243 | - } |
|
244 | - |
|
245 | - if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
246 | - throw new CRLFAtTheEnd(); |
|
247 | - } |
|
248 | - } |
|
24 | + /** |
|
25 | + * @var array |
|
26 | + */ |
|
27 | + protected $warnings = []; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var EmailLexer |
|
31 | + */ |
|
32 | + protected $lexer; |
|
33 | + |
|
34 | + /** |
|
35 | + * @var int |
|
36 | + */ |
|
37 | + protected $openedParenthesis = 0; |
|
38 | + |
|
39 | + public function __construct(EmailLexer $lexer) |
|
40 | + { |
|
41 | + $this->lexer = $lexer; |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * @return \Egulias\EmailValidator\Warning\Warning[] |
|
46 | + */ |
|
47 | + public function getWarnings() |
|
48 | + { |
|
49 | + return $this->warnings; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param string $str |
|
54 | + */ |
|
55 | + abstract public function parse($str); |
|
56 | + |
|
57 | + /** @return int */ |
|
58 | + public function getOpenedParenthesis() |
|
59 | + { |
|
60 | + return $this->openedParenthesis; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * validateQuotedPair |
|
65 | + */ |
|
66 | + protected function validateQuotedPair() |
|
67 | + { |
|
68 | + if (!($this->lexer->token['type'] === EmailLexer::INVALID |
|
69 | + || $this->lexer->token['type'] === EmailLexer::C_DEL)) { |
|
70 | + throw new ExpectingQPair(); |
|
71 | + } |
|
72 | + |
|
73 | + $this->warnings[QuotedPart::CODE] = |
|
74 | + new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
75 | + } |
|
76 | + |
|
77 | + protected function parseComments() |
|
78 | + { |
|
79 | + $this->openedParenthesis = 1; |
|
80 | + $this->isUnclosedComment(); |
|
81 | + $this->warnings[Comment::CODE] = new Comment(); |
|
82 | + while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
83 | + if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
|
84 | + $this->openedParenthesis++; |
|
85 | + } |
|
86 | + $this->warnEscaping(); |
|
87 | + $this->lexer->moveNext(); |
|
88 | + } |
|
89 | + |
|
90 | + $this->lexer->moveNext(); |
|
91 | + if ($this->lexer->isNextTokenAny(array(EmailLexer::GENERIC, EmailLexer::S_EMPTY))) { |
|
92 | + throw new ExpectingATEXT(); |
|
93 | + } |
|
94 | + |
|
95 | + if ($this->lexer->isNextToken(EmailLexer::S_AT)) { |
|
96 | + $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return bool |
|
102 | + */ |
|
103 | + protected function isUnclosedComment() |
|
104 | + { |
|
105 | + try { |
|
106 | + $this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
|
107 | + return true; |
|
108 | + } catch (\RuntimeException $e) { |
|
109 | + throw new UnclosedComment(); |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + protected function parseFWS() |
|
114 | + { |
|
115 | + $previous = $this->lexer->getPrevious(); |
|
116 | + |
|
117 | + $this->checkCRLFInFWS(); |
|
118 | + |
|
119 | + if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
|
120 | + throw new CRNoLF(); |
|
121 | + } |
|
122 | + |
|
123 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
124 | + throw new AtextAfterCFWS(); |
|
125 | + } |
|
126 | + |
|
127 | + if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
128 | + throw new ExpectingCTEXT(); |
|
129 | + } |
|
130 | + |
|
131 | + if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
132 | + $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
133 | + } else { |
|
134 | + $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + protected function checkConsecutiveDots() |
|
139 | + { |
|
140 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
141 | + throw new ConsecutiveDot(); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return bool |
|
147 | + */ |
|
148 | + protected function isFWS() |
|
149 | + { |
|
150 | + if ($this->escaped()) { |
|
151 | + return false; |
|
152 | + } |
|
153 | + |
|
154 | + if ($this->lexer->token['type'] === EmailLexer::S_SP || |
|
155 | + $this->lexer->token['type'] === EmailLexer::S_HTAB || |
|
156 | + $this->lexer->token['type'] === EmailLexer::S_CR || |
|
157 | + $this->lexer->token['type'] === EmailLexer::S_LF || |
|
158 | + $this->lexer->token['type'] === EmailLexer::CRLF |
|
159 | + ) { |
|
160 | + return true; |
|
161 | + } |
|
162 | + |
|
163 | + return false; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * @return bool |
|
168 | + */ |
|
169 | + protected function escaped() |
|
170 | + { |
|
171 | + $previous = $this->lexer->getPrevious(); |
|
172 | + |
|
173 | + if ($previous && $previous['type'] === EmailLexer::S_BACKSLASH |
|
174 | + && |
|
175 | + $this->lexer->token['type'] !== EmailLexer::GENERIC |
|
176 | + ) { |
|
177 | + return true; |
|
178 | + } |
|
179 | + |
|
180 | + return false; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * @return bool |
|
185 | + */ |
|
186 | + protected function warnEscaping() |
|
187 | + { |
|
188 | + if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { |
|
189 | + return false; |
|
190 | + } |
|
191 | + |
|
192 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
193 | + throw new ExpectingATEXT(); |
|
194 | + } |
|
195 | + |
|
196 | + if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
197 | + return false; |
|
198 | + } |
|
199 | + |
|
200 | + $this->warnings[QuotedPart::CODE] = |
|
201 | + new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
202 | + return true; |
|
203 | + |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param bool $hasClosingQuote |
|
208 | + * |
|
209 | + * @return bool |
|
210 | + */ |
|
211 | + protected function checkDQUOTE($hasClosingQuote) |
|
212 | + { |
|
213 | + if ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE) { |
|
214 | + return $hasClosingQuote; |
|
215 | + } |
|
216 | + if ($hasClosingQuote) { |
|
217 | + return $hasClosingQuote; |
|
218 | + } |
|
219 | + $previous = $this->lexer->getPrevious(); |
|
220 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] === EmailLexer::GENERIC) { |
|
221 | + throw new ExpectingATEXT(); |
|
222 | + } |
|
223 | + |
|
224 | + try { |
|
225 | + $this->lexer->find(EmailLexer::S_DQUOTE); |
|
226 | + $hasClosingQuote = true; |
|
227 | + } catch (\Exception $e) { |
|
228 | + throw new UnclosedQuotedString(); |
|
229 | + } |
|
230 | + $this->warnings[QuotedString::CODE] = new QuotedString($previous['value'], $this->lexer->token['value']); |
|
231 | + |
|
232 | + return $hasClosingQuote; |
|
233 | + } |
|
234 | + |
|
235 | + protected function checkCRLFInFWS() |
|
236 | + { |
|
237 | + if ($this->lexer->token['type'] !== EmailLexer::CRLF) { |
|
238 | + return; |
|
239 | + } |
|
240 | + |
|
241 | + if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
242 | + throw new CRLFX2(); |
|
243 | + } |
|
244 | + |
|
245 | + if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
246 | + throw new CRLFAtTheEnd(); |
|
247 | + } |
|
248 | + } |
|
249 | 249 | } |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | throw new CRNoLF(); |
121 | 121 | } |
122 | 122 | |
123 | - if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
123 | + if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
124 | 124 | throw new AtextAfterCFWS(); |
125 | 125 | } |
126 | 126 | |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | throw new ExpectingCTEXT(); |
129 | 129 | } |
130 | 130 | |
131 | - if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
131 | + if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
132 | 132 | $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
133 | 133 | } else { |
134 | 134 | $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
@@ -14,132 +14,132 @@ |
||
14 | 14 | |
15 | 15 | class LocalPart extends Parser |
16 | 16 | { |
17 | - public function parse($localPart) |
|
18 | - { |
|
19 | - $parseDQuote = true; |
|
20 | - $closingQuote = false; |
|
21 | - $openedParenthesis = 0; |
|
22 | - $totalLength = 0; |
|
23 | - |
|
24 | - while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { |
|
25 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) { |
|
26 | - throw new DotAtStart(); |
|
27 | - } |
|
28 | - |
|
29 | - $closingQuote = $this->checkDQUOTE($closingQuote); |
|
30 | - if ($closingQuote && $parseDQuote) { |
|
31 | - $parseDQuote = $this->parseDoubleQuote(); |
|
32 | - } |
|
33 | - |
|
34 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
35 | - $this->parseComments(); |
|
36 | - $openedParenthesis += $this->getOpenedParenthesis(); |
|
37 | - } |
|
38 | - |
|
39 | - if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
40 | - if ($openedParenthesis === 0) { |
|
41 | - throw new UnopenedComment(); |
|
42 | - } |
|
43 | - |
|
44 | - $openedParenthesis--; |
|
45 | - } |
|
46 | - |
|
47 | - $this->checkConsecutiveDots(); |
|
48 | - |
|
49 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT && |
|
50 | - $this->lexer->isNextToken(EmailLexer::S_AT) |
|
51 | - ) { |
|
52 | - throw new DotAtEnd(); |
|
53 | - } |
|
54 | - |
|
55 | - $this->warnEscaping(); |
|
56 | - $this->isInvalidToken($this->lexer->token, $closingQuote); |
|
57 | - |
|
58 | - if ($this->isFWS()) { |
|
59 | - $this->parseFWS(); |
|
60 | - } |
|
61 | - |
|
62 | - $totalLength += strlen($this->lexer->token['value']); |
|
63 | - $this->lexer->moveNext(); |
|
64 | - } |
|
65 | - |
|
66 | - if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) { |
|
67 | - $this->warnings[LocalTooLong::CODE] = new LocalTooLong(); |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return bool |
|
73 | - */ |
|
74 | - protected function parseDoubleQuote() |
|
75 | - { |
|
76 | - $parseAgain = true; |
|
77 | - $special = array( |
|
78 | - EmailLexer::S_CR => true, |
|
79 | - EmailLexer::S_HTAB => true, |
|
80 | - EmailLexer::S_LF => true |
|
81 | - ); |
|
82 | - |
|
83 | - $invalid = array( |
|
84 | - EmailLexer::C_NUL => true, |
|
85 | - EmailLexer::S_HTAB => true, |
|
86 | - EmailLexer::S_CR => true, |
|
87 | - EmailLexer::S_LF => true |
|
88 | - ); |
|
89 | - $setSpecialsWarning = true; |
|
90 | - |
|
91 | - $this->lexer->moveNext(); |
|
92 | - |
|
93 | - while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) { |
|
94 | - $parseAgain = false; |
|
95 | - if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { |
|
96 | - $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
97 | - $setSpecialsWarning = false; |
|
98 | - } |
|
99 | - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) { |
|
100 | - $this->lexer->moveNext(); |
|
101 | - } |
|
102 | - |
|
103 | - $this->lexer->moveNext(); |
|
104 | - |
|
105 | - if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) { |
|
106 | - throw new ExpectingATEXT(); |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - $prev = $this->lexer->getPrevious(); |
|
111 | - |
|
112 | - if ($prev['type'] === EmailLexer::S_BACKSLASH) { |
|
113 | - if (!$this->checkDQUOTE(false)) { |
|
114 | - throw new UnclosedQuotedString(); |
|
115 | - } |
|
116 | - } |
|
117 | - |
|
118 | - if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) { |
|
119 | - throw new ExpectingAT(); |
|
120 | - } |
|
121 | - |
|
122 | - return $parseAgain; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param bool $closingQuote |
|
127 | - */ |
|
128 | - protected function isInvalidToken(array $token, $closingQuote) |
|
129 | - { |
|
130 | - $forbidden = array( |
|
131 | - EmailLexer::S_COMMA, |
|
132 | - EmailLexer::S_CLOSEBRACKET, |
|
133 | - EmailLexer::S_OPENBRACKET, |
|
134 | - EmailLexer::S_GREATERTHAN, |
|
135 | - EmailLexer::S_LOWERTHAN, |
|
136 | - EmailLexer::S_COLON, |
|
137 | - EmailLexer::S_SEMICOLON, |
|
138 | - EmailLexer::INVALID |
|
139 | - ); |
|
140 | - |
|
141 | - if (in_array($token['type'], $forbidden) && !$closingQuote) { |
|
142 | - throw new ExpectingATEXT(); |
|
143 | - } |
|
144 | - } |
|
17 | + public function parse($localPart) |
|
18 | + { |
|
19 | + $parseDQuote = true; |
|
20 | + $closingQuote = false; |
|
21 | + $openedParenthesis = 0; |
|
22 | + $totalLength = 0; |
|
23 | + |
|
24 | + while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) { |
|
25 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type']) { |
|
26 | + throw new DotAtStart(); |
|
27 | + } |
|
28 | + |
|
29 | + $closingQuote = $this->checkDQUOTE($closingQuote); |
|
30 | + if ($closingQuote && $parseDQuote) { |
|
31 | + $parseDQuote = $this->parseDoubleQuote(); |
|
32 | + } |
|
33 | + |
|
34 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
35 | + $this->parseComments(); |
|
36 | + $openedParenthesis += $this->getOpenedParenthesis(); |
|
37 | + } |
|
38 | + |
|
39 | + if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
40 | + if ($openedParenthesis === 0) { |
|
41 | + throw new UnopenedComment(); |
|
42 | + } |
|
43 | + |
|
44 | + $openedParenthesis--; |
|
45 | + } |
|
46 | + |
|
47 | + $this->checkConsecutiveDots(); |
|
48 | + |
|
49 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT && |
|
50 | + $this->lexer->isNextToken(EmailLexer::S_AT) |
|
51 | + ) { |
|
52 | + throw new DotAtEnd(); |
|
53 | + } |
|
54 | + |
|
55 | + $this->warnEscaping(); |
|
56 | + $this->isInvalidToken($this->lexer->token, $closingQuote); |
|
57 | + |
|
58 | + if ($this->isFWS()) { |
|
59 | + $this->parseFWS(); |
|
60 | + } |
|
61 | + |
|
62 | + $totalLength += strlen($this->lexer->token['value']); |
|
63 | + $this->lexer->moveNext(); |
|
64 | + } |
|
65 | + |
|
66 | + if ($totalLength > LocalTooLong::LOCAL_PART_LENGTH) { |
|
67 | + $this->warnings[LocalTooLong::CODE] = new LocalTooLong(); |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return bool |
|
73 | + */ |
|
74 | + protected function parseDoubleQuote() |
|
75 | + { |
|
76 | + $parseAgain = true; |
|
77 | + $special = array( |
|
78 | + EmailLexer::S_CR => true, |
|
79 | + EmailLexer::S_HTAB => true, |
|
80 | + EmailLexer::S_LF => true |
|
81 | + ); |
|
82 | + |
|
83 | + $invalid = array( |
|
84 | + EmailLexer::C_NUL => true, |
|
85 | + EmailLexer::S_HTAB => true, |
|
86 | + EmailLexer::S_CR => true, |
|
87 | + EmailLexer::S_LF => true |
|
88 | + ); |
|
89 | + $setSpecialsWarning = true; |
|
90 | + |
|
91 | + $this->lexer->moveNext(); |
|
92 | + |
|
93 | + while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && null !== $this->lexer->token['type']) { |
|
94 | + $parseAgain = false; |
|
95 | + if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) { |
|
96 | + $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
97 | + $setSpecialsWarning = false; |
|
98 | + } |
|
99 | + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) { |
|
100 | + $this->lexer->moveNext(); |
|
101 | + } |
|
102 | + |
|
103 | + $this->lexer->moveNext(); |
|
104 | + |
|
105 | + if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) { |
|
106 | + throw new ExpectingATEXT(); |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + $prev = $this->lexer->getPrevious(); |
|
111 | + |
|
112 | + if ($prev['type'] === EmailLexer::S_BACKSLASH) { |
|
113 | + if (!$this->checkDQUOTE(false)) { |
|
114 | + throw new UnclosedQuotedString(); |
|
115 | + } |
|
116 | + } |
|
117 | + |
|
118 | + if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) { |
|
119 | + throw new ExpectingAT(); |
|
120 | + } |
|
121 | + |
|
122 | + return $parseAgain; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param bool $closingQuote |
|
127 | + */ |
|
128 | + protected function isInvalidToken(array $token, $closingQuote) |
|
129 | + { |
|
130 | + $forbidden = array( |
|
131 | + EmailLexer::S_COMMA, |
|
132 | + EmailLexer::S_CLOSEBRACKET, |
|
133 | + EmailLexer::S_OPENBRACKET, |
|
134 | + EmailLexer::S_GREATERTHAN, |
|
135 | + EmailLexer::S_LOWERTHAN, |
|
136 | + EmailLexer::S_COLON, |
|
137 | + EmailLexer::S_SEMICOLON, |
|
138 | + EmailLexer::INVALID |
|
139 | + ); |
|
140 | + |
|
141 | + if (in_array($token['type'], $forbidden) && !$closingQuote) { |
|
142 | + throw new ExpectingATEXT(); |
|
143 | + } |
|
144 | + } |
|
145 | 145 | } |
@@ -34,410 +34,410 @@ |
||
34 | 34 | |
35 | 35 | class DomainPart extends Parser |
36 | 36 | { |
37 | - const DOMAIN_MAX_LENGTH = 254; |
|
38 | - const LABEL_MAX_LENGTH = 63; |
|
39 | - |
|
40 | - /** |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - protected $domainPart = ''; |
|
44 | - |
|
45 | - public function parse($domainPart) |
|
46 | - { |
|
47 | - $this->lexer->moveNext(); |
|
48 | - |
|
49 | - $this->performDomainStartChecks(); |
|
50 | - |
|
51 | - $domain = $this->doParseDomainPart(); |
|
52 | - |
|
53 | - $prev = $this->lexer->getPrevious(); |
|
54 | - $length = strlen($domain); |
|
55 | - |
|
56 | - if ($prev['type'] === EmailLexer::S_DOT) { |
|
57 | - throw new DotAtEnd(); |
|
58 | - } |
|
59 | - if ($prev['type'] === EmailLexer::S_HYPHEN) { |
|
60 | - throw new DomainHyphened(); |
|
61 | - } |
|
62 | - if ($length > self::DOMAIN_MAX_LENGTH) { |
|
63 | - $this->warnings[DomainTooLong::CODE] = new DomainTooLong(); |
|
64 | - } |
|
65 | - if ($prev['type'] === EmailLexer::S_CR) { |
|
66 | - throw new CRLFAtTheEnd(); |
|
67 | - } |
|
68 | - $this->domainPart = $domain; |
|
69 | - } |
|
70 | - |
|
71 | - private function performDomainStartChecks() |
|
72 | - { |
|
73 | - $this->checkInvalidTokensAfterAT(); |
|
74 | - $this->checkEmptyDomain(); |
|
75 | - |
|
76 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
77 | - $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); |
|
78 | - $this->parseDomainComments(); |
|
79 | - } |
|
80 | - } |
|
81 | - |
|
82 | - private function checkEmptyDomain() |
|
83 | - { |
|
84 | - $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY || |
|
85 | - ($this->lexer->token['type'] === EmailLexer::S_SP && |
|
86 | - !$this->lexer->isNextToken(EmailLexer::GENERIC)); |
|
87 | - |
|
88 | - if ($thereIsNoDomain) { |
|
89 | - throw new NoDomainPart(); |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - private function checkInvalidTokensAfterAT() |
|
94 | - { |
|
95 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
96 | - throw new DotAtStart(); |
|
97 | - } |
|
98 | - if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) { |
|
99 | - throw new DomainHyphened(); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function getDomainPart() |
|
107 | - { |
|
108 | - return $this->domainPart; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param string $addressLiteral |
|
113 | - * @param int $maxGroups |
|
114 | - */ |
|
115 | - public function checkIPV6Tag($addressLiteral, $maxGroups = 8) |
|
116 | - { |
|
117 | - $prev = $this->lexer->getPrevious(); |
|
118 | - if ($prev['type'] === EmailLexer::S_COLON) { |
|
119 | - $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd(); |
|
120 | - } |
|
121 | - |
|
122 | - $IPv6 = substr($addressLiteral, 5); |
|
123 | - //Daniel Marschall's new IPv6 testing strategy |
|
124 | - $matchesIP = explode(':', $IPv6); |
|
125 | - $groupCount = count($matchesIP); |
|
126 | - $colons = strpos($IPv6, '::'); |
|
127 | - |
|
128 | - if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) { |
|
129 | - $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar(); |
|
130 | - } |
|
131 | - |
|
132 | - if ($colons === false) { |
|
133 | - // We need exactly the right number of groups |
|
134 | - if ($groupCount !== $maxGroups) { |
|
135 | - $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount(); |
|
136 | - } |
|
137 | - return; |
|
138 | - } |
|
139 | - |
|
140 | - if ($colons !== strrpos($IPv6, '::')) { |
|
141 | - $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon(); |
|
142 | - return; |
|
143 | - } |
|
144 | - |
|
145 | - if ($colons === 0 || $colons === (strlen($IPv6) - 2)) { |
|
146 | - // RFC 4291 allows :: at the start or end of an address |
|
147 | - //with 7 other groups in addition |
|
148 | - ++$maxGroups; |
|
149 | - } |
|
150 | - |
|
151 | - if ($groupCount > $maxGroups) { |
|
152 | - $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups(); |
|
153 | - } elseif ($groupCount === $maxGroups) { |
|
154 | - $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated(); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - protected function doParseDomainPart() |
|
162 | - { |
|
163 | - $domain = ''; |
|
164 | - $label = ''; |
|
165 | - $openedParenthesis = 0; |
|
166 | - do { |
|
167 | - $prev = $this->lexer->getPrevious(); |
|
168 | - |
|
169 | - $this->checkNotAllowedChars($this->lexer->token); |
|
170 | - |
|
171 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
172 | - $this->parseComments(); |
|
173 | - $openedParenthesis += $this->getOpenedParenthesis(); |
|
174 | - $this->lexer->moveNext(); |
|
175 | - $tmpPrev = $this->lexer->getPrevious(); |
|
176 | - if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
177 | - $openedParenthesis--; |
|
178 | - } |
|
179 | - } |
|
180 | - if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
181 | - if ($openedParenthesis === 0) { |
|
182 | - throw new UnopenedComment(); |
|
183 | - } else { |
|
184 | - $openedParenthesis--; |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - $this->checkConsecutiveDots(); |
|
189 | - $this->checkDomainPartExceptions($prev); |
|
190 | - |
|
191 | - if ($this->hasBrackets()) { |
|
192 | - $this->parseDomainLiteral(); |
|
193 | - } |
|
194 | - |
|
195 | - if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
196 | - $this->checkLabelLength($label); |
|
197 | - $label = ''; |
|
198 | - } else { |
|
199 | - $label .= $this->lexer->token['value']; |
|
200 | - } |
|
201 | - |
|
202 | - if ($this->isFWS()) { |
|
203 | - $this->parseFWS(); |
|
204 | - } |
|
205 | - |
|
206 | - $domain .= $this->lexer->token['value']; |
|
207 | - $this->lexer->moveNext(); |
|
208 | - if ($this->lexer->token['type'] === EmailLexer::S_SP) { |
|
209 | - throw new CharNotAllowed(); |
|
210 | - } |
|
211 | - } while (null !== $this->lexer->token['type']); |
|
212 | - |
|
213 | - $this->checkLabelLength($label); |
|
214 | - |
|
215 | - return $domain; |
|
216 | - } |
|
217 | - |
|
218 | - private function checkNotAllowedChars(array $token) |
|
219 | - { |
|
220 | - $notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true]; |
|
221 | - if (isset($notAllowed[$token['type']])) { |
|
222 | - throw new CharNotAllowed(); |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @return string|false |
|
228 | - */ |
|
229 | - protected function parseDomainLiteral() |
|
230 | - { |
|
231 | - if ($this->lexer->isNextToken(EmailLexer::S_COLON)) { |
|
232 | - $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
233 | - } |
|
234 | - if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) { |
|
235 | - $lexer = clone $this->lexer; |
|
236 | - $lexer->moveNext(); |
|
237 | - if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) { |
|
238 | - $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - return $this->doParseDomainLiteral(); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * @return string|false |
|
247 | - */ |
|
248 | - protected function doParseDomainLiteral() |
|
249 | - { |
|
250 | - $IPv6TAG = false; |
|
251 | - $addressLiteral = ''; |
|
252 | - do { |
|
253 | - if ($this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
254 | - throw new ExpectingDTEXT(); |
|
255 | - } |
|
256 | - |
|
257 | - if ($this->lexer->token['type'] === EmailLexer::INVALID || |
|
258 | - $this->lexer->token['type'] === EmailLexer::C_DEL || |
|
259 | - $this->lexer->token['type'] === EmailLexer::S_LF |
|
260 | - ) { |
|
261 | - $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
262 | - } |
|
263 | - |
|
264 | - if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENQBRACKET, EmailLexer::S_OPENBRACKET))) { |
|
265 | - throw new ExpectingDTEXT(); |
|
266 | - } |
|
267 | - |
|
268 | - if ($this->lexer->isNextTokenAny( |
|
269 | - array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF) |
|
270 | - )) { |
|
271 | - $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
272 | - $this->parseFWS(); |
|
273 | - } |
|
274 | - |
|
275 | - if ($this->lexer->isNextToken(EmailLexer::S_CR)) { |
|
276 | - throw new CRNoLF(); |
|
277 | - } |
|
278 | - |
|
279 | - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) { |
|
280 | - $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
281 | - $addressLiteral .= $this->lexer->token['value']; |
|
282 | - $this->lexer->moveNext(); |
|
283 | - $this->validateQuotedPair(); |
|
284 | - } |
|
285 | - if ($this->lexer->token['type'] === EmailLexer::S_IPV6TAG) { |
|
286 | - $IPv6TAG = true; |
|
287 | - } |
|
288 | - if ($this->lexer->token['type'] === EmailLexer::S_CLOSEQBRACKET) { |
|
289 | - break; |
|
290 | - } |
|
291 | - |
|
292 | - $addressLiteral .= $this->lexer->token['value']; |
|
293 | - |
|
294 | - } while ($this->lexer->moveNext()); |
|
295 | - |
|
296 | - $addressLiteral = str_replace('[', '', $addressLiteral); |
|
297 | - $addressLiteral = $this->checkIPV4Tag($addressLiteral); |
|
298 | - |
|
299 | - if (false === $addressLiteral) { |
|
300 | - return $addressLiteral; |
|
301 | - } |
|
302 | - |
|
303 | - if (!$IPv6TAG) { |
|
304 | - $this->warnings[DomainLiteral::CODE] = new DomainLiteral(); |
|
305 | - return $addressLiteral; |
|
306 | - } |
|
307 | - |
|
308 | - $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
309 | - |
|
310 | - $this->checkIPV6Tag($addressLiteral); |
|
311 | - |
|
312 | - return $addressLiteral; |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * @param string $addressLiteral |
|
317 | - * |
|
318 | - * @return string|false |
|
319 | - */ |
|
320 | - protected function checkIPV4Tag($addressLiteral) |
|
321 | - { |
|
322 | - $matchesIP = array(); |
|
323 | - |
|
324 | - // Extract IPv4 part from the end of the address-literal (if there is one) |
|
325 | - if (preg_match( |
|
326 | - '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', |
|
327 | - $addressLiteral, |
|
328 | - $matchesIP |
|
329 | - ) > 0 |
|
330 | - ) { |
|
331 | - $index = strrpos($addressLiteral, $matchesIP[0]); |
|
332 | - if ($index === 0) { |
|
333 | - $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
334 | - return false; |
|
335 | - } |
|
336 | - // Convert IPv4 part to IPv6 format for further testing |
|
337 | - $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0'; |
|
338 | - } |
|
339 | - |
|
340 | - return $addressLiteral; |
|
341 | - } |
|
342 | - |
|
343 | - protected function checkDomainPartExceptions(array $prev) |
|
344 | - { |
|
345 | - $invalidDomainTokens = array( |
|
346 | - EmailLexer::S_DQUOTE => true, |
|
347 | - EmailLexer::S_SQUOTE => true, |
|
348 | - EmailLexer::S_BACKTICK => true, |
|
349 | - EmailLexer::S_SEMICOLON => true, |
|
350 | - EmailLexer::S_GREATERTHAN => true, |
|
351 | - EmailLexer::S_LOWERTHAN => true, |
|
352 | - ); |
|
353 | - |
|
354 | - if (isset($invalidDomainTokens[$this->lexer->token['type']])) { |
|
355 | - throw new ExpectingATEXT(); |
|
356 | - } |
|
357 | - |
|
358 | - if ($this->lexer->token['type'] === EmailLexer::S_COMMA) { |
|
359 | - throw new CommaInDomain(); |
|
360 | - } |
|
361 | - |
|
362 | - if ($this->lexer->token['type'] === EmailLexer::S_AT) { |
|
363 | - throw new ConsecutiveAt(); |
|
364 | - } |
|
365 | - |
|
366 | - if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) { |
|
367 | - throw new ExpectingATEXT(); |
|
368 | - } |
|
369 | - |
|
370 | - if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
371 | - throw new DomainHyphened(); |
|
372 | - } |
|
373 | - |
|
374 | - if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH |
|
375 | - && $this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
376 | - throw new ExpectingATEXT(); |
|
377 | - } |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * @return bool |
|
382 | - */ |
|
383 | - protected function hasBrackets() |
|
384 | - { |
|
385 | - if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) { |
|
386 | - return false; |
|
387 | - } |
|
388 | - |
|
389 | - try { |
|
390 | - $this->lexer->find(EmailLexer::S_CLOSEBRACKET); |
|
391 | - } catch (\RuntimeException $e) { |
|
392 | - throw new ExpectingDomainLiteralClose(); |
|
393 | - } |
|
394 | - |
|
395 | - return true; |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * @param string $label |
|
400 | - */ |
|
401 | - protected function checkLabelLength($label) |
|
402 | - { |
|
403 | - if ($this->isLabelTooLong($label)) { |
|
404 | - $this->warnings[LabelTooLong::CODE] = new LabelTooLong(); |
|
405 | - } |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * @param string $label |
|
410 | - * @return bool |
|
411 | - */ |
|
412 | - private function isLabelTooLong($label) |
|
413 | - { |
|
414 | - if (preg_match('/[^\x00-\x7F]/', $label)) { |
|
415 | - idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
|
416 | - |
|
417 | - return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG); |
|
418 | - } |
|
419 | - |
|
420 | - return strlen($label) > self::LABEL_MAX_LENGTH; |
|
421 | - } |
|
422 | - |
|
423 | - protected function parseDomainComments() |
|
424 | - { |
|
425 | - $this->isUnclosedComment(); |
|
426 | - while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
427 | - $this->warnEscaping(); |
|
428 | - $this->lexer->moveNext(); |
|
429 | - } |
|
430 | - |
|
431 | - $this->lexer->moveNext(); |
|
432 | - if ($this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
433 | - throw new ExpectingATEXT(); |
|
434 | - } |
|
435 | - } |
|
436 | - |
|
437 | - protected function addTLDWarnings() |
|
438 | - { |
|
439 | - if ($this->warnings[DomainLiteral::CODE]) { |
|
440 | - $this->warnings[TLD::CODE] = new TLD(); |
|
441 | - } |
|
442 | - } |
|
37 | + const DOMAIN_MAX_LENGTH = 254; |
|
38 | + const LABEL_MAX_LENGTH = 63; |
|
39 | + |
|
40 | + /** |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + protected $domainPart = ''; |
|
44 | + |
|
45 | + public function parse($domainPart) |
|
46 | + { |
|
47 | + $this->lexer->moveNext(); |
|
48 | + |
|
49 | + $this->performDomainStartChecks(); |
|
50 | + |
|
51 | + $domain = $this->doParseDomainPart(); |
|
52 | + |
|
53 | + $prev = $this->lexer->getPrevious(); |
|
54 | + $length = strlen($domain); |
|
55 | + |
|
56 | + if ($prev['type'] === EmailLexer::S_DOT) { |
|
57 | + throw new DotAtEnd(); |
|
58 | + } |
|
59 | + if ($prev['type'] === EmailLexer::S_HYPHEN) { |
|
60 | + throw new DomainHyphened(); |
|
61 | + } |
|
62 | + if ($length > self::DOMAIN_MAX_LENGTH) { |
|
63 | + $this->warnings[DomainTooLong::CODE] = new DomainTooLong(); |
|
64 | + } |
|
65 | + if ($prev['type'] === EmailLexer::S_CR) { |
|
66 | + throw new CRLFAtTheEnd(); |
|
67 | + } |
|
68 | + $this->domainPart = $domain; |
|
69 | + } |
|
70 | + |
|
71 | + private function performDomainStartChecks() |
|
72 | + { |
|
73 | + $this->checkInvalidTokensAfterAT(); |
|
74 | + $this->checkEmptyDomain(); |
|
75 | + |
|
76 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
77 | + $this->warnings[DeprecatedComment::CODE] = new DeprecatedComment(); |
|
78 | + $this->parseDomainComments(); |
|
79 | + } |
|
80 | + } |
|
81 | + |
|
82 | + private function checkEmptyDomain() |
|
83 | + { |
|
84 | + $thereIsNoDomain = $this->lexer->token['type'] === EmailLexer::S_EMPTY || |
|
85 | + ($this->lexer->token['type'] === EmailLexer::S_SP && |
|
86 | + !$this->lexer->isNextToken(EmailLexer::GENERIC)); |
|
87 | + |
|
88 | + if ($thereIsNoDomain) { |
|
89 | + throw new NoDomainPart(); |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + private function checkInvalidTokensAfterAT() |
|
94 | + { |
|
95 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
96 | + throw new DotAtStart(); |
|
97 | + } |
|
98 | + if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN) { |
|
99 | + throw new DomainHyphened(); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function getDomainPart() |
|
107 | + { |
|
108 | + return $this->domainPart; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param string $addressLiteral |
|
113 | + * @param int $maxGroups |
|
114 | + */ |
|
115 | + public function checkIPV6Tag($addressLiteral, $maxGroups = 8) |
|
116 | + { |
|
117 | + $prev = $this->lexer->getPrevious(); |
|
118 | + if ($prev['type'] === EmailLexer::S_COLON) { |
|
119 | + $this->warnings[IPV6ColonEnd::CODE] = new IPV6ColonEnd(); |
|
120 | + } |
|
121 | + |
|
122 | + $IPv6 = substr($addressLiteral, 5); |
|
123 | + //Daniel Marschall's new IPv6 testing strategy |
|
124 | + $matchesIP = explode(':', $IPv6); |
|
125 | + $groupCount = count($matchesIP); |
|
126 | + $colons = strpos($IPv6, '::'); |
|
127 | + |
|
128 | + if (count(preg_grep('/^[0-9A-Fa-f]{0,4}$/', $matchesIP, PREG_GREP_INVERT)) !== 0) { |
|
129 | + $this->warnings[IPV6BadChar::CODE] = new IPV6BadChar(); |
|
130 | + } |
|
131 | + |
|
132 | + if ($colons === false) { |
|
133 | + // We need exactly the right number of groups |
|
134 | + if ($groupCount !== $maxGroups) { |
|
135 | + $this->warnings[IPV6GroupCount::CODE] = new IPV6GroupCount(); |
|
136 | + } |
|
137 | + return; |
|
138 | + } |
|
139 | + |
|
140 | + if ($colons !== strrpos($IPv6, '::')) { |
|
141 | + $this->warnings[IPV6DoubleColon::CODE] = new IPV6DoubleColon(); |
|
142 | + return; |
|
143 | + } |
|
144 | + |
|
145 | + if ($colons === 0 || $colons === (strlen($IPv6) - 2)) { |
|
146 | + // RFC 4291 allows :: at the start or end of an address |
|
147 | + //with 7 other groups in addition |
|
148 | + ++$maxGroups; |
|
149 | + } |
|
150 | + |
|
151 | + if ($groupCount > $maxGroups) { |
|
152 | + $this->warnings[IPV6MaxGroups::CODE] = new IPV6MaxGroups(); |
|
153 | + } elseif ($groupCount === $maxGroups) { |
|
154 | + $this->warnings[IPV6Deprecated::CODE] = new IPV6Deprecated(); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + protected function doParseDomainPart() |
|
162 | + { |
|
163 | + $domain = ''; |
|
164 | + $label = ''; |
|
165 | + $openedParenthesis = 0; |
|
166 | + do { |
|
167 | + $prev = $this->lexer->getPrevious(); |
|
168 | + |
|
169 | + $this->checkNotAllowedChars($this->lexer->token); |
|
170 | + |
|
171 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
172 | + $this->parseComments(); |
|
173 | + $openedParenthesis += $this->getOpenedParenthesis(); |
|
174 | + $this->lexer->moveNext(); |
|
175 | + $tmpPrev = $this->lexer->getPrevious(); |
|
176 | + if ($tmpPrev['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
177 | + $openedParenthesis--; |
|
178 | + } |
|
179 | + } |
|
180 | + if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
181 | + if ($openedParenthesis === 0) { |
|
182 | + throw new UnopenedComment(); |
|
183 | + } else { |
|
184 | + $openedParenthesis--; |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + $this->checkConsecutiveDots(); |
|
189 | + $this->checkDomainPartExceptions($prev); |
|
190 | + |
|
191 | + if ($this->hasBrackets()) { |
|
192 | + $this->parseDomainLiteral(); |
|
193 | + } |
|
194 | + |
|
195 | + if ($this->lexer->token['type'] === EmailLexer::S_DOT) { |
|
196 | + $this->checkLabelLength($label); |
|
197 | + $label = ''; |
|
198 | + } else { |
|
199 | + $label .= $this->lexer->token['value']; |
|
200 | + } |
|
201 | + |
|
202 | + if ($this->isFWS()) { |
|
203 | + $this->parseFWS(); |
|
204 | + } |
|
205 | + |
|
206 | + $domain .= $this->lexer->token['value']; |
|
207 | + $this->lexer->moveNext(); |
|
208 | + if ($this->lexer->token['type'] === EmailLexer::S_SP) { |
|
209 | + throw new CharNotAllowed(); |
|
210 | + } |
|
211 | + } while (null !== $this->lexer->token['type']); |
|
212 | + |
|
213 | + $this->checkLabelLength($label); |
|
214 | + |
|
215 | + return $domain; |
|
216 | + } |
|
217 | + |
|
218 | + private function checkNotAllowedChars(array $token) |
|
219 | + { |
|
220 | + $notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true]; |
|
221 | + if (isset($notAllowed[$token['type']])) { |
|
222 | + throw new CharNotAllowed(); |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @return string|false |
|
228 | + */ |
|
229 | + protected function parseDomainLiteral() |
|
230 | + { |
|
231 | + if ($this->lexer->isNextToken(EmailLexer::S_COLON)) { |
|
232 | + $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
233 | + } |
|
234 | + if ($this->lexer->isNextToken(EmailLexer::S_IPV6TAG)) { |
|
235 | + $lexer = clone $this->lexer; |
|
236 | + $lexer->moveNext(); |
|
237 | + if ($lexer->isNextToken(EmailLexer::S_DOUBLECOLON)) { |
|
238 | + $this->warnings[IPV6ColonStart::CODE] = new IPV6ColonStart(); |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + return $this->doParseDomainLiteral(); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * @return string|false |
|
247 | + */ |
|
248 | + protected function doParseDomainLiteral() |
|
249 | + { |
|
250 | + $IPv6TAG = false; |
|
251 | + $addressLiteral = ''; |
|
252 | + do { |
|
253 | + if ($this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
254 | + throw new ExpectingDTEXT(); |
|
255 | + } |
|
256 | + |
|
257 | + if ($this->lexer->token['type'] === EmailLexer::INVALID || |
|
258 | + $this->lexer->token['type'] === EmailLexer::C_DEL || |
|
259 | + $this->lexer->token['type'] === EmailLexer::S_LF |
|
260 | + ) { |
|
261 | + $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
262 | + } |
|
263 | + |
|
264 | + if ($this->lexer->isNextTokenAny(array(EmailLexer::S_OPENQBRACKET, EmailLexer::S_OPENBRACKET))) { |
|
265 | + throw new ExpectingDTEXT(); |
|
266 | + } |
|
267 | + |
|
268 | + if ($this->lexer->isNextTokenAny( |
|
269 | + array(EmailLexer::S_HTAB, EmailLexer::S_SP, $this->lexer->token['type'] === EmailLexer::CRLF) |
|
270 | + )) { |
|
271 | + $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
272 | + $this->parseFWS(); |
|
273 | + } |
|
274 | + |
|
275 | + if ($this->lexer->isNextToken(EmailLexer::S_CR)) { |
|
276 | + throw new CRNoLF(); |
|
277 | + } |
|
278 | + |
|
279 | + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH) { |
|
280 | + $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
|
281 | + $addressLiteral .= $this->lexer->token['value']; |
|
282 | + $this->lexer->moveNext(); |
|
283 | + $this->validateQuotedPair(); |
|
284 | + } |
|
285 | + if ($this->lexer->token['type'] === EmailLexer::S_IPV6TAG) { |
|
286 | + $IPv6TAG = true; |
|
287 | + } |
|
288 | + if ($this->lexer->token['type'] === EmailLexer::S_CLOSEQBRACKET) { |
|
289 | + break; |
|
290 | + } |
|
291 | + |
|
292 | + $addressLiteral .= $this->lexer->token['value']; |
|
293 | + |
|
294 | + } while ($this->lexer->moveNext()); |
|
295 | + |
|
296 | + $addressLiteral = str_replace('[', '', $addressLiteral); |
|
297 | + $addressLiteral = $this->checkIPV4Tag($addressLiteral); |
|
298 | + |
|
299 | + if (false === $addressLiteral) { |
|
300 | + return $addressLiteral; |
|
301 | + } |
|
302 | + |
|
303 | + if (!$IPv6TAG) { |
|
304 | + $this->warnings[DomainLiteral::CODE] = new DomainLiteral(); |
|
305 | + return $addressLiteral; |
|
306 | + } |
|
307 | + |
|
308 | + $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
309 | + |
|
310 | + $this->checkIPV6Tag($addressLiteral); |
|
311 | + |
|
312 | + return $addressLiteral; |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * @param string $addressLiteral |
|
317 | + * |
|
318 | + * @return string|false |
|
319 | + */ |
|
320 | + protected function checkIPV4Tag($addressLiteral) |
|
321 | + { |
|
322 | + $matchesIP = array(); |
|
323 | + |
|
324 | + // Extract IPv4 part from the end of the address-literal (if there is one) |
|
325 | + if (preg_match( |
|
326 | + '/\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/', |
|
327 | + $addressLiteral, |
|
328 | + $matchesIP |
|
329 | + ) > 0 |
|
330 | + ) { |
|
331 | + $index = strrpos($addressLiteral, $matchesIP[0]); |
|
332 | + if ($index === 0) { |
|
333 | + $this->warnings[AddressLiteral::CODE] = new AddressLiteral(); |
|
334 | + return false; |
|
335 | + } |
|
336 | + // Convert IPv4 part to IPv6 format for further testing |
|
337 | + $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0'; |
|
338 | + } |
|
339 | + |
|
340 | + return $addressLiteral; |
|
341 | + } |
|
342 | + |
|
343 | + protected function checkDomainPartExceptions(array $prev) |
|
344 | + { |
|
345 | + $invalidDomainTokens = array( |
|
346 | + EmailLexer::S_DQUOTE => true, |
|
347 | + EmailLexer::S_SQUOTE => true, |
|
348 | + EmailLexer::S_BACKTICK => true, |
|
349 | + EmailLexer::S_SEMICOLON => true, |
|
350 | + EmailLexer::S_GREATERTHAN => true, |
|
351 | + EmailLexer::S_LOWERTHAN => true, |
|
352 | + ); |
|
353 | + |
|
354 | + if (isset($invalidDomainTokens[$this->lexer->token['type']])) { |
|
355 | + throw new ExpectingATEXT(); |
|
356 | + } |
|
357 | + |
|
358 | + if ($this->lexer->token['type'] === EmailLexer::S_COMMA) { |
|
359 | + throw new CommaInDomain(); |
|
360 | + } |
|
361 | + |
|
362 | + if ($this->lexer->token['type'] === EmailLexer::S_AT) { |
|
363 | + throw new ConsecutiveAt(); |
|
364 | + } |
|
365 | + |
|
366 | + if ($this->lexer->token['type'] === EmailLexer::S_OPENQBRACKET && $prev['type'] !== EmailLexer::S_AT) { |
|
367 | + throw new ExpectingATEXT(); |
|
368 | + } |
|
369 | + |
|
370 | + if ($this->lexer->token['type'] === EmailLexer::S_HYPHEN && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
371 | + throw new DomainHyphened(); |
|
372 | + } |
|
373 | + |
|
374 | + if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH |
|
375 | + && $this->lexer->isNextToken(EmailLexer::GENERIC)) { |
|
376 | + throw new ExpectingATEXT(); |
|
377 | + } |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * @return bool |
|
382 | + */ |
|
383 | + protected function hasBrackets() |
|
384 | + { |
|
385 | + if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) { |
|
386 | + return false; |
|
387 | + } |
|
388 | + |
|
389 | + try { |
|
390 | + $this->lexer->find(EmailLexer::S_CLOSEBRACKET); |
|
391 | + } catch (\RuntimeException $e) { |
|
392 | + throw new ExpectingDomainLiteralClose(); |
|
393 | + } |
|
394 | + |
|
395 | + return true; |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * @param string $label |
|
400 | + */ |
|
401 | + protected function checkLabelLength($label) |
|
402 | + { |
|
403 | + if ($this->isLabelTooLong($label)) { |
|
404 | + $this->warnings[LabelTooLong::CODE] = new LabelTooLong(); |
|
405 | + } |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * @param string $label |
|
410 | + * @return bool |
|
411 | + */ |
|
412 | + private function isLabelTooLong($label) |
|
413 | + { |
|
414 | + if (preg_match('/[^\x00-\x7F]/', $label)) { |
|
415 | + idn_to_ascii($label, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46, $idnaInfo); |
|
416 | + |
|
417 | + return (bool) ($idnaInfo['errors'] & IDNA_ERROR_LABEL_TOO_LONG); |
|
418 | + } |
|
419 | + |
|
420 | + return strlen($label) > self::LABEL_MAX_LENGTH; |
|
421 | + } |
|
422 | + |
|
423 | + protected function parseDomainComments() |
|
424 | + { |
|
425 | + $this->isUnclosedComment(); |
|
426 | + while (!$this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
427 | + $this->warnEscaping(); |
|
428 | + $this->lexer->moveNext(); |
|
429 | + } |
|
430 | + |
|
431 | + $this->lexer->moveNext(); |
|
432 | + if ($this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
433 | + throw new ExpectingATEXT(); |
|
434 | + } |
|
435 | + } |
|
436 | + |
|
437 | + protected function addTLDWarnings() |
|
438 | + { |
|
439 | + if ($this->warnings[DomainLiteral::CODE]) { |
|
440 | + $this->warnings[TLD::CODE] = new TLD(); |
|
441 | + } |
|
442 | + } |
|
443 | 443 | } |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | } |
256 | 256 | |
257 | 257 | if ($this->lexer->token['type'] === EmailLexer::INVALID || |
258 | - $this->lexer->token['type'] === EmailLexer::C_DEL || |
|
258 | + $this->lexer->token['type'] === EmailLexer::C_DEL || |
|
259 | 259 | $this->lexer->token['type'] === EmailLexer::S_LF |
260 | 260 | ) { |
261 | 261 | $this->warnings[ObsoleteDTEXT::CODE] = new ObsoleteDTEXT(); |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | */ |
320 | 320 | protected function checkIPV4Tag($addressLiteral) |
321 | 321 | { |
322 | - $matchesIP = array(); |
|
322 | + $matchesIP = array(); |
|
323 | 323 | |
324 | 324 | // Extract IPv4 part from the end of the address-literal (if there is one) |
325 | 325 | if (preg_match( |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | return false; |
335 | 335 | } |
336 | 336 | // Convert IPv4 part to IPv6 format for further testing |
337 | - $addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0'; |
|
337 | + $addressLiteral = substr($addressLiteral, 0, (int) $index).'0:0'; |
|
338 | 338 | } |
339 | 339 | |
340 | 340 | return $addressLiteral; |
@@ -7,61 +7,61 @@ |
||
7 | 7 | |
8 | 8 | class EmailValidator |
9 | 9 | { |
10 | - /** |
|
11 | - * @var EmailLexer |
|
12 | - */ |
|
13 | - private $lexer; |
|
10 | + /** |
|
11 | + * @var EmailLexer |
|
12 | + */ |
|
13 | + private $lexer; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @var Warning\Warning[] |
|
17 | - */ |
|
18 | - protected $warnings = []; |
|
15 | + /** |
|
16 | + * @var Warning\Warning[] |
|
17 | + */ |
|
18 | + protected $warnings = []; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @var InvalidEmail|null |
|
22 | - */ |
|
23 | - protected $error; |
|
20 | + /** |
|
21 | + * @var InvalidEmail|null |
|
22 | + */ |
|
23 | + protected $error; |
|
24 | 24 | |
25 | - public function __construct() |
|
26 | - { |
|
27 | - $this->lexer = new EmailLexer(); |
|
28 | - } |
|
25 | + public function __construct() |
|
26 | + { |
|
27 | + $this->lexer = new EmailLexer(); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $email |
|
32 | - * @param EmailValidation $emailValidation |
|
33 | - * @return bool |
|
34 | - */ |
|
35 | - public function isValid($email, EmailValidation $emailValidation) |
|
36 | - { |
|
37 | - $isValid = $emailValidation->isValid($email, $this->lexer); |
|
38 | - $this->warnings = $emailValidation->getWarnings(); |
|
39 | - $this->error = $emailValidation->getError(); |
|
30 | + /** |
|
31 | + * @param string $email |
|
32 | + * @param EmailValidation $emailValidation |
|
33 | + * @return bool |
|
34 | + */ |
|
35 | + public function isValid($email, EmailValidation $emailValidation) |
|
36 | + { |
|
37 | + $isValid = $emailValidation->isValid($email, $this->lexer); |
|
38 | + $this->warnings = $emailValidation->getWarnings(); |
|
39 | + $this->error = $emailValidation->getError(); |
|
40 | 40 | |
41 | - return $isValid; |
|
42 | - } |
|
41 | + return $isValid; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * @return boolean |
|
46 | - */ |
|
47 | - public function hasWarnings() |
|
48 | - { |
|
49 | - return !empty($this->warnings); |
|
50 | - } |
|
44 | + /** |
|
45 | + * @return boolean |
|
46 | + */ |
|
47 | + public function hasWarnings() |
|
48 | + { |
|
49 | + return !empty($this->warnings); |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @return array |
|
54 | - */ |
|
55 | - public function getWarnings() |
|
56 | - { |
|
57 | - return $this->warnings; |
|
58 | - } |
|
52 | + /** |
|
53 | + * @return array |
|
54 | + */ |
|
55 | + public function getWarnings() |
|
56 | + { |
|
57 | + return $this->warnings; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * @return InvalidEmail|null |
|
62 | - */ |
|
63 | - public function getError() |
|
64 | - { |
|
65 | - return $this->error; |
|
66 | - } |
|
60 | + /** |
|
61 | + * @return InvalidEmail|null |
|
62 | + */ |
|
63 | + public function getError() |
|
64 | + { |
|
65 | + return $this->error; |
|
66 | + } |
|
67 | 67 | } |
@@ -9,74 +9,74 @@ |
||
9 | 9 | */ |
10 | 10 | class EguliasAutoLoader |
11 | 11 | { |
12 | - /** |
|
13 | - * @var string The namespace prefix for this instance. |
|
14 | - */ |
|
15 | - protected $namespace = ''; |
|
12 | + /** |
|
13 | + * @var string The namespace prefix for this instance. |
|
14 | + */ |
|
15 | + protected $namespace = ''; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @var string The filesystem prefix to use for this instance |
|
19 | - */ |
|
20 | - protected $path = ''; |
|
17 | + /** |
|
18 | + * @var string The filesystem prefix to use for this instance |
|
19 | + */ |
|
20 | + protected $path = ''; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Build the instance of the autoloader |
|
24 | - * |
|
25 | - * @param string $namespace The prefixed namespace this instance will load |
|
26 | - * @param string $path The filesystem path to the root of the namespace |
|
27 | - */ |
|
28 | - public function __construct($namespace, $path) |
|
29 | - { |
|
30 | - $this->namespace = ltrim($namespace, '\\'); |
|
31 | - $this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
|
32 | - } |
|
22 | + /** |
|
23 | + * Build the instance of the autoloader |
|
24 | + * |
|
25 | + * @param string $namespace The prefixed namespace this instance will load |
|
26 | + * @param string $path The filesystem path to the root of the namespace |
|
27 | + */ |
|
28 | + public function __construct($namespace, $path) |
|
29 | + { |
|
30 | + $this->namespace = ltrim($namespace, '\\'); |
|
31 | + $this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Try to load a class |
|
36 | - * |
|
37 | - * @param string $class The class name to load |
|
38 | - * |
|
39 | - * @return boolean If the loading was successful |
|
40 | - */ |
|
41 | - public function load($class) |
|
42 | - { |
|
43 | - $class = ltrim($class, '\\'); |
|
44 | - if (strpos($class, $this->namespace) === 0) { |
|
45 | - $nsparts = explode('\\', $class); |
|
46 | - $class = array_pop($nsparts); |
|
47 | - $path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/'; |
|
48 | - $max=count($nsparts); |
|
49 | - for ($i=2; $i<$max;$i++) { |
|
50 | - $path .= $nsparts[$i].'/'; |
|
51 | - } |
|
52 | - $nsparts = array(); |
|
53 | - $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; |
|
54 | - if (file_exists($path)) { |
|
55 | - require $path; |
|
56 | - return true; |
|
57 | - } |
|
58 | - } |
|
34 | + /** |
|
35 | + * Try to load a class |
|
36 | + * |
|
37 | + * @param string $class The class name to load |
|
38 | + * |
|
39 | + * @return boolean If the loading was successful |
|
40 | + */ |
|
41 | + public function load($class) |
|
42 | + { |
|
43 | + $class = ltrim($class, '\\'); |
|
44 | + if (strpos($class, $this->namespace) === 0) { |
|
45 | + $nsparts = explode('\\', $class); |
|
46 | + $class = array_pop($nsparts); |
|
47 | + $path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/'; |
|
48 | + $max=count($nsparts); |
|
49 | + for ($i=2; $i<$max;$i++) { |
|
50 | + $path .= $nsparts[$i].'/'; |
|
51 | + } |
|
52 | + $nsparts = array(); |
|
53 | + $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; |
|
54 | + if (file_exists($path)) { |
|
55 | + require $path; |
|
56 | + return true; |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - return false; |
|
61 | - } |
|
60 | + return false; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Register the autoloader to PHP |
|
65 | - * |
|
66 | - * @return boolean The status of the registration |
|
67 | - */ |
|
68 | - public function register() |
|
69 | - { |
|
70 | - return spl_autoload_register(array($this, 'load')); |
|
71 | - } |
|
63 | + /** |
|
64 | + * Register the autoloader to PHP |
|
65 | + * |
|
66 | + * @return boolean The status of the registration |
|
67 | + */ |
|
68 | + public function register() |
|
69 | + { |
|
70 | + return spl_autoload_register(array($this, 'load')); |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Unregister the autoloader to PHP |
|
75 | - * |
|
76 | - * @return boolean The status of the unregistration |
|
77 | - */ |
|
78 | - public function unregister() |
|
79 | - { |
|
80 | - return spl_autoload_unregister(array($this, 'load')); |
|
81 | - } |
|
73 | + /** |
|
74 | + * Unregister the autoloader to PHP |
|
75 | + * |
|
76 | + * @return boolean The status of the unregistration |
|
77 | + */ |
|
78 | + public function unregister() |
|
79 | + { |
|
80 | + return spl_autoload_unregister(array($this, 'load')); |
|
81 | + } |
|
82 | 82 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | public function __construct($namespace, $path) |
29 | 29 | { |
30 | 30 | $this->namespace = ltrim($namespace, '\\'); |
31 | - $this->path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR; |
|
31 | + $this->path = rtrim($path, '/\\').DIRECTORY_SEPARATOR; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -44,13 +44,13 @@ discard block |
||
44 | 44 | if (strpos($class, $this->namespace) === 0) { |
45 | 45 | $nsparts = explode('\\', $class); |
46 | 46 | $class = array_pop($nsparts); |
47 | - $path = $this->path . 'swiftmailer/egulias/email-validator/EmailValidator/'; |
|
48 | - $max=count($nsparts); |
|
49 | - for ($i=2; $i<$max;$i++) { |
|
47 | + $path = $this->path.'swiftmailer/egulias/email-validator/EmailValidator/'; |
|
48 | + $max = count($nsparts); |
|
49 | + for ($i = 2; $i < $max; $i++) { |
|
50 | 50 | $path .= $nsparts[$i].'/'; |
51 | 51 | } |
52 | 52 | $nsparts = array(); |
53 | - $path .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; |
|
53 | + $path .= str_replace('_', DIRECTORY_SEPARATOR, $class).'.php'; |
|
54 | 54 | if (file_exists($path)) { |
55 | 55 | require $path; |
56 | 56 | return true; |
@@ -29,304 +29,304 @@ |
||
29 | 29 | */ |
30 | 30 | abstract class AbstractLexer |
31 | 31 | { |
32 | - /** |
|
33 | - * Lexer original input string. |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $input; |
|
38 | - |
|
39 | - /** |
|
40 | - * Array of scanned tokens. |
|
41 | - * |
|
42 | - * Each token is an associative array containing three items: |
|
43 | - * - 'value' : the string value of the token in the input string |
|
44 | - * - 'type' : the type of the token (identifier, numeric, string, input |
|
45 | - * parameter, none) |
|
46 | - * - 'position' : the position of the token in the input string |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - private $tokens = array(); |
|
51 | - |
|
52 | - /** |
|
53 | - * Current lexer position in input string. |
|
54 | - * |
|
55 | - * @var integer |
|
56 | - */ |
|
57 | - private $position = 0; |
|
58 | - |
|
59 | - /** |
|
60 | - * Current peek of current lexer position. |
|
61 | - * |
|
62 | - * @var integer |
|
63 | - */ |
|
64 | - private $peek = 0; |
|
65 | - |
|
66 | - /** |
|
67 | - * The next token in the input. |
|
68 | - * |
|
69 | - * @var array |
|
70 | - */ |
|
71 | - public $lookahead; |
|
72 | - |
|
73 | - /** |
|
74 | - * The last matched/seen token. |
|
75 | - * |
|
76 | - * @var array |
|
77 | - */ |
|
78 | - public $token; |
|
79 | - |
|
80 | - /** |
|
81 | - * Sets the input data to be tokenized. |
|
82 | - * |
|
83 | - * The Lexer is immediately reset and the new input tokenized. |
|
84 | - * Any unprocessed tokens from any previous input are lost. |
|
85 | - * |
|
86 | - * @param string $input The input to be tokenized. |
|
87 | - * |
|
88 | - * @return void |
|
89 | - */ |
|
90 | - public function setInput($input) |
|
91 | - { |
|
92 | - $this->input = $input; |
|
93 | - $this->tokens = array(); |
|
94 | - |
|
95 | - $this->reset(); |
|
96 | - $this->scan($input); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Resets the lexer. |
|
101 | - * |
|
102 | - * @return void |
|
103 | - */ |
|
104 | - public function reset() |
|
105 | - { |
|
106 | - $this->lookahead = null; |
|
107 | - $this->token = null; |
|
108 | - $this->peek = 0; |
|
109 | - $this->position = 0; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Resets the peek pointer to 0. |
|
114 | - * |
|
115 | - * @return void |
|
116 | - */ |
|
117 | - public function resetPeek() |
|
118 | - { |
|
119 | - $this->peek = 0; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Resets the lexer position on the input to the given position. |
|
124 | - * |
|
125 | - * @param integer $position Position to place the lexical scanner. |
|
126 | - * |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public function resetPosition($position = 0) |
|
130 | - { |
|
131 | - $this->position = $position; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Retrieve the original lexer's input until a given position. |
|
136 | - * |
|
137 | - * @param integer $position |
|
138 | - * |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function getInputUntilPosition($position) |
|
142 | - { |
|
143 | - return substr($this->input, 0, $position); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Checks whether a given token matches the current lookahead. |
|
148 | - * |
|
149 | - * @param integer|string $token |
|
150 | - * |
|
151 | - * @return boolean |
|
152 | - */ |
|
153 | - public function isNextToken($token) |
|
154 | - { |
|
155 | - return null !== $this->lookahead && $this->lookahead['type'] === $token; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Checks whether any of the given tokens matches the current lookahead. |
|
160 | - * |
|
161 | - * @param array $tokens |
|
162 | - * |
|
163 | - * @return boolean |
|
164 | - */ |
|
165 | - public function isNextTokenAny(array $tokens) |
|
166 | - { |
|
167 | - return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * Moves to the next token in the input string. |
|
172 | - * |
|
173 | - * @return boolean |
|
174 | - */ |
|
175 | - public function moveNext() |
|
176 | - { |
|
177 | - $this->peek = 0; |
|
178 | - $this->token = $this->lookahead; |
|
179 | - $this->lookahead = (isset($this->tokens[$this->position])) |
|
180 | - ? $this->tokens[$this->position++] : null; |
|
181 | - |
|
182 | - return $this->lookahead !== null; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Tells the lexer to skip input tokens until it sees a token with the given value. |
|
187 | - * |
|
188 | - * @param string $type The token type to skip until. |
|
189 | - * |
|
190 | - * @return void |
|
191 | - */ |
|
192 | - public function skipUntil($type) |
|
193 | - { |
|
194 | - while ($this->lookahead !== null && $this->lookahead['type'] !== $type) { |
|
195 | - $this->moveNext(); |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Checks if given value is identical to the given token. |
|
201 | - * |
|
202 | - * @param mixed $value |
|
203 | - * @param integer $token |
|
204 | - * |
|
205 | - * @return boolean |
|
206 | - */ |
|
207 | - public function isA($value, $token) |
|
208 | - { |
|
209 | - return $this->getType($value) === $token; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Moves the lookahead token forward. |
|
214 | - * |
|
215 | - * @return array|null The next token or NULL if there are no more tokens ahead. |
|
216 | - */ |
|
217 | - public function peek() |
|
218 | - { |
|
219 | - if (isset($this->tokens[$this->position + $this->peek])) { |
|
220 | - return $this->tokens[$this->position + $this->peek++]; |
|
221 | - } else { |
|
222 | - return null; |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Peeks at the next token, returns it and immediately resets the peek. |
|
228 | - * |
|
229 | - * @return array|null The next token or NULL if there are no more tokens ahead. |
|
230 | - */ |
|
231 | - public function glimpse() |
|
232 | - { |
|
233 | - $peek = $this->peek(); |
|
234 | - $this->peek = 0; |
|
235 | - return $peek; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Scans the input string for tokens. |
|
240 | - * |
|
241 | - * @param string $input A query string. |
|
242 | - * |
|
243 | - * @return void |
|
244 | - */ |
|
245 | - protected function scan($input) |
|
246 | - { |
|
247 | - static $regex; |
|
248 | - |
|
249 | - if ( ! isset($regex)) { |
|
250 | - $regex = sprintf( |
|
251 | - '/(%s)|%s/%s', |
|
252 | - implode(')|(', $this->getCatchablePatterns()), |
|
253 | - implode('|', $this->getNonCatchablePatterns()), |
|
254 | - $this->getModifiers() |
|
255 | - ); |
|
256 | - } |
|
257 | - |
|
258 | - $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; |
|
259 | - $matches = preg_split($regex, $input, -1, $flags); |
|
260 | - |
|
261 | - if (false === $matches) { |
|
262 | - // Work around https://bugs.php.net/78122 |
|
263 | - $matches = array(array($input, 0)); |
|
264 | - } |
|
265 | - |
|
266 | - foreach ($matches as $match) { |
|
267 | - // Must remain before 'value' assignment since it can change content |
|
268 | - $type = $this->getType($match[0]); |
|
269 | - |
|
270 | - $this->tokens[] = array( |
|
271 | - 'value' => $match[0], |
|
272 | - 'type' => $type, |
|
273 | - 'position' => $match[1], |
|
274 | - ); |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Gets the literal for a given token. |
|
280 | - * |
|
281 | - * @param integer $token |
|
282 | - * |
|
283 | - * @return string |
|
284 | - */ |
|
285 | - public function getLiteral($token) |
|
286 | - { |
|
287 | - $className = get_class($this); |
|
288 | - $reflClass = new \ReflectionClass($className); |
|
289 | - $constants = $reflClass->getConstants(); |
|
290 | - |
|
291 | - foreach ($constants as $name => $value) { |
|
292 | - if ($value === $token) { |
|
293 | - return $className . '::' . $name; |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - return $token; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Regex modifiers |
|
302 | - * |
|
303 | - * @return string |
|
304 | - */ |
|
305 | - protected function getModifiers() |
|
306 | - { |
|
307 | - return 'i'; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Lexical catchable patterns. |
|
312 | - * |
|
313 | - * @return array |
|
314 | - */ |
|
315 | - abstract protected function getCatchablePatterns(); |
|
316 | - |
|
317 | - /** |
|
318 | - * Lexical non-catchable patterns. |
|
319 | - * |
|
320 | - * @return array |
|
321 | - */ |
|
322 | - abstract protected function getNonCatchablePatterns(); |
|
323 | - |
|
324 | - /** |
|
325 | - * Retrieve token type. Also processes the token value if necessary. |
|
326 | - * |
|
327 | - * @param string $value |
|
328 | - * |
|
329 | - * @return integer |
|
330 | - */ |
|
331 | - abstract protected function getType(&$value); |
|
32 | + /** |
|
33 | + * Lexer original input string. |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $input; |
|
38 | + |
|
39 | + /** |
|
40 | + * Array of scanned tokens. |
|
41 | + * |
|
42 | + * Each token is an associative array containing three items: |
|
43 | + * - 'value' : the string value of the token in the input string |
|
44 | + * - 'type' : the type of the token (identifier, numeric, string, input |
|
45 | + * parameter, none) |
|
46 | + * - 'position' : the position of the token in the input string |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + private $tokens = array(); |
|
51 | + |
|
52 | + /** |
|
53 | + * Current lexer position in input string. |
|
54 | + * |
|
55 | + * @var integer |
|
56 | + */ |
|
57 | + private $position = 0; |
|
58 | + |
|
59 | + /** |
|
60 | + * Current peek of current lexer position. |
|
61 | + * |
|
62 | + * @var integer |
|
63 | + */ |
|
64 | + private $peek = 0; |
|
65 | + |
|
66 | + /** |
|
67 | + * The next token in the input. |
|
68 | + * |
|
69 | + * @var array |
|
70 | + */ |
|
71 | + public $lookahead; |
|
72 | + |
|
73 | + /** |
|
74 | + * The last matched/seen token. |
|
75 | + * |
|
76 | + * @var array |
|
77 | + */ |
|
78 | + public $token; |
|
79 | + |
|
80 | + /** |
|
81 | + * Sets the input data to be tokenized. |
|
82 | + * |
|
83 | + * The Lexer is immediately reset and the new input tokenized. |
|
84 | + * Any unprocessed tokens from any previous input are lost. |
|
85 | + * |
|
86 | + * @param string $input The input to be tokenized. |
|
87 | + * |
|
88 | + * @return void |
|
89 | + */ |
|
90 | + public function setInput($input) |
|
91 | + { |
|
92 | + $this->input = $input; |
|
93 | + $this->tokens = array(); |
|
94 | + |
|
95 | + $this->reset(); |
|
96 | + $this->scan($input); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Resets the lexer. |
|
101 | + * |
|
102 | + * @return void |
|
103 | + */ |
|
104 | + public function reset() |
|
105 | + { |
|
106 | + $this->lookahead = null; |
|
107 | + $this->token = null; |
|
108 | + $this->peek = 0; |
|
109 | + $this->position = 0; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Resets the peek pointer to 0. |
|
114 | + * |
|
115 | + * @return void |
|
116 | + */ |
|
117 | + public function resetPeek() |
|
118 | + { |
|
119 | + $this->peek = 0; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Resets the lexer position on the input to the given position. |
|
124 | + * |
|
125 | + * @param integer $position Position to place the lexical scanner. |
|
126 | + * |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public function resetPosition($position = 0) |
|
130 | + { |
|
131 | + $this->position = $position; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Retrieve the original lexer's input until a given position. |
|
136 | + * |
|
137 | + * @param integer $position |
|
138 | + * |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function getInputUntilPosition($position) |
|
142 | + { |
|
143 | + return substr($this->input, 0, $position); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Checks whether a given token matches the current lookahead. |
|
148 | + * |
|
149 | + * @param integer|string $token |
|
150 | + * |
|
151 | + * @return boolean |
|
152 | + */ |
|
153 | + public function isNextToken($token) |
|
154 | + { |
|
155 | + return null !== $this->lookahead && $this->lookahead['type'] === $token; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Checks whether any of the given tokens matches the current lookahead. |
|
160 | + * |
|
161 | + * @param array $tokens |
|
162 | + * |
|
163 | + * @return boolean |
|
164 | + */ |
|
165 | + public function isNextTokenAny(array $tokens) |
|
166 | + { |
|
167 | + return null !== $this->lookahead && in_array($this->lookahead['type'], $tokens, true); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * Moves to the next token in the input string. |
|
172 | + * |
|
173 | + * @return boolean |
|
174 | + */ |
|
175 | + public function moveNext() |
|
176 | + { |
|
177 | + $this->peek = 0; |
|
178 | + $this->token = $this->lookahead; |
|
179 | + $this->lookahead = (isset($this->tokens[$this->position])) |
|
180 | + ? $this->tokens[$this->position++] : null; |
|
181 | + |
|
182 | + return $this->lookahead !== null; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Tells the lexer to skip input tokens until it sees a token with the given value. |
|
187 | + * |
|
188 | + * @param string $type The token type to skip until. |
|
189 | + * |
|
190 | + * @return void |
|
191 | + */ |
|
192 | + public function skipUntil($type) |
|
193 | + { |
|
194 | + while ($this->lookahead !== null && $this->lookahead['type'] !== $type) { |
|
195 | + $this->moveNext(); |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Checks if given value is identical to the given token. |
|
201 | + * |
|
202 | + * @param mixed $value |
|
203 | + * @param integer $token |
|
204 | + * |
|
205 | + * @return boolean |
|
206 | + */ |
|
207 | + public function isA($value, $token) |
|
208 | + { |
|
209 | + return $this->getType($value) === $token; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Moves the lookahead token forward. |
|
214 | + * |
|
215 | + * @return array|null The next token or NULL if there are no more tokens ahead. |
|
216 | + */ |
|
217 | + public function peek() |
|
218 | + { |
|
219 | + if (isset($this->tokens[$this->position + $this->peek])) { |
|
220 | + return $this->tokens[$this->position + $this->peek++]; |
|
221 | + } else { |
|
222 | + return null; |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Peeks at the next token, returns it and immediately resets the peek. |
|
228 | + * |
|
229 | + * @return array|null The next token or NULL if there are no more tokens ahead. |
|
230 | + */ |
|
231 | + public function glimpse() |
|
232 | + { |
|
233 | + $peek = $this->peek(); |
|
234 | + $this->peek = 0; |
|
235 | + return $peek; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Scans the input string for tokens. |
|
240 | + * |
|
241 | + * @param string $input A query string. |
|
242 | + * |
|
243 | + * @return void |
|
244 | + */ |
|
245 | + protected function scan($input) |
|
246 | + { |
|
247 | + static $regex; |
|
248 | + |
|
249 | + if ( ! isset($regex)) { |
|
250 | + $regex = sprintf( |
|
251 | + '/(%s)|%s/%s', |
|
252 | + implode(')|(', $this->getCatchablePatterns()), |
|
253 | + implode('|', $this->getNonCatchablePatterns()), |
|
254 | + $this->getModifiers() |
|
255 | + ); |
|
256 | + } |
|
257 | + |
|
258 | + $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_OFFSET_CAPTURE; |
|
259 | + $matches = preg_split($regex, $input, -1, $flags); |
|
260 | + |
|
261 | + if (false === $matches) { |
|
262 | + // Work around https://bugs.php.net/78122 |
|
263 | + $matches = array(array($input, 0)); |
|
264 | + } |
|
265 | + |
|
266 | + foreach ($matches as $match) { |
|
267 | + // Must remain before 'value' assignment since it can change content |
|
268 | + $type = $this->getType($match[0]); |
|
269 | + |
|
270 | + $this->tokens[] = array( |
|
271 | + 'value' => $match[0], |
|
272 | + 'type' => $type, |
|
273 | + 'position' => $match[1], |
|
274 | + ); |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Gets the literal for a given token. |
|
280 | + * |
|
281 | + * @param integer $token |
|
282 | + * |
|
283 | + * @return string |
|
284 | + */ |
|
285 | + public function getLiteral($token) |
|
286 | + { |
|
287 | + $className = get_class($this); |
|
288 | + $reflClass = new \ReflectionClass($className); |
|
289 | + $constants = $reflClass->getConstants(); |
|
290 | + |
|
291 | + foreach ($constants as $name => $value) { |
|
292 | + if ($value === $token) { |
|
293 | + return $className . '::' . $name; |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + return $token; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Regex modifiers |
|
302 | + * |
|
303 | + * @return string |
|
304 | + */ |
|
305 | + protected function getModifiers() |
|
306 | + { |
|
307 | + return 'i'; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Lexical catchable patterns. |
|
312 | + * |
|
313 | + * @return array |
|
314 | + */ |
|
315 | + abstract protected function getCatchablePatterns(); |
|
316 | + |
|
317 | + /** |
|
318 | + * Lexical non-catchable patterns. |
|
319 | + * |
|
320 | + * @return array |
|
321 | + */ |
|
322 | + abstract protected function getNonCatchablePatterns(); |
|
323 | + |
|
324 | + /** |
|
325 | + * Retrieve token type. Also processes the token value if necessary. |
|
326 | + * |
|
327 | + * @param string $value |
|
328 | + * |
|
329 | + * @return integer |
|
330 | + */ |
|
331 | + abstract protected function getType(&$value); |
|
332 | 332 | } |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | { |
247 | 247 | static $regex; |
248 | 248 | |
249 | - if ( ! isset($regex)) { |
|
249 | + if (!isset($regex)) { |
|
250 | 250 | $regex = sprintf( |
251 | 251 | '/(%s)|%s/%s', |
252 | 252 | implode(')|(', $this->getCatchablePatterns()), |
@@ -290,7 +290,7 @@ discard block |
||
290 | 290 | |
291 | 291 | foreach ($constants as $name => $value) { |
292 | 292 | if ($value === $token) { |
293 | - return $className . '::' . $name; |
|
293 | + return $className.'::'.$name; |
|
294 | 294 | } |
295 | 295 | } |
296 | 296 |