You are here

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'
telephone_validation.services.yml in ./telephone_validation.services.yml
telephone_validation.services.yml
1 service uses Validator
telephone_validation.validator in ./telephone_validation.services.yml
Drupal\telephone_validation\Validator

File

src/Validator.php, line 13

Namespace

Drupal\telephone_validation
View 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

Namesort descending Modifiers Type Description Overrides
Validator::$countryManager public property Country Manager service.
Validator::$phoneUtils public property Phone Number util.
Validator::getCountryList public function Get list of countries with country code and leading digits.
Validator::isValid public function Check if number is valid for given settings.
Validator::__construct public function Validator constructor.