| 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 |
|
|
|
32 |
public function parse() : Result
|
|
|
33 |
{
|
|
|
34 |
if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
|
|
|
35 |
$this->openedParenthesis++;
|
|
|
36 |
if($this->noClosingParenthesis()) {
|
|
|
37 |
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
|
|
|
42 |
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
$this->warnings[WarningComment::CODE] = new WarningComment();
|
|
|
46 |
|
|
|
47 |
$moreTokens = true;
|
|
|
48 |
while ($this->commentStrategy->exitCondition($this->lexer, $this->openedParenthesis) && $moreTokens){
|
|
|
49 |
|
|
|
50 |
if ($this->lexer->isNextToken(EmailLexer::S_OPENPARENTHESIS)) {
|
|
|
51 |
$this->openedParenthesis++;
|
|
|
52 |
}
|
|
|
53 |
$this->warnEscaping();
|
|
|
54 |
if($this->lexer->isNextToken(EmailLexer::S_CLOSEPARENTHESIS)) {
|
|
|
55 |
$this->openedParenthesis--;
|
|
|
56 |
}
|
|
|
57 |
$moreTokens = $this->lexer->moveNext();
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if($this->openedParenthesis >= 1) {
|
|
|
61 |
return new InvalidEmail(new UnclosedComment(), $this->lexer->token['value']);
|
|
|
62 |
}
|
|
|
63 |
if ($this->openedParenthesis < 0) {
|
|
|
64 |
return new InvalidEmail(new UnOpenedComment(), $this->lexer->token['value']);
|
|
|
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 |
*/
|
|
|
78 |
private function warnEscaping() : bool
|
|
|
79 |
{
|
|
|
80 |
//Backslash found
|
|
|
81 |
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) {
|
|
|
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] =
|
|
|
90 |
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']);
|
|
|
91 |
return true;
|
|
|
92 |
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
private function noClosingParenthesis() : bool
|
|
|
96 |
{
|
|
|
97 |
try {
|
|
|
98 |
$this->lexer->find(EmailLexer::S_CLOSEPARENTHESIS);
|
|
|
99 |
return false;
|
|
|
100 |
} catch (\RuntimeException $e) {
|
|
|
101 |
return true;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|