IpVNValidator::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/phpviet/yii-validation
4
 *
5
 * @copyright (c) PHP Viet
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace phpviet\yii\validation\validators;
10
11
use Yii;
12
use yii\validators\Validator;
13
use yii\validators\IpValidator;
14
use PHPViet\Validation\Rules\IpVN as ConcreteIpVN;
15
use PHPViet\Validation\Validator as ConcreteValidator;
16
17
/**
18
 * @author Vuong Minh <[email protected]>
19
 *
20
 * @since 1.0.0
21
 */
22
class IpVNValidator extends Validator
23
{
24
    const IPV4 = ConcreteIpVN::IPV4;
25
26
    const IPV6 = ConcreteIpVN::IPV6;
27
28
    /**
29
     * @var int|null Version ip cần kiểm tra
30
     */
31
    public $version;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function init(): void
37
    {
38
        $this->message = $this->message ?? $this->getDefaultMessage();
39
40
        parent::init();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function validateValue($value): ?array
47
    {
48
        if (ConcreteValidator::ipVN($this->version)->validate($value)) {
49
            return null;
50
        }
51
52
        return [$this->message, []];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @throws \yii\base\InvalidConfigException
59
     */
60
    public function clientValidateAttribute($model, $attribute, $view): ?string
61
    {
62
        return $this->getClientIpValidator()->clientValidateAttribute($model, $attribute, $view);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     *
68
     * @throws \yii\base\InvalidConfigException
69
     */
70
    public function getClientOptions($model, $attribute): array
71
    {
72
        return $this->getClientIpValidator()->getClientOptions($model, $attribute);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function getDefaultMessage(): string
79
    {
80
        switch ($this->version) {
81
            case self::IPV4:
82
                return Yii::t('phpviet/validation', '{attribute} must be Vietnam ipv4.');
83
            case self::IPV6:
84
                return Yii::t('phpviet/validation', '{attribute} must be Vietnam ipv6.');
85
            default:
86
                return Yii::t('phpviet/validation', '{attribute} must be Vietnam ip.');
87
        }
88
    }
89
90
    /**
91
     * @var IpValidator
92
     *
93
     * @see getClientIpValidator()
94
     */
95
    private $_clientIpValidator;
96
97
    /**
98
     * Trả về [[IpValidator]] hổ trợ cho việc tạo js validator tại client.
99
     *
100
     * @return IpValidator
101
     * @throws \yii\base\InvalidConfigException
102
     */
103
    protected function getClientIpValidator(): IpValidator
104
    {
105
        if (null === $this->_clientIpValidator) {
106
            return $this->_clientIpValidator = Yii::createObject([
107
                'class' => IpValidator::class,
108
                'message' => $this->message,
109
                'ipv4NotAllowed' => $this->message,
110
                'ipv6NotAllowed' => $this->message,
111
                'ipv4' => null === $this->version || self::IPV4 === $this->version,
112
                'ipv6' => null === $this->version || self::IPV6 === $this->version,
113
            ]);
114
        }
115
116
        return $this->_clientIpValidator;
117
    }
118
}
119