Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
150 lars 2
 
148 lars 3
namespace Egulias\EmailValidator\Parser;
4
 
5
use Egulias\EmailValidator\EmailLexer;
6
use Egulias\EmailValidator\Warning\CFWSNearAt;
7
use Egulias\EmailValidator\Result\InvalidEmail;
8
use Egulias\EmailValidator\Warning\CFWSWithFWS;
9
use Egulias\EmailValidator\Result\Reason\CRNoLF;
10
use Egulias\EmailValidator\Result\Reason\AtextAfterCFWS;
11
use Egulias\EmailValidator\Result\Reason\CRLFAtTheEnd;
12
use Egulias\EmailValidator\Result\Reason\CRLFX2;
13
use Egulias\EmailValidator\Result\Reason\ExpectingCTEXT;
14
use Egulias\EmailValidator\Result\Result;
15
use Egulias\EmailValidator\Result\ValidEmail;
16
 
17
class  FoldingWhiteSpace extends PartParser
18
{
19
    public const FWS_TYPES = [
20
        EmailLexer::S_SP,
21
        EmailLexer::S_HTAB,
22
        EmailLexer::S_CR,
23
        EmailLexer::S_LF,
24
        EmailLexer::CRLF
25
    ];
26
 
150 lars 27
    public function parse(): Result
148 lars 28
    {
29
        if (!$this->isFWS()) {
30
            return new ValidEmail();
31
        }
32
 
33
        $previous = $this->lexer->getPrevious();
34
 
35
        $resultCRLF = $this->checkCRLFInFWS();
36
        if ($resultCRLF->isInvalid()) {
37
            return $resultCRLF;
38
        }
39
 
150 lars 40
        if ($this->lexer->current->isA(EmailLexer::S_CR)) {
41
            return new InvalidEmail(new CRNoLF(), $this->lexer->current->value);
148 lars 42
        }
43
 
150 lars 44
        if ($this->lexer->isNextToken(EmailLexer::GENERIC) && !$previous->isA(EmailLexer::S_AT)) {
45
            return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->current->value);
148 lars 46
        }
47
 
150 lars 48
        if ($this->lexer->current->isA(EmailLexer::S_LF) || $this->lexer->current->isA(EmailLexer::C_NUL)) {
49
            return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->current->value);
148 lars 50
        }
51
 
150 lars 52
        if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous->isA(EmailLexer::S_AT)) {
148 lars 53
            $this->warnings[CFWSNearAt::CODE] = new CFWSNearAt();
54
        } else {
55
            $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
56
        }
57
 
58
        return new ValidEmail();
59
    }
60
 
150 lars 61
    protected function checkCRLFInFWS(): Result
148 lars 62
    {
150 lars 63
        if (!$this->lexer->current->isA(EmailLexer::CRLF)) {
148 lars 64
            return new ValidEmail();
65
        }
66
 
67
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
150 lars 68
            return new InvalidEmail(new CRLFX2(), $this->lexer->current->value);
148 lars 69
        }
70
 
71
        //this has no coverage. Condition is repeated from above one
72
        if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) {
150 lars 73
            return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->current->value);
148 lars 74
        }
75
 
76
        return new ValidEmail();
77
    }
150 lars 78
 
79
    protected function isFWS(): bool
148 lars 80
    {
81
        if ($this->escaped()) {
82
            return false;
83
        }
84
 
150 lars 85
        return in_array($this->lexer->current->type, self::FWS_TYPES);
148 lars 86
    }
87
}