Completed
Push — master ( 81f1ce...19e6c6 )
by Vuong
01:29
created

TelNumValidator::isUse()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 6.7272
cc 7
eloc 15
nc 24
nop 1
crap 7
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
12
use yii\validators\RegularExpressionValidator;
13
14
15
/**
16
 * Lớp TelNumValidator dùng để kiểm tra số điện thoại trong nước có hợp lệ hay không.
17
 * Ví dụ khai báo kiểm tra số điện thoại hợp lệ trong `model`
18
 *
19
 * ```php
20
 *      public function rules() {
21
 *          return [
22
 *              ['telAttr', 'telnumvn', 'message' => 'Số điện thoại phải là số trong nước']
23
 *          ];
24
 *      }
25
 * ```
26
 *
27
 * Chỉ kiểm tra số di động (bỏ số bàn)
28
 *
29
 * ```php
30
 *      public function rules() {
31
 *          return [
32
 *              ['telAttr', 'telnumvn', 'message' => 'Số điện thoại phải là số trong nước', 'exceptTelco' => 'landLine']
33
 *          ];
34
 *      }
35
 * ```
36
 *
37
 * Chỉ kiểm tra số bàn (bỏ số di động)
38
 *
39
 * ```php
40
 *      public function rules() {
41
 *          return [
42
 *              ['telAttr', 'telnumvn', 'message' => 'Số điện thoại phải là số trong nước', 'onlyTelco' => 'landLine']
43
 *          ];
44
 *      }
45
 * ```
46
 *
47
 * danh sách các telco (nhà mạng) hổ trợ: mobiFone, vinaPhone, viettel, vietNamMobile, gMobile, beeline, vsat, indoChina, landLine.
48
 *
49
 * Sử dụng trong DynamicModel tương tự.
50
 *
51
 * @author Vuong Minh <[email protected]>
52
 * @since 1.0
53
 */
54
class TelNumValidator extends RegularExpressionValidator
55
{
56
    /**
57
     * @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.
58
     * 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ỏ.
59
     *
60
     * 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.
61
     *
62
     * @see exceptTelco
63
     */
64
    public $onlyTelco = [];
65
66
    /**
67
     * @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).
68
     * @see onlyTelco
69
     */
70
    public $exceptTelco = [];
71
72
    /**
73
     * @var string Pattern kiểm tra số viettel.
74
     */
75
    public $viettel = '^(\+?84|0)?(9[6-8]|16[2-9]|86)\d{7}$';
76
77
    /**
78
     * @var string Pattern kiểm tra số vinaphone.
79
     */
80
    public $vinaPhone = '^(\+?84|0)?(9[14]|12[3-5]|12[79]|88)\d{7}$';
81
82
    /**
83
     * @var string Pattern kiểm tra số mobiphone.
84
     */
85
    public $mobiFone = '^(\+?84|0)?(9[03]|12[0-2]|12[68]|89)\d{7}$';
86
87
    /**
88
     * @var string Pattern kiểm tra số vnmobi.
89
     */
90
    public $vietNamMobile = '^(\+?84|0)?(92|18[68]|82)[\d]{7}$';
91
92
    /**
93
     * @var string Pattern kiểm tra số gmobile.
94
     */
95
    public $gMobile = '^(\+?84|0)?199[\d]{7}$';
96
97
    /**
98
     * @var string Pattern kiểm tra số beeline.
99
     */
100
    public $beeline = '^(\+?84|0)?99[3-7][\d]{7}$';
101
102
    /**
103
     * @var string Pattern kiểm tra số vsat.
104
     */
105
    public $vsat = '^(\+?84|0)?992[\d]{7}$';
106
107
    /**
108
     * @var string Pattern kiểm tra số indochina.
109
     */
110
    public $indoChina = '^(\+?84|0)?99[89][\d]{7}$';
111
112
    /**
113
     * @var string Pattern kiểm tra số điện thoại bàn.
114
     */
115
    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}))$';
116
117
    /**
118
     * @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ệ).
119
     * 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.
120
     */
121
    public $mobileFormat = false;
122
123
    /**
124
     * @inheritdoc
125
     * @throws InvalidConfigException
126
     */
127 12
    public function init()
128
    {
129 12
        $this->onlyTelco = (array)$this->onlyTelco;
130 12
        $this->exceptTelco = (array)$this->exceptTelco;
131
132 12
        $pattern = [];
133 12
        foreach (['viettel', 'mobiFone', 'vinaPhone', 'indoChina', 'gMobile', 'beeline', 'vsat', 'landLine'] as $telco) {
134 12
            if ($this->isUse($telco)) {
135 12
                $pattern[] = $this->{$telco};
136
            }
137
        }
138
139 12
        if (!empty($pattern)) {
140 12
            $this->pattern = "/(" . implode(")|(", $pattern) . ")/";
141
        } else {
142
            throw new InvalidConfigException('Your telco setup is not valid!');
143
        }
144
145 12
        parent::init();
146 12
    }
147
148
149
    /**
150
     * 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.
151
     * Nó được trích từ [yii\base\ActionFilter::isActive()].
152
     *
153
     * @param string $telco Tên nhà mạng cần kiểm tra
154
     * @return bool Trả về `TRUE` nếu như telco la thành phần kiểm tra.
155
     */
156 12
    protected function isUse($telco)
157
    {
158 12
        if (empty($this->onlyTelco)) {
159 12
            $only = true;
160
        } else {
161 1
            $only = false;
162 1
            foreach ($this->onlyTelco as $expect) {
163 1
                if ($telco === $expect) {
164 1
                    $only = true;
165 1
                    break;
166
                }
167
            }
168
        }
169
170 12
        $except = false;
171 12
        foreach ($this->exceptTelco as $expect) {
172 1
            if ($telco === $expect) {
173 1
                $except = true;
174 1
                break;
175
            }
176
        }
177
178 12
        return $only && !$except;
179
    }
180
181
    /**
182
     * @inheritdoc
183
     */
184 12
    public function validateAttribute($model, $attribute)
185
    {
186 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...
187 12
            if ($this->mobileFormat) {
188 5
                $model->{$attribute} = preg_replace('/^(\+?84|0)?(\d+)$/', '0$2', $model->{$attribute});
189
            }
190
191 12
            return null;
192
        } else {
193
            return $result;
194
        }
195
    }
196
197
}
198