1 | <?php |
||
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 |
||
118 | } |
||
119 |