Total Complexity | 11 |
Total Lines | 76 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | class Email extends AbstractRule |
||
13 | { |
||
14 | /** |
||
15 | * Whether to check MX record. |
||
16 | * |
||
17 | * @var bool |
||
18 | */ |
||
19 | private $mx; |
||
20 | |||
21 | /** |
||
22 | * Whether to check host. |
||
23 | * |
||
24 | * @var bool |
||
25 | */ |
||
26 | private $host; |
||
27 | |||
28 | /** |
||
29 | * Email validation rule constructor. |
||
30 | * |
||
31 | * @param bool $mx |
||
32 | * @param bool $host |
||
33 | */ |
||
34 | public function __construct($mx = false, $host = false) |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Validates input. |
||
42 | * |
||
43 | * @param mixed $input |
||
44 | * |
||
45 | * @return bool |
||
46 | */ |
||
47 | public function isValid($input = null) |
||
48 | { |
||
49 | if (!preg_match('/^.+\@\S+\.\S+$/', $input)) { |
||
|
|||
50 | return false; |
||
51 | } |
||
52 | |||
53 | $host = substr($input, strrpos($input, '@') + 1); |
||
54 | |||
55 | if ($this->mx && !$this->checkMX($host)) { |
||
56 | $this->violations[] = 'mx'; |
||
57 | } |
||
58 | |||
59 | if ($this->host && !$this->checkHost($host)) { |
||
60 | $this->violations[] = 'host'; |
||
61 | } |
||
62 | |||
63 | return !$this->hasViolations(); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Checks DNS records for MX type. |
||
68 | * |
||
69 | * @param string $host |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | private function checkMX($host) |
||
74 | { |
||
75 | return checkdnsrr($host, 'MX'); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Check if one of MX, A or AAAA DNS RR exists. |
||
80 | * |
||
81 | * @param string $host |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | private function checkHost($host) |
||
88 | } |
||
89 | } |
||
90 |