DomainValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 32
ccs 10
cts 11
cp 0.9091
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 8 2
A validateValue() 0 10 3
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