floor12 /
yii2-phone
| 1 | <?php |
||
| 2 | |||
| 3 | |||
| 4 | namespace floor12\phone; |
||
| 5 | |||
| 6 | use Yii; |
||
| 7 | use yii\base\Model; |
||
| 8 | use yii\validators\Validator; |
||
| 9 | use yii\web\View; |
||
| 10 | |||
| 11 | class PhoneValidator extends Validator |
||
| 12 | { |
||
| 13 | |||
| 14 | protected $formatError; |
||
| 15 | protected $lengthError; |
||
| 16 | |||
| 17 | 10 | public function init() |
|
| 18 | { |
||
| 19 | 10 | self::registerTranslations(); |
|
| 20 | 10 | $this->formatError = Yii::t('app.floor12.phone', 'The phone number must contain only numbers.'); |
|
| 21 | 10 | $this->lengthError = Yii::t('app.floor12.phone', 'The phone number must be 11 to 15 digits long.'); |
|
| 22 | 10 | Yii::$app->getView()->registerJs(sprintf('f12_phone_error_format = "%s";', $this->formatError), View::POS_END); |
|
| 23 | 10 | Yii::$app->getView()->registerJs(sprintf('f12_phone_error_length = "%s";', $this->lengthError), View::POS_END); |
|
| 24 | 10 | parent::init(); |
|
| 25 | } |
||
| 26 | |||
| 27 | 10 | public static function registerTranslations() |
|
| 28 | { |
||
| 29 | 10 | $i18n = Yii::$app->i18n; |
|
| 30 | 10 | $i18n->translations['app.floor12.phone'] = [ |
|
| 31 | 10 | 'class' => 'yii\i18n\PhpMessageSource', |
|
| 32 | 10 | 'basePath' => '@vendor/floor12/yii2-phone/src/messages', |
|
| 33 | 10 | 'sourceLanguage' => 'en-US', |
|
| 34 | 10 | 'fileMap' => [ |
|
| 35 | 10 | 'app.floor12.phone' => 'phone.php', |
|
| 36 | 10 | ], |
|
| 37 | 10 | ]; |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param Model $model |
||
| 42 | * @param string $attribute |
||
| 43 | */ |
||
| 44 | 9 | public function validateAttribute($model, $attribute) |
|
| 45 | { |
||
| 46 | 9 | $model->$attribute = $this->clear($model->$attribute); |
|
| 47 | 9 | $result = $this->validateValue($model->$attribute); |
|
| 48 | 9 | if (!empty($result)) { |
|
| 49 | 3 | $this->addError($model, $attribute, $result[0], $result[1]); |
|
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $value |
||
| 55 | * @return mixed |
||
| 56 | */ |
||
| 57 | 9 | protected function clear($value) |
|
| 58 | { |
||
| 59 | 9 | return str_replace([' ', '-', '(', ')', '_', '+'], '', trim($value)); |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | 9 | protected function validateValue($value) |
|
| 66 | { |
||
| 67 | 9 | if (is_array($value) || is_object($value)) { |
|
| 68 | return [Yii::t('yii', '{attribute} is invalid.'), []]; |
||
| 69 | } |
||
| 70 | |||
| 71 | 9 | if (!is_numeric($value)) { |
|
| 72 | 1 | return [$this->formatError, []]; |
|
| 73 | } |
||
| 74 | |||
| 75 | 8 | if ((strlen(strval($value)) > 15) || (strlen(strval($value)) < 11)) { |
|
| 76 | 2 | return [$this->lengthError, []]; |
|
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param mixed $value |
||
| 82 | * @param null $error |
||
|
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | 10 | public function validate($value, &$error = null) |
|
| 86 | { |
||
| 87 | 10 | if (!is_null($value) && !is_string($value) && !is_numeric($value)) { |
|
| 88 | 1 | return false; |
|
| 89 | } |
||
| 90 | 9 | $value = $this->clear($value); |
|
| 91 | 9 | $result = $this->validateValue($value); |
|
| 92 | 9 | if (!empty($result)) { |
|
| 93 | 3 | $error = $result[0]; |
|
| 94 | 3 | return false; |
|
| 95 | } |
||
| 96 | 6 | return true; |
|
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param Model $model |
||
| 101 | * @param string $attribute |
||
| 102 | * @param View $view |
||
| 103 | * @return string|null |
||
| 104 | */ |
||
| 105 | public function clientValidateAttribute($model, $attribute, $view) |
||
| 106 | { |
||
| 107 | PhoneValidatorAsset::register($view); |
||
| 108 | $options = $this->getClientOptions($model, $attribute); |
||
| 109 | return 'f12phone.validatePhone(value, messages, ' . json_encode($options, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ');'; |
||
| 110 | } |
||
| 111 | |||
| 112 | } |
||
| 113 |