public function Validator::isValid in Telephone Validation 8.2
Check if number is valid for given settings.
Parameters
string $value: Phone number.
int $format: Supported input format.
array $country: (optional) List of supported countries. If empty all countries are valid.
Return value
bool Boolean representation of validation result.
File
- src/
Validator.php, line 50
Class
- Validator
- Performs telephone validation.
Namespace
Drupal\telephone_validationCode
public function isValid($value, $format, array $country = []) {
try {
// Get default country.
$default_region = $format == PhoneNumberFormat::NATIONAL ? reset($country) : NULL;
// Parse to object.
$number = $this->phoneUtils
->parse($value, $default_region);
} catch (\Exception $e) {
// If number could not be parsed by phone utils that's a one good reason
// to say it's not valid.
return FALSE;
}
// Perform basic telephone validation.
if (!$this->phoneUtils
->isValidNumber($number)) {
return FALSE;
}
// If country array is not empty and default region can be loaded
// do region matching validation.
// This condition is always TRUE for national phone number format.
if (!empty($country) && ($default_region = $this->phoneUtils
->getRegionCodeForNumber($number))) {
// Check if number's region matches list of supported countries.
if (array_search($default_region, $country) === FALSE) {
return FALSE;
}
}
return TRUE;
}