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