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