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