| 148 |
lars |
1 |
<?php
|
| 150 |
lars |
2 |
|
| 148 |
lars |
3 |
namespace Egulias\EmailValidator\Parser;
|
|
|
4 |
|
|
|
5 |
use Egulias\EmailValidator\EmailLexer;
|
|
|
6 |
use Egulias\EmailValidator\Result\ValidEmail;
|
|
|
7 |
use Egulias\EmailValidator\Result\InvalidEmail;
|
|
|
8 |
use Egulias\EmailValidator\Warning\CFWSWithFWS;
|
|
|
9 |
use Egulias\EmailValidator\Warning\QuotedString;
|
|
|
10 |
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT;
|
|
|
11 |
use Egulias\EmailValidator\Result\Reason\UnclosedQuotedString;
|
|
|
12 |
use Egulias\EmailValidator\Result\Result;
|
|
|
13 |
|
|
|
14 |
class DoubleQuote extends PartParser
|
|
|
15 |
{
|
| 150 |
lars |
16 |
public function parse(): Result
|
| 148 |
lars |
17 |
{
|
|
|
18 |
|
|
|
19 |
$validQuotedString = $this->checkDQUOTE();
|
| 150 |
lars |
20 |
if ($validQuotedString->isInvalid()) return $validQuotedString;
|
| 148 |
lars |
21 |
|
|
|
22 |
$special = [
|
|
|
23 |
EmailLexer::S_CR => true,
|
|
|
24 |
EmailLexer::S_HTAB => true,
|
|
|
25 |
EmailLexer::S_LF => true
|
|
|
26 |
];
|
|
|
27 |
|
|
|
28 |
$invalid = [
|
|
|
29 |
EmailLexer::C_NUL => true,
|
|
|
30 |
EmailLexer::S_HTAB => true,
|
|
|
31 |
EmailLexer::S_CR => true,
|
|
|
32 |
EmailLexer::S_LF => true
|
|
|
33 |
];
|
| 150 |
lars |
34 |
|
| 148 |
lars |
35 |
$setSpecialsWarning = true;
|
|
|
36 |
|
|
|
37 |
$this->lexer->moveNext();
|
|
|
38 |
|
| 150 |
lars |
39 |
while (!$this->lexer->current->isA(EmailLexer::S_DQUOTE) && !$this->lexer->current->isA(EmailLexer::S_EMPTY)) {
|
|
|
40 |
if (isset($special[$this->lexer->current->type]) && $setSpecialsWarning) {
|
| 148 |
lars |
41 |
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
|
|
|
42 |
$setSpecialsWarning = false;
|
|
|
43 |
}
|
| 150 |
lars |
44 |
if ($this->lexer->current->isA(EmailLexer::S_BACKSLASH) && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
|
| 148 |
lars |
45 |
$this->lexer->moveNext();
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
$this->lexer->moveNext();
|
|
|
49 |
|
| 150 |
lars |
50 |
if (!$this->escaped() && isset($invalid[$this->lexer->current->type])) {
|
|
|
51 |
return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value);
|
| 148 |
lars |
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
$prev = $this->lexer->getPrevious();
|
|
|
56 |
|
| 150 |
lars |
57 |
if ($prev->isA(EmailLexer::S_BACKSLASH)) {
|
| 148 |
lars |
58 |
$validQuotedString = $this->checkDQUOTE();
|
| 150 |
lars |
59 |
if ($validQuotedString->isInvalid()) return $validQuotedString;
|
| 148 |
lars |
60 |
}
|
|
|
61 |
|
| 150 |
lars |
62 |
if (!$this->lexer->isNextToken(EmailLexer::S_AT) && !$prev->isA(EmailLexer::S_BACKSLASH)) {
|
|
|
63 |
return new InvalidEmail(new ExpectingATEXT("Expecting ATEXT between DQUOTE"), $this->lexer->current->value);
|
| 148 |
lars |
64 |
}
|
|
|
65 |
|
|
|
66 |
return new ValidEmail();
|
|
|
67 |
}
|
|
|
68 |
|
| 150 |
lars |
69 |
protected function checkDQUOTE(): Result
|
| 148 |
lars |
70 |
{
|
|
|
71 |
$previous = $this->lexer->getPrevious();
|
|
|
72 |
|
| 150 |
lars |
73 |
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous->isA(EmailLexer::GENERIC)) {
|
| 148 |
lars |
74 |
$description = 'https://tools.ietf.org/html/rfc5322#section-3.2.4 - quoted string should be a unit';
|
| 150 |
lars |
75 |
return new InvalidEmail(new ExpectingATEXT($description), $this->lexer->current->value);
|
| 148 |
lars |
76 |
}
|
|
|
77 |
|
|
|
78 |
try {
|
|
|
79 |
$this->lexer->find(EmailLexer::S_DQUOTE);
|
|
|
80 |
} catch (\Exception $e) {
|
| 150 |
lars |
81 |
return new InvalidEmail(new UnclosedQuotedString(), $this->lexer->current->value);
|
| 148 |
lars |
82 |
}
|
| 150 |
lars |
83 |
$this->warnings[QuotedString::CODE] = new QuotedString($previous->value, $this->lexer->current->value);
|
| 148 |
lars |
84 |
|
|
|
85 |
return new ValidEmail();
|
|
|
86 |
}
|
|
|
87 |
}
|