|
| 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 | + |
| 13 | +use yii\validators\Validator; |
| 14 | +use yii\validators\IpValidator; |
| 15 | + |
| 16 | +use PHPViet\Validation\Validator as ConcreteValidator; |
| 17 | +use PHPViet\Validation\Rules\IpVN as ConcreteIpVN; |
| 18 | + |
| 19 | +/** |
| 20 | + * @author Vuong Minh <vuongxuongminh@gmail.com> |
| 21 | + * @since 1.0.0 |
| 22 | + */ |
| 23 | +class IpVNValidator extends Validator |
| 24 | +{ |
| 25 | + |
| 26 | + const IPV4 = ConcreteIpVN::IPV4; |
| 27 | + |
| 28 | + const IPV6 = ConcreteIpVN::IPV6; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var int|null Version ip cần kiểm tra |
| 32 | + */ |
| 33 | + public $version; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritDoc |
| 37 | + */ |
| 38 | + public function init() |
| 39 | + { |
| 40 | + $this->message = $this->message ?? $this->getDefaultMessage(); |
| 41 | + |
| 42 | + parent::init(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritDoc |
| 47 | + */ |
| 48 | + public function validateValue($value) |
| 49 | + { |
| 50 | + if (ConcreteValidator::ipVN($this->version)->validate($value)) { |
| 51 | + |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + return [$this->message, []]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @inheritDoc |
| 60 | + * @throws \yii\base\InvalidConfigException |
| 61 | + */ |
| 62 | + public function clientValidateAttribute($model, $attribute, $view) |
| 63 | + { |
| 64 | + return $this->getClientIpValidator()->clientValidateAttribute($model, $attribute, $view); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritDoc |
| 69 | + * @throws \yii\base\InvalidConfigException |
| 70 | + */ |
| 71 | + public function getClientOptions($model, $attribute) |
| 72 | + { |
| 73 | + return $this->getClientIpValidator()->getClientOptions($model, $attribute); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @inheritDoc |
| 78 | + */ |
| 79 | + protected function getDefaultMessage(): string |
| 80 | + { |
| 81 | + switch ($this->version) { |
| 82 | + case self::IPV4: |
| 83 | + return Yii::t('phpviet/validation', 'ipv4'); |
| 84 | + case self::IPV6: |
| 85 | + return Yii::t('phpviet/validation', 'ipv6'); |
| 86 | + default: |
| 87 | + return Yii::t('phpviet/validation', 'ip'); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @var IpValidator |
| 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 | +} |
0 commit comments