|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Lexer\AbstractLexer; |
|
6
|
|
|
|
|
7
|
|
|
class EmailLexer extends AbstractLexer |
|
8
|
|
|
{ |
|
9
|
|
|
//ASCII values |
|
10
|
|
|
public const S_EMPTY = null; |
|
11
|
|
|
public const C_NUL = 0; |
|
12
|
|
|
public const S_HTAB = 9; |
|
13
|
|
|
public const S_LF = 10; |
|
14
|
|
|
public const S_CR = 13; |
|
15
|
|
|
public const S_SP = 32; |
|
16
|
|
|
public const EXCLAMATION = 33; |
|
17
|
|
|
public const S_DQUOTE = 34; |
|
18
|
|
|
public const NUMBER_SIGN = 35; |
|
19
|
|
|
public const DOLLAR = 36; |
|
20
|
|
|
public const PERCENTAGE = 37; |
|
21
|
|
|
public const AMPERSAND = 38; |
|
22
|
|
|
public const S_SQUOTE = 39; |
|
23
|
|
|
public const S_OPENPARENTHESIS = 40; |
|
24
|
|
|
public const S_CLOSEPARENTHESIS = 41; |
|
25
|
|
|
public const ASTERISK = 42; |
|
26
|
|
|
public const S_PLUS = 43; |
|
27
|
|
|
public const S_COMMA = 44; |
|
28
|
|
|
public const S_HYPHEN = 45; |
|
29
|
|
|
public const S_DOT = 46; |
|
30
|
|
|
public const S_SLASH = 47; |
|
31
|
|
|
public const S_COLON = 58; |
|
32
|
|
|
public const S_SEMICOLON = 59; |
|
33
|
|
|
public const S_LOWERTHAN = 60; |
|
34
|
|
|
public const S_EQUAL = 61; |
|
35
|
|
|
public const S_GREATERTHAN = 62; |
|
36
|
|
|
public const QUESTIONMARK = 63; |
|
37
|
|
|
public const S_AT = 64; |
|
38
|
|
|
public const S_OPENBRACKET = 91; |
|
39
|
|
|
public const S_BACKSLASH = 92; |
|
40
|
|
|
public const S_CLOSEBRACKET = 93; |
|
41
|
|
|
public const CARET = 94; |
|
42
|
|
|
public const S_UNDERSCORE = 95; |
|
43
|
|
|
public const S_BACKTICK = 96; |
|
44
|
|
|
public const S_OPENCURLYBRACES = 123; |
|
45
|
|
|
public const S_PIPE = 124; |
|
46
|
|
|
public const S_CLOSECURLYBRACES = 125; |
|
47
|
|
|
public const S_TILDE = 126; |
|
48
|
|
|
public const C_DEL = 127; |
|
49
|
|
|
public const INVERT_QUESTIONMARK= 168; |
|
50
|
|
|
public const INVERT_EXCLAMATION = 173; |
|
51
|
|
|
public const GENERIC = 300; |
|
52
|
|
|
public const S_IPV6TAG = 301; |
|
53
|
|
|
public const INVALID = 302; |
|
54
|
|
|
public const CRLF = 1310; |
|
55
|
|
|
public const S_DOUBLECOLON = 5858; |
|
56
|
|
|
public const ASCII_INVALID_FROM = 127; |
|
57
|
|
|
public const ASCII_INVALID_TO = 199; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3) |
|
61
|
|
|
* |
|
62
|
|
|
* @var array |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $charValue = [ |
|
65
|
|
|
'{' => self::S_OPENCURLYBRACES, |
|
66
|
|
|
'}' => self::S_CLOSECURLYBRACES, |
|
67
|
|
|
'(' => self::S_OPENPARENTHESIS, |
|
68
|
|
|
')' => self::S_CLOSEPARENTHESIS, |
|
69
|
|
|
'<' => self::S_LOWERTHAN, |
|
70
|
|
|
'>' => self::S_GREATERTHAN, |
|
71
|
|
|
'[' => self::S_OPENBRACKET, |
|
72
|
|
|
']' => self::S_CLOSEBRACKET, |
|
73
|
|
|
':' => self::S_COLON, |
|
74
|
|
|
';' => self::S_SEMICOLON, |
|
75
|
|
|
'@' => self::S_AT, |
|
76
|
|
|
'\\' => self::S_BACKSLASH, |
|
77
|
|
|
'/' => self::S_SLASH, |
|
78
|
|
|
',' => self::S_COMMA, |
|
79
|
|
|
'.' => self::S_DOT, |
|
80
|
|
|
"'" => self::S_SQUOTE, |
|
81
|
|
|
"`" => self::S_BACKTICK, |
|
82
|
|
|
'"' => self::S_DQUOTE, |
|
83
|
|
|
'-' => self::S_HYPHEN, |
|
84
|
|
|
'::' => self::S_DOUBLECOLON, |
|
85
|
|
|
' ' => self::S_SP, |
|
86
|
|
|
"\t" => self::S_HTAB, |
|
87
|
|
|
"\r" => self::S_CR, |
|
88
|
|
|
"\n" => self::S_LF, |
|
89
|
|
|
"\r\n" => self::CRLF, |
|
90
|
|
|
'IPv6' => self::S_IPV6TAG, |
|
91
|
|
|
'' => self::S_EMPTY, |
|
92
|
|
|
'\0' => self::C_NUL, |
|
93
|
|
|
'*' => self::ASTERISK, |
|
94
|
|
|
'!' => self::EXCLAMATION, |
|
95
|
|
|
'&' => self::AMPERSAND, |
|
96
|
|
|
'^' => self::CARET, |
|
97
|
|
|
'$' => self::DOLLAR, |
|
98
|
|
|
'%' => self::PERCENTAGE, |
|
99
|
|
|
'~' => self::S_TILDE, |
|
100
|
|
|
'|' => self::S_PIPE, |
|
101
|
|
|
'_' => self::S_UNDERSCORE, |
|
102
|
|
|
'=' => self::S_EQUAL, |
|
103
|
|
|
'+' => self::S_PLUS, |
|
104
|
|
|
'¿' => self::INVERT_QUESTIONMARK, |
|
105
|
|
|
'?' => self::QUESTIONMARK, |
|
106
|
|
|
'#' => self::NUMBER_SIGN, |
|
107
|
|
|
'¡' => self::INVERT_EXCLAMATION, |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
public const INVALID_CHARS_REGEX = "/[^\p{S}\p{C}\p{Cc}]+/iu"; |
|
111
|
|
|
|
|
112
|
|
|
public const VALID_UTF8_REGEX = '/\p{Cc}+/u'; |
|
113
|
|
|
|
|
114
|
|
|
public const CATCHABLE_PATTERNS = [ |
|
115
|
|
|
'[a-zA-Z]+[46]?', //ASCII and domain literal |
|
116
|
|
|
'[^\x00-\x7F]', //UTF-8 |
|
117
|
|
|
'[0-9]+', |
|
118
|
|
|
'\r\n', |
|
119
|
|
|
'::', |
|
120
|
|
|
'\s+?', |
|
121
|
|
|
'.', |
|
122
|
|
|
]; |
|
123
|
|
|
|
|
124
|
|
|
public const NON_CATCHABLE_PATTERNS = [ |
|
125
|
|
|
'[\xA0-\xff]+', |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
public const MODIFIERS = 'iu'; |
|
129
|
|
|
|
|
130
|
|
|
/** @var bool */ |
|
131
|
|
|
protected $hasInvalidTokens = false; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @var array |
|
135
|
|
|
* |
|
136
|
|
|
* @psalm-var array{value:string, type:null|int, position:int}|array<empty, empty> |
|
137
|
|
|
*/ |
|
138
|
|
|
protected $previous = []; |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @var array |
|
142
|
|
|
* |
|
143
|
|
|
* @psalm-var array{value:string, type:null|int, position:int}|array<empty, empty>|\Doctrine\Common\Lexer\Token|null |
|
144
|
|
|
*/ |
|
145
|
|
|
private static $nullToken; |
|
146
|
|
|
|
|
147
|
|
|
/** @var string */ |
|
148
|
|
|
private $accumulator = ''; |
|
149
|
|
|
|
|
150
|
|
|
/** @var bool */ |
|
151
|
|
|
private $hasToRecord = false; |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @psalm-suppress InvalidPropertyAssignmentValue |
|
155
|
|
|
*/ |
|
156
|
340 |
|
public function __construct() |
|
157
|
|
|
{ |
|
158
|
340 |
|
if (null === self::$nullToken) { |
|
|
|
|
|
|
159
|
1 |
|
self::$nullToken = $this->denormalizeToken([ |
|
160
|
1 |
|
'value' => '', |
|
161
|
1 |
|
'type' => null, |
|
162
|
1 |
|
'position' => 0, |
|
163
|
1 |
|
]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
340 |
|
$this->previous = $this->token = self::$nullToken; |
|
167
|
340 |
|
$this->lookahead = null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
308 |
|
public function reset() : void |
|
171
|
|
|
{ |
|
172
|
308 |
|
$this->hasInvalidTokens = false; |
|
173
|
308 |
|
parent::reset(); |
|
174
|
308 |
|
$this->previous = $this->token = self::$nullToken; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param int $type |
|
179
|
|
|
* @throws \UnexpectedValueException |
|
180
|
|
|
* @return boolean |
|
181
|
|
|
* |
|
182
|
|
|
* @psalm-suppress InvalidScalarArgument |
|
183
|
|
|
*/ |
|
184
|
54 |
|
public function find($type) : bool |
|
185
|
|
|
{ |
|
186
|
54 |
|
$search = clone $this; |
|
187
|
54 |
|
$search->skipUntil($type); |
|
|
|
|
|
|
188
|
|
|
|
|
189
|
54 |
|
if (!$search->lookahead) { |
|
190
|
6 |
|
throw new \UnexpectedValueException($type . ' not found'); |
|
191
|
|
|
} |
|
192
|
48 |
|
return true; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* moveNext |
|
197
|
|
|
* |
|
198
|
|
|
* @psalm-suppress InvalidPropertyAssignmentValue |
|
199
|
|
|
* @return boolean |
|
200
|
|
|
*/ |
|
201
|
297 |
|
public function moveNext() : bool |
|
202
|
|
|
{ |
|
203
|
297 |
|
if ($this->hasToRecord && $this->previous === self::$nullToken) { |
|
204
|
179 |
|
$this->accumulator .= $this->getToken()['value']; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
297 |
|
$this->previous = $this->token; |
|
208
|
|
|
|
|
209
|
297 |
|
if($this->lookahead === null) { |
|
210
|
297 |
|
$this->lookahead = self::$nullToken; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
297 |
|
$hasNext = parent::moveNext(); |
|
214
|
|
|
|
|
215
|
297 |
|
if ($this->hasToRecord) { |
|
216
|
179 |
|
$this->accumulator .= $this->getToken()['value']; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
297 |
|
return $hasNext; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Retrieve token type. Also processes the token value if necessary. |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $value |
|
226
|
|
|
* @throws \InvalidArgumentException |
|
227
|
|
|
* @return integer |
|
228
|
|
|
*/ |
|
229
|
300 |
|
protected function getType(&$value) |
|
230
|
|
|
{ |
|
231
|
300 |
|
$encoded = $value; |
|
232
|
|
|
|
|
233
|
300 |
|
if (mb_detect_encoding($value, 'auto', true) !== 'UTF-8') { |
|
234
|
237 |
|
$encoded = mb_convert_encoding($value, 'UTF-8', 'Windows-1252'); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
300 |
|
if ($this->isValid($encoded)) { |
|
|
|
|
|
|
238
|
229 |
|
return $this->charValue[$encoded]; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
262 |
|
if ($this->isNullType($encoded)) { |
|
|
|
|
|
|
242
|
2 |
|
return self::C_NUL; |
|
|
|
|
|
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
261 |
|
if ($this->isInvalidChar($encoded)) { |
|
|
|
|
|
|
246
|
66 |
|
$this->hasInvalidTokens = true; |
|
247
|
66 |
|
return self::INVALID; |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
|
|
251
|
199 |
|
return self::GENERIC; |
|
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
300 |
|
protected function isValid(string $value) : bool |
|
255
|
|
|
{ |
|
256
|
300 |
|
return isset($this->charValue[$value]); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
262 |
|
protected function isNullType(string $value) : bool |
|
260
|
|
|
{ |
|
261
|
262 |
|
return $value === "\0"; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
261 |
|
protected function isInvalidChar(string $value) : bool |
|
265
|
|
|
{ |
|
266
|
261 |
|
return !preg_match(self::INVALID_CHARS_REGEX, $value); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
protected function isUTF8Invalid(string $value) : bool |
|
270
|
|
|
{ |
|
271
|
|
|
return preg_match(self::VALID_UTF8_REGEX, $value) !== false; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
191 |
|
public function hasInvalidTokens() : bool |
|
275
|
|
|
{ |
|
276
|
191 |
|
return $this->hasInvalidTokens; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
178 |
|
public function getPrevious() : array |
|
280
|
|
|
{ |
|
281
|
178 |
|
return $this->normalizeToken($this->previous); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
299 |
|
public function getToken() : array |
|
285
|
|
|
{ |
|
286
|
299 |
|
return $this->normalizeToken($this->token); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* @param array|\ArrayAccess|\Doctrine\Common\Lexer\Token $token |
|
291
|
|
|
* @return array|\Doctrine\Common\Lexer\Token |
|
292
|
|
|
*/ |
|
293
|
1 |
|
private function denormalizeToken($token) |
|
294
|
|
|
{ |
|
295
|
1 |
|
if (class_exists('Doctrine\Common\Lexer\Token')) { |
|
296
|
1 |
|
if ($token instanceof \Doctrine\Common\Lexer\Token) { |
|
297
|
|
|
return $token; |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
1 |
|
if (is_array($token) || $token instanceof \ArrayAccess) { |
|
|
|
|
|
|
301
|
1 |
|
return new \Doctrine\Common\Lexer\Token($token['value'],$token['type'],$token['position']); |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
if (is_array($token)) { |
|
306
|
|
|
return $token; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
throw new \LogicException(sprintf('unsupported type of token "%s"', get_debug_type($token))); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @psalm-param array{value:string, type:null|int, position:int}|array<empty, empty>|\Doctrine\Common\Lexer\Token|null $token |
|
314
|
|
|
*/ |
|
315
|
299 |
|
private function normalizeToken($token): array |
|
316
|
|
|
{ |
|
317
|
299 |
|
if (is_array($token)) { |
|
318
|
|
|
return $token; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
299 |
|
if (class_exists('Doctrine\Common\Lexer\Token') && $token instanceof \Doctrine\Common\Lexer\Token) { |
|
322
|
299 |
|
return [ |
|
323
|
299 |
|
'value' => $token->value, |
|
324
|
299 |
|
'type' => $token->type, |
|
325
|
299 |
|
'position' => $token->position, |
|
326
|
299 |
|
]; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
throw new \LogicException(sprintf('unsupported type of token "%s"', get_debug_type($token))); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Lexical catchable patterns. |
|
334
|
|
|
* |
|
335
|
|
|
* @return string[] |
|
336
|
|
|
*/ |
|
337
|
301 |
|
protected function getCatchablePatterns() : array |
|
338
|
|
|
{ |
|
339
|
301 |
|
return self::CATCHABLE_PATTERNS; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Lexical non-catchable patterns. |
|
344
|
|
|
* |
|
345
|
|
|
* @return string[] |
|
346
|
|
|
*/ |
|
347
|
301 |
|
protected function getNonCatchablePatterns() : array |
|
348
|
|
|
{ |
|
349
|
301 |
|
return self::NON_CATCHABLE_PATTERNS; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
301 |
|
protected function getModifiers() : string |
|
353
|
|
|
{ |
|
354
|
301 |
|
return self::MODIFIERS; |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
151 |
|
public function getAccumulatedValues() : string |
|
358
|
|
|
{ |
|
359
|
151 |
|
return $this->accumulator; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
188 |
|
public function startRecording() : void |
|
363
|
|
|
{ |
|
364
|
188 |
|
$this->hasToRecord = true; |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
148 |
|
public function stopRecording() : void |
|
368
|
|
|
{ |
|
369
|
148 |
|
$this->hasToRecord = false; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
149 |
|
public function clearRecorded() : void |
|
373
|
|
|
{ |
|
374
|
149 |
|
$this->accumulator = ''; |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
|