class Validator in Telephone Validation 8.2
Performs telephone validation.
Hierarchy
- class \Drupal\telephone_validation\Validator
Expanded class hierarchy of Validator
2 files declare their use of Validator
- TelephoneConstraintValidator.php in src/
Plugin/ Validation/ Constraint/ TelephoneConstraintValidator.php - ValidatorTest.php in tests/
src/ Unit/ ValidatorTest.php
1 string reference to 'Validator'
1 service uses Validator
File
- src/
Validator.php, line 13
Namespace
Drupal\telephone_validationView source
class Validator {
/**
* Phone Number util.
*
* @var \libphonenumber\PhoneNumberUtil
*/
public $phoneUtils;
/**
* Country Manager service.
*
* @var \Drupal\Core\Locale\CountryManagerInterface
*/
public $countryManager;
/**
* Validator constructor.
*/
public function __construct(CountryManagerInterface $country_manager) {
$this->phoneUtils = PhoneNumberUtil::getInstance();
$this->countryManager = $country_manager;
}
/**
* Check if number is valid for given settings.
*
* @param string $value
* Phone number.
* @param int $format
* Supported input format.
* @param array $country
* (optional) List of supported countries. If empty all countries are valid.
*
* @return bool
* Boolean representation of validation result.
*/
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;
}
/**
* Get list of countries with country code and leading digits.
*
* @return array
* Flatten array you can use it directly in select lists.
*/
public function getCountryList() {
$regions = [];
foreach ($this->countryManager
->getList() as $region => $name) {
$region_meta = $this->phoneUtils
->getMetadataForRegion($region);
if (is_object($region_meta)) {
$regions[$region] = (string) new FormattableMarkup('@country - +@country_code', [
'@country' => $name,
'@country_code' => $region_meta
->getCountryCode(),
]);
}
}
return $regions;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Validator:: |
public | property | Country Manager service. | |
Validator:: |
public | property | Phone Number util. | |
Validator:: |
public | function | Get list of countries with country code and leading digits. | |
Validator:: |
public | function | Check if number is valid for given settings. | |
Validator:: |
public | function | Validator constructor. |