TelNumValidator::validateAttribute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 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. Nó được phát triển theo tài liệu wiki
15
 * link: https://vi.wikipedia.org/wiki/M%C3%A3_%C4%91i%E1%BB%87n_tho%E1%BA%A1i_Vi%E1%BB%87t_Nam
16
 *
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
    /**
58
     * @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.
59
     * 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ỏ.
60
     *
61
     * 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.
62
     *
63
     * @see exceptTelco
64
     */
65
    public $onlyTelco = [];
66
67
    /**
68
     * @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).
69
     * @see onlyTelco
70
     */
71
    public $exceptTelco = [];
72
73
    /**
74
     * @var string Pattern kiểm tra số viettel.
75
     */
76
    public $viettel = '^(\+?84|0)(3[2-9]|86|9[6-8])\d{7}$';
77
78
    /**
79
     * @var string Pattern kiểm tra số vinaphone.
80
     */
81
    public $vinaPhone = '^(\+?84|0)(8[1-5]|88|9[14])\d{7}$';
82
83
    /**
84
     * @var string Pattern kiểm tra số mobiphone.
85
     */
86
    public $mobiFone = '^(\+?84|0)(70|7[6-9]|89|9[03])\d{7}$';
87
88
    /**
89
     * @var string Pattern kiểm tra số vnmobi.
90
     */
91
    public $vietNamMobile = '^(\+?84|0)(5[68]|92)[\d]{7}$';
92
93
    /**
94
     * @var string Pattern kiểm tra số gmobile.
95
     */
96
    public $gMobile = '^(\+?84|0)([59]9|95)[\d]{7}$';
97
98
    /**
99
     * @var string Pattern kiểm tra số indochina.
100
     */
101
    public $indoChina = '^(\+?84|0)87[\d]{7}$';
102
103
    /**
104
     * @var string Pattern kiểm tra số điện thoại bàn.
105
     */
106
    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}))$';
107
108
    /**
109
     * @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ệ).
110
     * 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.
111
     */
112
    public $mobileFormat = false;
113
114
    /**
115
     * @inheritdoc
116
     * @throws InvalidConfigException
117
     */
118 12
    public function init()
119
    {
120 12
        $this->onlyTelco = (array)$this->onlyTelco;
121 12
        $this->exceptTelco = (array)$this->exceptTelco;
122
123 12
        $pattern = [];
124 12
        foreach (['viettel', 'vinaPhone', 'mobiFone', 'vietNamMobile', 'indoChina', 'gMobile', 'landLine'] as $telco) {
125 12
            if ($this->isUse($telco)) {
126 12
                $pattern[] = $this->{$telco};
127
            }
128
        }
129
130 12
        if (!empty($pattern)) {
131 12
            $this->pattern = "~(" . implode(")|(", $pattern) . ")~";
132
        } else {
133
            throw new InvalidConfigException('Your telco setup is not valid!');
134
        }
135
136 12
        parent::init();
137 12
    }
138
139
140
    /**
141
     * 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.
142
     * Nó được trích từ [yii\base\ActionFilter::isActive()].
143
     *
144
     * @param string $telco Tên nhà mạng cần kiểm tra
145
     * @return bool Trả về `TRUE` nếu như telco la thành phần kiểm tra.
146
     */
147 12
    protected function isUse($telco)
148
    {
149 12
        if (empty($this->onlyTelco)) {
150 12
            $only = true;
151
        } else {
152 1
            $only = false;
153 1
            foreach ($this->onlyTelco as $expect) {
154 1
                if ($telco === $expect) {
155 1
                    $only = true;
156 1
                    break;
157
                }
158
            }
159
        }
160
161 12
        $except = false;
162 12
        foreach ($this->exceptTelco as $expect) {
163 1
            if ($telco === $expect) {
164 1
                $except = true;
165 1
                break;
166
            }
167
        }
168
169 12
        return $only && !$except;
170
    }
171
172
    /**
173
     * @inheritdoc
174
     */
175 12
    public function validateAttribute($model, $attribute)
176
    {
177 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...
178 12
            if ($this->mobileFormat) {
179 5
                $model->{$attribute} = preg_replace('/^(\+?84|0)?(\d+)$/', '0$2', $model->{$attribute});
180
            }
181
        }
182
183 12
        return $result;
184
    }
185
186
}
187