IpValidator::init()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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\validators\IpValidator as BaseIpValidator;
11
12
/**
13
 * Lớp IpValidator dùng để kiểm tra `ip` Việt Nam.
14
 * Ví dụ khai báo kiểm tra ip trong model
15
 *
16
 * ```php
17
 *      public function rules() {
18
 *          return [
19
 *              ['ipAttr', 'ipvn', 'message' => 'Ip phải là ip Việt Nam!']
20
 *          ];
21
 *      }
22
 * ```
23
 *
24
 * @author Vuong Minh <[email protected]>
25
 * @since 1.0
26
 */
27
class IpValidator extends BaseIpValidator
28
{
29
30
    /**
31
     * @var array Thuộc tính hổ trợ cache range ip Việt Nam.
32
     */
33
    private static $_ipRanges = [];
34
35
    /**
36
     * @throws \yii\base\InvalidConfigException
37
     */
38 16
    public function init()
39
    {
40 16
        parent::init();
41
42 16
        if (!empty(self::$_ipRanges)) {
43 15
            $this->setRanges(self::$_ipRanges);
44
        } else {
45 1
            $this->setRanges(self::$_ipRanges = require(__DIR__ . '/resource/ip-ranges.php'));
46
        }
47 16
    }
48
49
50
}
51