| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Egulias\EmailValidator\Parser;
|
|
|
4 |
|
|
|
5 |
use Egulias\EmailValidator\EmailLexer;
|
|
|
6 |
use Egulias\EmailValidator\Result\Result;
|
|
|
7 |
use Egulias\EmailValidator\Result\ValidEmail;
|
|
|
8 |
use Egulias\EmailValidator\Result\InvalidEmail;
|
|
|
9 |
use Egulias\EmailValidator\Warning\LocalTooLong;
|
|
|
10 |
use Egulias\EmailValidator\Result\Reason\DotAtEnd;
|
|
|
11 |
use Egulias\EmailValidator\Result\Reason\DotAtStart;
|
|
|
12 |
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot;
|
|
|
13 |
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
|
|
|
14 |
use Egulias\EmailValidator\Parser\CommentStrategy\LocalComment;
|
|
|
15 |
|
|
|
16 |
class LocalPart extends PartParser
|
|
|
17 |
{
|
|
|
18 |
public const INVALID_TOKENS = [
|
|
|
19 |
EmailLexer::S_COMMA => EmailLexer::S_COMMA,
|
|
|
20 |
EmailLexer::S_CLOSEBRACKET => EmailLexer::S_CLOSEBRACKET,
|
|
|
21 |
EmailLexer::S_OPENBRACKET => EmailLexer::S_OPENBRACKET,
|
|
|
22 |
EmailLexer::S_GREATERTHAN => EmailLexer::S_GREATERTHAN,
|
|
|
23 |
EmailLexer::S_LOWERTHAN => EmailLexer::S_LOWERTHAN,
|
|
|
24 |
EmailLexer::S_COLON => EmailLexer::S_COLON,
|
|
|
25 |
EmailLexer::S_SEMICOLON => EmailLexer::S_SEMICOLON,
|
|
|
26 |
EmailLexer::INVALID => EmailLexer::INVALID
|
|
|
27 |
];
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* @var string
|
|
|
31 |
*/
|
|
|
32 |
private $localPart = '';
|
|
|
33 |
|
|
|
34 |
|
|
|
35 |
public function parse() : Result
|
|
|
36 |
{
|
|
|
37 |
$this->lexer->startRecording();
|
|
|
38 |
|
|
|
39 |
while ($this->lexer->token['type'] !== EmailLexer::S_AT && null !== $this->lexer->token['type']) {
|
|
|
40 |
if ($this->hasDotAtStart()) {
|
|
|
41 |
return new InvalidEmail(new DotAtStart(), $this->lexer->token['value']);
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
if ($this->lexer->token['type'] === EmailLexer::S_DQUOTE) {
|
|
|
45 |
$dquoteParsingResult = $this->parseDoubleQuote();
|
|
|
46 |
|
|
|
47 |
//Invalid double quote parsing
|
|
|
48 |
if($dquoteParsingResult->isInvalid()) {
|
|
|
49 |
return $dquoteParsingResult;
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS ||
|
|
|
54 |
$this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS ) {
|
|
|
55 |
$commentsResult = $this->parseComments();
|
|
|
56 |
|
|
|
57 |
//Invalid comment parsing
|
|
|
58 |
if($commentsResult->isInvalid()) {
|
|
|
59 |
return $commentsResult;
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) {
|
|
|
64 |
return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
|
|
|
68 |
$this->lexer->isNextToken(EmailLexer::S_AT)
|
|
|
69 |
) {
|
|
|
70 |
return new InvalidEmail(new DotAtEnd(), $this->lexer->token['value']);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$resultEscaping = $this->validateEscaping();
|
|
|
74 |
if ($resultEscaping->isInvalid()) {
|
|
|
75 |
return $resultEscaping;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$resultToken = $this->validateTokens(false);
|
|
|
79 |
if ($resultToken->isInvalid()) {
|
|
|
80 |
return $resultToken;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$resultFWS = $this->parseLocalFWS();
|
|
|
84 |
if($resultFWS->isInvalid()) {
|
|
|
85 |
return $resultFWS;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$this->lexer->moveNext();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
$this->lexer->stopRecording();
|
|
|
92 |
$this->localPart = rtrim($this->lexer->getAccumulatedValues(), '@');
|
|
|
93 |
if (strlen($this->localPart) > LocalTooLong::LOCAL_PART_LENGTH) {
|
|
|
94 |
$this->warnings[LocalTooLong::CODE] = new LocalTooLong();
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
return new ValidEmail();
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
protected function validateTokens(bool $hasComments) : Result
|
|
|
101 |
{
|
|
|
102 |
if (isset(self::INVALID_TOKENS[$this->lexer->token['type']])) {
|
|
|
103 |
return new InvalidEmail(new ExpectingATEXT('Invalid token found'), $this->lexer->token['value']);
|
|
|
104 |
}
|
|
|
105 |
return new ValidEmail();
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public function localPart() : string
|
|
|
109 |
{
|
|
|
110 |
return $this->localPart;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
private function parseLocalFWS() : Result
|
|
|
114 |
{
|
|
|
115 |
$foldingWS = new FoldingWhiteSpace($this->lexer);
|
|
|
116 |
$resultFWS = $foldingWS->parse();
|
|
|
117 |
if ($resultFWS->isValid()) {
|
|
|
118 |
$this->warnings = array_merge($this->warnings, $foldingWS->getWarnings());
|
|
|
119 |
}
|
|
|
120 |
return $resultFWS;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
private function hasDotAtStart() : bool
|
|
|
124 |
{
|
|
|
125 |
return $this->lexer->token['type'] === EmailLexer::S_DOT && null === $this->lexer->getPrevious()['type'];
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
private function parseDoubleQuote() : Result
|
|
|
129 |
{
|
|
|
130 |
$dquoteParser = new DoubleQuote($this->lexer);
|
|
|
131 |
$parseAgain = $dquoteParser->parse();
|
|
|
132 |
$this->warnings = array_merge($this->warnings, $dquoteParser->getWarnings());
|
|
|
133 |
|
|
|
134 |
return $parseAgain;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
protected function parseComments(): Result
|
|
|
138 |
{
|
|
|
139 |
$commentParser = new Comment($this->lexer, new LocalComment());
|
|
|
140 |
$result = $commentParser->parse();
|
|
|
141 |
$this->warnings = array_merge($this->warnings, $commentParser->getWarnings());
|
|
|
142 |
if($result->isInvalid()) {
|
|
|
143 |
return $result;
|
|
|
144 |
}
|
|
|
145 |
return $result;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
private function validateEscaping() : Result
|
|
|
149 |
{
|
|
|
150 |
//Backslash found
|
|
|
151 |
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
|
|
|
152 |
return new ValidEmail();
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
if ($this->lexer->isNextToken(EmailLexer::GENERIC)) {
|
|
|
156 |
return new InvalidEmail(new ExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
|
|
|
160 |
return new ValidEmail();
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
return new ValidEmail();
|
|
|
164 |
}
|
|
|
165 |
}
|