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