DomainValidator::validateValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\craft\domains\validators;
10
11
use Craft;
12
use yii\validators\Validator;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
class DomainValidator extends Validator
19
{
20
    /**
21
     * The Domain pattern
22
     */
23
    const PATTERN = '/^(?!\-)(?:[a-zA-Z\d\-]{0,62}[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}$/';
24
25
    /**
26
     * @inheritdoc
27
     */
28
    protected function validateValue($value)
29
    {
30
        if ($value) {
31
            if (preg_match(self::PATTERN, $value) !== 1) {
32
                return [
33
                    Craft::t('domains', "Invalid domain '{domain}'", ['domain' => $value]),
34
                    []
35
                ];
36
            }
37
        }
38
39
        return null;
40
    }
41
}
42