Completed
Push — master ( 38deb0...7544ac )
by Anton
11s
created

EmailRule   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 11 5
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Validator\Rule;
12
13
/**
14
 * Check for email
15
 *
16
 * @package Bluz\Validator\Rule
17
 */
18
class EmailRule extends AbstractRule
19
{
20
    /**
21
     * @var string error template
22
     */
23
    protected $description = 'must be a valid email address';
24
25
    /**
26
     * @var bool check DNS record flag
27
     */
28
    protected $checkDns;
29
30
    /**
31
     * Setup validation rule
32
     *
33
     * @param bool $checkDns
34
     */
35 17
    public function __construct($checkDns = false)
36
    {
37 17
        $this->checkDns = $checkDns;
38 17
    }
39
40
    /**
41
     * Check input data
42
     *
43
     * @param  mixed $input
44
     *
45
     * @return bool
46
     */
47 17
    public function validate($input): bool
48
    {
49 17
        if (is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL)) {
50 6
            list(, $domain) = explode('@', $input, 2);
51 6
            if ($this->checkDns) {
52
                return checkdnsrr($domain, 'MX') || checkdnsrr($domain, 'A');
53
            }
54 6
            return true;
55
        }
56 11
        return false;
57
    }
58
}
59