public function validateAttribute($model, $attribute)
{
// if countryAttribute is set
if (!isset($country) && isset($this->countryAttribute)) {
$countryAttribute = $this->countryAttribute;
$country = $model->{$countryAttribute};
}
// if country is fixed
if (!isset($country) && isset($this->country)) {
$country = $this->country;
}
// if none select from our models with best effort
if (!isset($country) && isset($model->country_code)) {
$country = $model->country_code;
}
if (!isset($country) && isset($model->country)) {
$country = $model->country;
}
// if none and strict
if (!isset($country) && $this->strict) {
$this->addError($model, $attribute, \Yii::t('skeeks/cms', 'For phone validation country required'));
return false;
}
if (!isset($country)) {
return true;
}
$phoneUtil = PhoneNumberUtil::getInstance();
try {
$numberProto = $phoneUtil->parse($model->{$attribute}, $country);
if ($phoneUtil->isValidNumber($numberProto)) {
if ($this->format == true) {
$model->{$attribute} = $phoneUtil->format($numberProto, PhoneNumberFormat::INTERNATIONAL);
}
return true;
} else {
$this->addError($model, $attribute, \Yii::t('skeeks/cms', 'Phone number does not seem to be a valid phone number'));
return false;
}
} catch (NumberParseException $e) {
$this->addError($model, $attribute, \Yii::t('skeeks/cms', 'Unexpected Phone Number Format'));
} catch (Exception $e) {
$this->addError($model, $attribute, \Yii::t('skeeks/cms', 'Unexpected Phone Number Format or Country Code'));
}
}