1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator\Parser; |
4
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\EmailLexer; |
6
|
|
|
use Egulias\EmailValidator\Exception\CRLFAtTheEnd; |
7
|
|
|
use Egulias\EmailValidator\Exception\CRLFX2; |
8
|
|
|
use Egulias\EmailValidator\Exception\ExpectingATEXT; |
9
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
10
|
|
|
use Egulias\EmailValidator\Result\Reason\ConsecutiveDot; |
11
|
|
|
use Egulias\EmailValidator\Result\Reason\ExpectingATEXT as ReasonExpectingATEXT; |
12
|
|
|
use Egulias\EmailValidator\Result\Result; |
13
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
14
|
|
|
use Egulias\EmailValidator\Warning\QuotedPart; |
15
|
|
|
|
16
|
|
|
abstract class Parser |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Egulias\EmailValidator\Warning\Warning[] |
20
|
|
|
*/ |
21
|
|
|
protected $warnings = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var EmailLexer |
25
|
|
|
*/ |
26
|
|
|
protected $lexer; |
27
|
|
|
|
28
|
|
|
public function __construct(EmailLexer $lexer) |
29
|
|
|
{ |
30
|
|
|
$this->lexer = $lexer; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return \Egulias\EmailValidator\Warning\Warning[] |
35
|
|
|
*/ |
36
|
|
|
public function getWarnings() |
37
|
|
|
{ |
38
|
|
|
return $this->warnings; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $str |
43
|
|
|
*/ |
44
|
|
|
abstract public function parse($str); |
45
|
|
|
|
46
|
|
View Code Duplication |
protected function parseFWS() : Result |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$foldingWS = new FoldingWhiteSpace($this->lexer); |
49
|
|
|
$resultFWS = $foldingWS->parse('remove'); |
50
|
|
|
$this->warnings = array_merge($this->warnings, $foldingWS->getWarnings()); |
51
|
|
|
return $resultFWS; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function checkConsecutiveDots() : Result |
55
|
|
|
{ |
56
|
|
View Code Duplication |
if ($this->lexer->token['type'] === EmailLexer::S_DOT && $this->lexer->isNextToken(EmailLexer::S_DOT)) { |
|
|
|
|
57
|
|
|
return new InvalidEmail(new ConsecutiveDot(), $this->lexer->token['value']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return new ValidEmail(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
protected function escaped() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$previous = $this->lexer->getPrevious(); |
69
|
|
|
|
70
|
|
|
return $previous && $previous['type'] === EmailLexer::S_BACKSLASH |
|
|
|
|
71
|
|
|
&& |
72
|
|
|
$this->lexer->token['type'] !== EmailLexer::GENERIC; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected 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->isNextToken(EmailLexer::GENERIC)) { |
86
|
|
|
throw new ExpectingATEXT(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
View Code Duplication |
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->warnings[QuotedPart::CODE] = |
94
|
|
|
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
95
|
|
|
return true; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function validateEscaping() : Result |
100
|
|
|
{ |
101
|
|
|
//Backslash found |
102
|
|
|
if ($this->lexer->token['type'] !== EmailLexer::S_BACKSLASH) { |
103
|
|
|
return new ValidEmail(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->lexer->isNextToken(EmailLexer::GENERIC)) { |
107
|
|
|
return new InvalidEmail(new ReasonExpectingATEXT('Found ATOM after escaping'), $this->lexer->token['value']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB, EmailLexer::C_DEL))) { |
|
|
|
|
111
|
|
|
return new ValidEmail(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->warnings[QuotedPart::CODE] = |
115
|
|
|
new QuotedPart($this->lexer->getPrevious()['type'], $this->lexer->token['type']); |
116
|
|
|
|
117
|
|
|
return new ValidEmail(); |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.