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