1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EmailValidator\Validator; |
6
|
|
|
|
7
|
|
|
use EmailValidator\EmailAddress; |
8
|
|
|
use EmailValidator\Validator\LocalPart\AtomValidator; |
9
|
|
|
use EmailValidator\Validator\LocalPart\QuotedStringValidator; |
10
|
|
|
use EmailValidator\Validator\Domain\DomainNameValidator; |
11
|
|
|
use EmailValidator\Validator\Domain\DomainLiteralValidator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Main validator class that implements RFC 5322 email validation |
15
|
|
|
*/ |
16
|
|
|
class Rfc5322Validator |
17
|
|
|
{ |
18
|
|
|
private const MAX_LOCAL_PART_LENGTH = 64; |
19
|
|
|
|
20
|
|
|
private AtomValidator $atomValidator; |
21
|
|
|
private QuotedStringValidator $quotedStringValidator; |
22
|
|
|
private DomainNameValidator $domainNameValidator; |
23
|
|
|
private DomainLiteralValidator $domainLiteralValidator; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor initializes all specialized validators |
27
|
|
|
*/ |
28
|
|
|
public function __construct() |
29
|
|
|
{ |
30
|
|
|
$this->atomValidator = new AtomValidator(); |
31
|
|
|
$this->quotedStringValidator = new QuotedStringValidator(); |
32
|
|
|
$this->domainNameValidator = new DomainNameValidator(); |
33
|
|
|
$this->domainLiteralValidator = new DomainLiteralValidator(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Validates an email address according to RFC 5322 |
38
|
|
|
* |
39
|
|
|
* @param EmailAddress $email The email address to validate |
40
|
|
|
* @return bool True if the email address is valid |
41
|
|
|
*/ |
42
|
|
|
public function validate(EmailAddress $email): bool |
43
|
|
|
{ |
44
|
|
|
$localPart = $email->getLocalPart(); |
45
|
|
|
$domain = $email->getDomain(); |
46
|
|
|
|
47
|
|
|
if ($localPart === null || $domain === null) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->validateLocalPart($localPart) && $this->validateDomain($domain); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Validates the local part of an email address |
56
|
|
|
* |
57
|
|
|
* @param string $localPart The local part to validate |
58
|
|
|
* @return bool True if the local part is valid |
59
|
|
|
*/ |
60
|
|
|
private function validateLocalPart(string $localPart): bool |
61
|
|
|
{ |
62
|
|
|
// Empty local part is invalid |
63
|
|
|
if ($localPart === '') { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Check length |
68
|
|
|
if (strlen($localPart) > self::MAX_LOCAL_PART_LENGTH) { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Check if it's a quoted string |
73
|
|
|
if (substr($localPart, 0, 1) === '"') { |
74
|
|
|
return $this->quotedStringValidator->validate($localPart); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Otherwise, treat as dot-atom |
78
|
|
|
return $this->atomValidator->validate($localPart); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Validates the domain part of an email address |
83
|
|
|
* |
84
|
|
|
* @param string $domain The domain to validate |
85
|
|
|
* @return bool True if the domain is valid |
86
|
|
|
*/ |
87
|
|
|
private function validateDomain(string $domain): bool |
88
|
|
|
{ |
89
|
|
|
// Empty domain is invalid |
90
|
|
|
if ($domain === '') { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Check if it's a domain literal |
95
|
|
|
if (substr($domain, 0, 1) === '[') { |
96
|
|
|
return $this->domainLiteralValidator->validate($domain); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Otherwise, treat as domain name |
100
|
|
|
return $this->domainNameValidator->validate($domain); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|