|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
|
6
|
|
|
use Egulias\EmailValidator\Result\Result; |
|
7
|
|
|
use Egulias\EmailValidator\Warning\QuotedPart; |
|
8
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
|
9
|
|
|
use Egulias\EmailValidator\Parser\CommentStrategy\CommentStrategy; |
|
10
|
|
|
use Egulias\EmailValidator\Result\Reason\UnclosedComment; |
|
11
|
|
|
use Egulias\EmailValidator\Result\Reason\UnOpenedComment; |
|
12
|
|
|
use Egulias\EmailValidator\Warning\Comment as WarningComment; |
|
13
|
|
|
|
|
14
|
|
|
class Comment extends PartParser |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
private $openedParenthesis = 0; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var CommentStrategy |
|
23
|
|
|
*/ |
|
24
|
|
|
private $commentStrategy; |
|
25
|
|
|
|
|
26
|
15 |
|
public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy) |
|
27
|
|
|
{ |
|
28
|
15 |
|
$this->lexer = $lexer; |
|
29
|
15 |
|
$this->commentStrategy = $commentStrategy; |
|
30
|
15 |
|
} |
|
31
|
|
|
|
|
32
|
15 |
|
public function parse() : Result |
|
33
|
|
|
{ |
|
34
|
15 |
|
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) { |
|
35
|
13 |
|
$this->openedParenthesis++; |
|
36
|
13 |
|
if($this->noClosingParenthesis()) { |
|
37
|
5 |
|
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
10 |
|
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) { |
|
42
|
2 |
|
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
$this->warnings[WarningComment::CODE] = new WarningComment(); |
|
46
|
|
|
|
|
47
|
8 |
|
$moreTokens = true; |
|
48
|
8 |
|
while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens){ |
|
49
|
|
|
|
|
50
|
8 |
|
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) { |
|
51
|
1 |
|
$this->openedParenthesis++; |
|
52
|
|
|
} |
|
53
|
8 |
|
$this->warnEscaping(); |
|
54
|
8 |
|
if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) { |
|
55
|
8 |
|
$this->openedParenthesis--; |
|
56
|
|
|
} |
|
57
|
8 |
|
$moreTokens = $this->lexer->moveNext(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
8 |
|
if($this->openedParenthesis >= 1) { |
|
61
|
|
|
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']); |
|
62
|
8 |
|
} else if ($this->openedParenthesis < 0) { |
|
63
|
3 |
|
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
5 |
|
$finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer); |
|
67
|
|
|
|
|
68
|
5 |
|
$this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings()); |
|
69
|
|
|
|
|
70
|
5 |
|
return $finalValidations; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
8 |
|
private function warnEscaping() : bool |
|
78
|
|
|
{ |
|
79
|
|
|
//Backslash found |
|
80
|
8 |
|
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { |
|
81
|
8 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
85
|
|
|
return false; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->warnings[QuotedPart::CODE] = |
|
89
|
|
|
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
|
90
|
|
|
return true; |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
13 |
|
private function noClosingParenthesis() : bool |
|
95
|
|
|
{ |
|
96
|
|
|
try { |
|
97
|
13 |
|
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS); |
|
98
|
8 |
|
return false; |
|
99
|
5 |
|
} catch (\RuntimeException $e) { |
|
100
|
5 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |