Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
    public function parse(string $str) : Result
29
    {
30
        $result = parent::parse($str);
31
 
32
        $this->addLongEmailWarning($this->idLeft, $this->idRight);
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->processIDLeft();
48
    }
49
 
50
    protected function parseRightFromAt(): Result
51
    {
52
        return $this->processIDRight();
53
    }
54
 
55
    private function processIDLeft() : Result
56
    {
57
        $localPartParser = new IDLeftPart($this->lexer);
58
        $localPartResult = $localPartParser->parse();
59
        $this->idLeft = $localPartParser->localPart();
60
        $this->warnings = array_merge($localPartParser->getWarnings(), $this->warnings);
61
 
62
        return $localPartResult;
63
    }
64
 
65
    private function processIDRight() : Result
66
    {
67
        $domainPartParser = new IDRightPart($this->lexer);
68
        $domainPartResult = $domainPartParser->parse();
69
        $this->idRight = $domainPartParser->domainPart();
70
        $this->warnings = array_merge($domainPartParser->getWarnings(), $this->warnings);
71
 
72
        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
    private function addLongEmailWarning(string $localPart, string $parsedDomainPart) : void
86
    {
87
        if (strlen($localPart . '@' . $parsedDomainPart) > self::EMAILID_MAX_LENGTH) {
88
            $this->warnings[EmailTooLong::CODE] = new EmailTooLong();
89
        }
90
    }
91
}