3 namespace Egulias\EmailValidator;
5 use Doctrine\Common\Lexer\AbstractLexer;
7 class EmailLexer extends AbstractLexer
13 const S_BACKSLASH = 92;
16 const S_OPENPARENTHESIS = 49;
17 const S_CLOSEPARENTHESIS = 261;
18 const S_OPENBRACKET = 262;
19 const S_CLOSEBRACKET = 263;
22 const S_DOUBLECOLON = 266;
27 const S_IPV6TAG = 271;
28 const S_LOWERTHAN = 272;
29 const S_GREATERTHAN = 273;
31 const S_SEMICOLON = 275;
32 const S_OPENQBRACKET = 276;
33 const S_CLOSEQBRACKET = 277;
39 const ASCII_INVALID_FROM = 127;
40 const ASCII_INVALID_TO = 199;
43 * US-ASCII visible characters not valid for atext (@link http://tools.ietf.org/html/rfc5322#section-3.2.3)
47 protected $charValue = array(
48 '(' => self::S_OPENPARENTHESIS,
49 ')' => self::S_CLOSEPARENTHESIS,
50 '<' => self::S_LOWERTHAN,
51 '>' => self::S_GREATERTHAN,
52 '[' => self::S_OPENBRACKET,
53 ']' => self::S_CLOSEBRACKET,
55 ';' => self::S_SEMICOLON,
57 '\\' => self::S_BACKSLASH,
61 '"' => self::S_DQUOTE,
62 '-' => self::S_HYPHEN,
63 '::' => self::S_DOUBLECOLON,
69 'IPv6' => self::S_IPV6TAG,
70 '{' => self::S_OPENQBRACKET,
71 '}' => self::S_CLOSEQBRACKET,
76 protected $hasInvalidTokens = false;
80 public function reset()
82 $this->hasInvalidTokens = false;
86 public function hasInvalidTokens()
88 return $this->hasInvalidTokens;
93 * @throws \UnexpectedValueException
96 public function find($type)
98 $search = clone $this;
99 $search->skipUntil($type);
101 if (!$search->lookahead) {
102 throw new \UnexpectedValueException($type . ' not found');
110 * @return array token
112 public function getPrevious()
114 return $this->previous;
122 public function moveNext()
124 $this->previous = $this->token;
126 return parent::moveNext();
130 * Lexical catchable patterns.
134 protected function getCatchablePatterns()
137 '[a-zA-Z_]+[46]?', //ASCII and domain literal
138 '[^\x00-\x7F]', //UTF-8
148 * Lexical non-catchable patterns.
152 protected function getNonCatchablePatterns()
154 return array('[\xA0-\xff]+');
158 * Retrieve token type. Also processes the token value if necessary.
160 * @param string $value
161 * @throws \InvalidArgumentException
164 protected function getType(&$value)
166 if ($this->isNullType($value)) {
170 if ($this->isValid($value)) {
171 return $this->charValue[$value];
174 if ($this->isUTF8Invalid($value)) {
175 $this->hasInvalidTokens = true;
176 return self::INVALID;
179 return self::GENERIC;
182 protected function isValid($value)
184 if (isset($this->charValue[$value])) {
195 protected function isNullType($value)
197 if ($value === "\0") {
208 protected function isUTF8Invalid($value)
210 if (preg_match('/\p{Cc}+/u', $value)) {
217 protected function getModifiers()