| 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\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 |
public function __construct(EmailLexer $lexer, CommentStrategy $commentStrategy)
|
|
|
27 |
{
|
|
|
28 |
$this->lexer = $lexer;
|
|
|
29 |
$this->commentStrategy = $commentStrategy;
|
|
|
30 |
}
|
|
|
31 |
|
| 150 |
lars |
32 |
public function parse(): Result
|
| 148 |
lars |
33 |
{
|
| 150 |
lars |
34 |
if ($this->lexer->current->isA(EmailLexer::S_OPENPARENTHESIS)) {
|
| 148 |
lars |
35 |
$this->openedParenthesis++;
|
| 150 |
lars |
36 |
if ($this->noClosingParenthesis()) {
|
|
|
37 |
return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value);
|
| 148 |
lars |
38 |
}
|
|
|
39 |
}
|
|
|
40 |
|
| 150 |
lars |
41 |
if ($this->lexer->current->isA(EmailLexer::S_CLOSEPARENTHESIS)) {
|
|
|
42 |
return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value);
|
| 148 |
lars |
43 |
}
|
|
|
44 |
|
|
|
45 |
$this->warnings[WarningComment::CODE] = new WarningComment();
|
|
|
46 |
|
|
|
47 |
$moreTokens = true;
|
| 150 |
lars |
48 |
while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens) {
|
| 148 |
lars |
49 |
|
|
|
50 |
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
|
|
|
51 |
$this->openedParenthesis++;
|
|
|
52 |
}
|
|
|
53 |
$this->warnEscaping();
|
| 150 |
lars |
54 |
if ($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
|
| 148 |
lars |
55 |
$this->openedParenthesis--;
|
|
|
56 |
}
|
|
|
57 |
$moreTokens = $this->lexer->moveNext();
|
|
|
58 |
}
|
|
|
59 |
|
| 150 |
lars |
60 |
if ($this->openedParenthesis >= 1) {
|
|
|
61 |
return new InvalidEmail(new UnclosedComment(), $this->lexer->current->value);
|
| 148 |
lars |
62 |
}
|
|
|
63 |
if ($this->openedParenthesis < 0) {
|
| 150 |
lars |
64 |
return new InvalidEmail(new UnOpenedComment(), $this->lexer->current->value);
|
| 148 |
lars |
65 |
}
|
|
|
66 |
|
|
|
67 |
$finalValidations = $this->commentStrategy->endOfLoopValidations($this->lexer);
|
|
|
68 |
|
|
|
69 |
$this->warnings = array_merge($this->warnings, $this->commentStrategy->getWarnings());
|
|
|
70 |
|
|
|
71 |
return $finalValidations;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* @return bool
|
|
|
77 |
*/
|
| 150 |
lars |
78 |
private function warnEscaping(): bool
|
| 148 |
lars |
79 |
{
|
|
|
80 |
//Backslash found
|
| 150 |
lars |
81 |
if (!$this->lexer->current->isA(EmailLexer::S_BACKSLASH)) {
|
| 148 |
lars |
82 |
return false;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) {
|
|
|
86 |
return false;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$this->warnings[QuotedPart::CODE] =
|
| 150 |
lars |
90 |
new QuotedPart($this->lexer->getPrevious()->type, $this->lexer->current->type);
|
| 148 |
lars |
91 |
return true;
|
|
|
92 |
}
|
|
|
93 |
|
| 150 |
lars |
94 |
private function noClosingParenthesis(): bool
|
| 148 |
lars |
95 |
{
|
|
|
96 |
try {
|
|
|
97 |
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
|
|
|
98 |
return false;
|
|
|
99 |
} catch (\RuntimeException $e) {
|
|
|
100 |
return true;
|
|
|
101 |
}
|
|
|
102 |
}
|
|
|
103 |
}
|