DomainValidator::validateValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.0416
1
<?php
2
/**
3
 * @link https://github.com/yiiviet/yii2-validator
4
 * @copyright Copyright (c) 2018 Yii Viet
5
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
6
 */
7
8
namespace yiiviet\validator;
9
10
use kdn\yii2\validators\DomainValidator as BaseDomainValidator;
11
12
/**
13
 * Lớp DomainValidator dùng để kiểm tra tên miền có phải là của Việt Nam hay không.
14
 * Ví dụ khai báo kiểm tra domain Việt Nam trong `model`
15
 *
16
 * ```php
17
 *      public function rules() {
18
 *          return [
19
 *              ['domainAttr', 'domainvn', 'message' => 'Tên miền không phải miền VN!']
20
 *          ];
21
 *      }
22
 * ```
23
 *
24
 * @author Vuong Minh <[email protected]>
25
 * @since 1.0
26
 */
27
class DomainValidator extends BaseDomainValidator
28
{
29
30
    /**
31
     * @inheritdoc
32
     * @throws \yii\base\InvalidConfigException
33
     */
34 15
    public function init()
35
    {
36 15
        parent::init();
37
38 15
        if (empty($this->message)) {
39 15
            $this->message = '{attribute} is invalid `.vn` domain!';
40
        }
41 15
    }
42
43
    /**
44
     * @inheritdoc
45
     * @throws \yii\base\NotSupportedException
46
     */
47 15
    public function validateValue($value)
48
    {
49 15
        if ($result = parent::validateValue($value)) {
50
            return $result;
51 15
        } elseif (!preg_match('/\.vn$/', $value)) {
52 5
            return [$this->message, []];
53
        } else {
54 10
            return null;
55
        }
56
    }
57
58
}
59