Completed
Push — master ( 10aad8...6434d9 )
by Vuong
01:55
created

TelNumValidator   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 143
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 20 4
B isUse() 0 24 7
A validateAttribute() 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 yii\base\InvalidConfigException;
11
use yii\validators\RegularExpressionValidator;
12
13
/**
14
 * Lớp TelNumValidator dùng để kiểm tra số điện thoại trong nước có hợp lệ hay không.
15
 * Ví dụ khai báo kiểm tra số điện thoại hợp lệ trong `model`
16
 *
17
 * ```php
18
 *      public function rules() {
19
 *          return [
20
 *              ['telAttr', 'telnumvn', 'message' => 'Số điện thoại phải là số trong nước']
21
 *          ];
22
 *      }
23
 * ```
24
 *
25
 * Chỉ kiểm tra số di động (bỏ số bàn)
26
 *
27
 * ```php
28
 *      public function rules() {
29
 *          return [
30
 *              ['telAttr', 'telnumvn', 'message' => 'Số điện thoại phải là số trong nước', 'exceptTelco' => 'landLine']
31
 *          ];
32
 *      }
33
 * ```
34
 *
35
 * Chỉ kiểm tra số bàn (bỏ số di động)
36
 *
37
 * ```php
38
 *      public function rules() {
39
 *          return [
40
 *              ['telAttr', 'telnumvn', 'message' => 'Số điện thoại phải là số trong nước', 'onlyTelco' => 'landLine']
41
 *          ];
42
 *      }
43
 * ```
44
 *
45
 * danh sách các telco (nhà mạng) hổ trợ: mobiFone, vinaPhone, viettel, vietNamMobile, gMobile, beeline, vsat, indoChina, landLine.
46
 *
47
 * Sử dụng trong DynamicModel tương tự.
48
 *
49
 * @author Vuong Minh <[email protected]>
50
 * @since 1.0
51
 */
52
class TelNumValidator extends RegularExpressionValidator
53
{
54
55
    /**
56
     * @var array Mảng chứa tên các telco (nhà mạng). Các nhà mạng nằm trong mảng sẽ là kiểu ràng buộc của dữ liệu.
57
     * Lưu ý nếu như tên telco cũng nằm ở mảng `exceptTelco` thì mặc định telco đó sẽ bị loại bỏ.
58
     *
59
     * Nếu mảng là rổng động nghĩa với dữ liệu kiểm tra chỉ cần là số điện thoại Việt Nam.
60
     *
61
     * @see exceptTelco
62
     */
63
    public $onlyTelco = [];
64
65
    /**
66
     * @var array Mảng chứa tên các telco (nhà mạng). Các nhà mạng nằm trong mảng sẽ được bỏ qua (không kiểm tra).
67
     * @see onlyTelco
68
     */
69
    public $exceptTelco = [];
70
71
    /**
72
     * @var string Pattern kiểm tra số viettel.
73
     */
74
    public $viettel = '^(\+?84|0)?(9[6-8]|3[2-9]|86)\d{7}$';
75
76
    /**
77
     * @var string Pattern kiểm tra số vinaphone.
78
     */
79
    public $vinaPhone = '^(\+?84|0)?(9[14]|8[1-5]|88)\d{7}$';
80
81
    /**
82
     * @var string Pattern kiểm tra số mobiphone.
83
     */
84
    public $mobiFone = '^(\+?84|0)?(9[03]|7[6-8]|7[09]|89)\d{7}$';
85
86
    /**
87
     * @var string Pattern kiểm tra số vnmobi.
88
     */
89
    public $vietNamMobile = '^(\+?84|0)?(92|5[68]|82)[\d]{7}$';
90
91
    /**
92
     * @var string Pattern kiểm tra số gmobile.
93
     */
94
    public $gMobile = '^(\+?84|0)?59[\d]{7}$';
95
96
    /**
97
     * @var string Pattern kiểm tra số beeline.
98
     */
99
    public $beeline = '^(\+?84|0)?99[3-7][\d]{7}$';
100
101
    /**
102
     * @var string Pattern kiểm tra số vsat.
103
     */
104
    public $vsat = '^(\+?84|0)?992[\d]{7}$';
105
106
    /**
107
     * @var string Pattern kiểm tra số indochina.
108
     */
109
    public $indoChina = '^(\+?84|0)?99[89][\d]{7}$';
110
111
    /**
112
     * @var string Pattern kiểm tra số điện thoại bàn.
113
     */
114
    public $landLine = '^(\+?84|0)?(((20[3-9]|21[0-6]|21[89]|22[0-2]|22[5-9]|23[2-9]|24[2-5]|248|25[12]|25[4-9]|26[0-3]|27[0-7]|28[2-5]|29([0-4]|[67])|299)\d{7})|((246[236]|247[13]|286[23]|287[13])\d{6}))$';
115
116
    /**
117
     * @var bool|string Thiết lập kiểu `format` di động thêm '0' sau khi thực thi kiểm tra hoàn tất (dữ liệu attr hợp lệ).
118
     * Nó dùng để chuẩn hóa dữ liệu cho bạn. Mặc định `FALSE` sẽ không can thiệp đến dữ liệu của bạn.
119
     */
120
    public $mobileFormat = false;
121
122
    /**
123
     * @inheritdoc
124
     * @throws InvalidConfigException
125
     */
126 12
    public function init()
127
    {
128 12
        $this->onlyTelco = (array)$this->onlyTelco;
129 12
        $this->exceptTelco = (array)$this->exceptTelco;
130
131 12
        $pattern = [];
132 12
        foreach (['viettel', 'mobiFone', 'vinaPhone', 'indoChina', 'gMobile', 'beeline', 'vsat', 'landLine'] as $telco) {
133 12
            if ($this->isUse($telco)) {
134 12
                $pattern[] = $this->{$telco};
135
            }
136
        }
137
138 12
        if (!empty($pattern)) {
139 12
            $this->pattern = "~(" . implode(")|(", $pattern) . ")~";
140
        } else {
141
            throw new InvalidConfigException('Your telco setup is not valid!');
142
        }
143
144 12
        parent::init();
145 12
    }
146
147
148
    /**
149
     * Phương thức kiểm tra telco (nhà mạng) có phải là thành phần cần kiểm tra hay không.
150
     * Nó được trích từ [yii\base\ActionFilter::isActive()].
151
     *
152
     * @param string $telco Tên nhà mạng cần kiểm tra
153
     * @return bool Trả về `TRUE` nếu như telco la thành phần kiểm tra.
154
     */
155 12
    protected function isUse($telco)
156
    {
157 12
        if (empty($this->onlyTelco)) {
158 12
            $only = true;
159
        } else {
160 1
            $only = false;
161 1
            foreach ($this->onlyTelco as $expect) {
162 1
                if ($telco === $expect) {
163 1
                    $only = true;
164 1
                    break;
165
                }
166
            }
167
        }
168
169 12
        $except = false;
170 12
        foreach ($this->exceptTelco as $expect) {
171 1
            if ($telco === $expect) {
172 1
                $except = true;
173 1
                break;
174
            }
175
        }
176
177 12
        return $only && !$except;
178
    }
179
180
    /**
181
     * @inheritdoc
182
     */
183 12
    public function validateAttribute($model, $attribute)
184
    {
185 12
        if (($result = parent::validateAttribute($model, $attribute)) === null) {
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as parent::validateAttribute($model, $attribute) (which targets yii\validators\Validator::validateAttribute()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
186 12
            if ($this->mobileFormat) {
187 5
                $model->{$attribute} = preg_replace('/^(\+?84|0)?(\d+)$/', '0$2', $model->{$attribute});
188
            }
189
        }
190
191 12
        return $result;
192
    }
193
194
}
195