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

DomainRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 domain
15
 *
16
 * @package Bluz\Validator\Rule
17
 */
18
class DomainRule extends AbstractRule
19
{
20
    /**
21
     * @var string error template
22
     */
23
    protected $description = 'must be a valid domain';
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 8
    public function __construct($checkDns = false)
36
    {
37 8
        $this->checkDns = $checkDns;
38 8
    }
39
40
    /**
41
     * Check input data
42
     *
43
     * @param  string $input
44
     *
45
     * @return bool
46
     */
47 8
    public function validate($input): bool
48
    {
49 8
        $input = (string)$input;
50
        // check by regular expression
51 8
        if (preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-z\d](-*[a-z\d])*))*$/i", $input)
52 8
            && preg_match("/^.{1,253}$/", $input)
53 8
            && preg_match("/^[^\.]{1,63}(\.[^\.]{1,63})*$/", $input)
54
        ) {
55
            // check by DNS record
56 4
            if ($this->checkDns) {
57
                return checkdnsrr($input, 'A');
58
            }
59 4
            return true;
60
        }
61 4
        return false;
62
    }
63
}
64