|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator; |
|
4
|
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\Parser; |
|
6
|
|
|
use Egulias\EmailValidator\EmailLexer; |
|
7
|
|
|
use Egulias\EmailValidator\Result\Result; |
|
8
|
|
|
use Egulias\EmailValidator\Parser\IDLeftPart; |
|
9
|
|
|
use Egulias\EmailValidator\Parser\IDRightPart; |
|
10
|
|
|
use Egulias\EmailValidator\Result\ValidEmail; |
|
11
|
|
|
use Egulias\EmailValidator\Result\InvalidEmail; |
|
12
|
|
|
use Egulias\EmailValidator\Warning\EmailTooLong; |
|
13
|
|
|
use Egulias\EmailValidator\Result\Reason\NoLocalPart; |
|
14
|
|
|
|
|
15
|
|
|
class MessageIDParser extends Parser |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
const EMAILID_MAX_LENGTH = 254; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $idLeft = ''; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $idRight = ''; |
|
29
|
|
|
|
|
30
|
9 |
|
public function __construct(EmailLexer $lexer) |
|
31
|
|
|
{ |
|
32
|
9 |
|
$this->lexer = $lexer; |
|
33
|
9 |
|
} |
|
34
|
|
|
|
|
35
|
9 |
|
public function parse(string $str) : Result |
|
36
|
|
|
{ |
|
37
|
9 |
|
$result = parent::parse($str); |
|
38
|
|
|
|
|
39
|
9 |
|
$this->addLongEmailWarning($this->idLeft, $this->idRight); |
|
40
|
|
|
|
|
41
|
9 |
|
return $result; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
9 |
|
protected function preLeftParsing(): Result |
|
45
|
|
|
{ |
|
46
|
9 |
|
if (!$this->hasAtToken()) { |
|
47
|
|
|
return new InvalidEmail(new NoLocalPart(), $this->lexer->token["value"]); |
|
48
|
|
|
} |
|
49
|
9 |
|
return new ValidEmail(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
9 |
|
protected function parseLeftFromAt(): Result |
|
53
|
|
|
{ |
|
54
|
9 |
|
return $this->processIDLeft(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
7 |
|
protected function parseRightFromAt(): Result |
|
58
|
|
|
{ |
|
59
|
7 |
|
return $this->processIDRight(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
9 |
|
private function processIDLeft() : Result |
|
63
|
|
|
{ |
|
64
|
9 |
|
$localPartParser = new IDLeftPart($this->lexer); |
|
65
|
9 |
|
$localPartResult = $localPartParser->parse(); |
|
66
|
9 |
|
$this->idLeft = $localPartParser->localPart(); |
|
67
|
9 |
|
$this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings); |
|
68
|
|
|
|
|
69
|
9 |
|
return $localPartResult; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
7 |
|
private function processIDRight() : Result |
|
73
|
|
|
{ |
|
74
|
7 |
|
$domainPartParser = new IDRightPart($this->lexer); |
|
75
|
7 |
|
$domainPartResult = $domainPartParser->parse(); |
|
76
|
7 |
|
$this->idRight = $domainPartParser->domainPart(); |
|
77
|
7 |
|
$this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings); |
|
78
|
|
|
|
|
79
|
7 |
|
return $domainPartResult; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getLeftPart() : string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->idLeft; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getRightPart() : string |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->idRight; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
9 |
|
private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void |
|
93
|
|
|
{ |
|
94
|
9 |
|
if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) { |
|
95
|
|
|
$this->warnings[EmailTooLong::CODE] = new EmailTooLong(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |