|
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
|
167 |
|
public function parse() : Result |
|
19
|
|
|
{ |
|
20
|
167 |
|
if (!$this->isFWS()) { |
|
21
|
166 |
|
return new ValidEmail(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
22 |
|
$previous = $this->lexer->getPrevious(); |
|
25
|
|
|
|
|
26
|
22 |
|
$resultCRLF = $this->checkCRLFInFWS(); |
|
27
|
22 |
|
if ($resultCRLF->isInvalid()) { |
|
28
|
1 |
|
return $resultCRLF; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
21 |
|
if ($this->lexer->token['type'] === EmailLexer::S_CR) { |
|
32
|
3 |
|
return new InvalidEmail(new CRNoLF(), $this->lexer->token['value']); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
18 |
|
if ($this->lexer->isNextToken(EmailLexer::GENERIC) && $previous['type'] !== EmailLexer::S_AT) { |
|
36
|
8 |
|
return new InvalidEmail(new AtextAfterCFWS(), $this->lexer->token['value']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
12 |
|
if ($this->lexer->token['type'] === EmailLexer::S_LF || $this->lexer->token['type'] === EmailLexer::C_NUL) { |
|
40
|
|
|
return new InvalidEmail(new ExpectingCTEXT(), $this->lexer->token['value']); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
12 |
|
if ($this->lexer->isNextToken(EmailLexer::S_AT) || $previous['type'] === EmailLexer::S_AT) { |
|
44
|
3 |
|
$this->warnings[CFWSNearAt::CODE] = new CFWSNearAt(); |
|
45
|
|
|
} else { |
|
46
|
9 |
|
$this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
12 |
|
return new ValidEmail(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
22 |
|
protected function checkCRLFInFWS() : Result |
|
53
|
|
|
{ |
|
54
|
22 |
|
if ($this->lexer->token['type'] !== EmailLexer::CRLF) { |
|
55
|
21 |
|
return new ValidEmail(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
59
|
1 |
|
return new InvalidEmail(new CRLFX2(), $this->lexer->token['value']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//this has no coverage. Condition is repeated from above one |
|
63
|
|
|
if (!$this->lexer->isNextTokenAny(array(EmailLexer::S_SP, EmailLexer::S_HTAB))) { |
|
64
|
|
|
return new InvalidEmail(new CRLFAtTheEnd(), $this->lexer->token['value']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return new ValidEmail(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
167 |
|
protected function isFWS() : bool |
|
71
|
|
|
{ |
|
72
|
167 |
|
if ($this->escaped()) { |
|
73
|
1 |
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
167 |
|
return $this->lexer->token['type'] === EmailLexer::S_SP || |
|
77
|
167 |
|
$this->lexer->token['type'] === EmailLexer::S_HTAB || |
|
78
|
167 |
|
$this->lexer->token['type'] === EmailLexer::S_CR || |
|
79
|
167 |
|
$this->lexer->token['type'] === EmailLexer::S_LF || |
|
80
|
167 |
|
$this->lexer->token['type'] === EmailLexer::CRLF; |
|
81
|
|
|
} |
|
82
|
|
|
} |