|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Egulias\EmailValidator; |
|
4
|
|
|
|
|
5
|
|
|
use Egulias\EmailValidator\Warning\NoDNSMXRecord; |
|
6
|
|
|
use Egulias\EmailValidator\Warning\NoDNSRecord; |
|
7
|
|
|
use Egulias\EmailValidator\Warning\DomainLiteral; |
|
8
|
|
|
use Egulias\EmailValidator\Warning\TLD; |
|
9
|
|
|
use Egulias\EmailValidator\Validation\EmailValidation; |
|
10
|
|
|
|
|
11
|
|
|
class EmailValidator |
|
12
|
|
|
{ |
|
13
|
|
|
const ERR_DEPREC_REACHED = 151; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var EmailLexer |
|
17
|
|
|
*/ |
|
18
|
|
|
private $lexer; |
|
19
|
|
|
protected $warnings; |
|
20
|
|
|
protected $error; |
|
21
|
|
|
protected $threshold = 255; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->lexer = new EmailLexer(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param $email |
|
30
|
|
|
* @param EmailValidation $emailValidation |
|
31
|
|
|
* @return bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public function isValid($email, EmailValidation $emailValidation) |
|
34
|
|
|
{ |
|
35
|
|
|
$isValid = $emailValidation->isValid($email, $this->lexer); |
|
36
|
|
|
$this->warnings = $emailValidation->getWarnings(); |
|
37
|
|
|
$this->error = $emailValidation->getError(); |
|
38
|
|
|
return $isValid; |
|
39
|
|
|
|
|
40
|
|
|
// if ($this->hasWarnings() && ((int) max($this->warnings) > $this->threshold)) { |
|
41
|
|
|
// $this->error = self::ERR_DEPREC_REACHED; |
|
42
|
|
|
// |
|
43
|
|
|
// return false; |
|
44
|
|
|
// } |
|
45
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return boolean |
|
50
|
|
|
*/ |
|
51
|
|
|
public function hasWarnings() |
|
52
|
|
|
{ |
|
53
|
|
|
return !empty($this->warnings); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getWarnings() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->warnings; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getError() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->error; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function checkDNS() |
|
73
|
|
|
{ |
|
74
|
|
|
$checked = true; |
|
75
|
|
|
$MXresult = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'MX'); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
if (!$MXresult) { |
|
78
|
|
|
$this->warnings[NoDNSMXRecord::CODE] = new NoDNSMXRecord(); |
|
79
|
|
|
$Aresult = checkdnsrr(trim($this->parser->getParsedDomainPart()), 'A'); |
|
80
|
|
|
if (!$Aresult) { |
|
81
|
|
|
$this->warnings[NoDNSRecord::CODE] = new NoDNSRecord(); |
|
82
|
|
|
$checked = false; |
|
83
|
|
|
$this->addTLDWarnings(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return $checked; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
protected function addTLDWarnings() |
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($this->warnings[NoDNSMXRecord::CODE]) && |
|
92
|
|
|
!isset($this->warnings[NoDNSRecord::CODE]) && |
|
93
|
|
|
isset($this->warnings[DomainLiteral::CODE]) |
|
94
|
|
|
) { |
|
95
|
|
|
$this->warnings[TLD::CODE] = new TLD(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: