You are here

function phone_libphonenumber_valid in Phone 7.2

Validates a phone number with libphonenumber.

Parameters

string $number: The raw phone number we are working with.

string $countrycode: The selected countrycode for this number.

string $extension: An extension number.

bool $all_countries: When TRUE, all countries are allowed, when FALSE, supply $allowed_countries to perform extra validation.

array $allowed_countries: An array of allowed countries. When empty, this check will be ignored.

Return value

string Returns boolean TRUE when the number is valid, a formatted error message otherwise.

1 call to phone_libphonenumber_valid()
phone_element_validate in includes/phone.element.inc
An #element_validate callback for the phone element.

File

includes/phone.libphonenumber.inc, line 69
Provides integration functions with the libraries API and libphonenumber.

Code

function phone_libphonenumber_valid($number, $countrycode, $extension, $all_countries = TRUE, $allowed_countries = array()) {
  try {

    // Get the parsed phone objects.
    // Allow alpha characters, in case $number contains an extension that we need to parse.
    list($phoneutil, $phonenumber) = _phone_libphonenumber($number, $countrycode, $extension, TRUE);

    // If this is an alpha number and it has an extension, inform the user that this is not supported.
    if ($phonenumber
      ->hasExtension() && $phoneutil
      ->isAlphaNumber($number)) {
      return t('Vanity numbers do not support extensions. Input the real number if you wish to use an extension.');
    }

    // Get the actual countrycode used, in case we didn't have one before,
    // and libphonenumber was able to detect one from a country code.
    if ($phonenumber
      ->getCountryCodeSource() != libphonenumber\CountryCodeSource::FROM_DEFAULT_COUNTRY) {
      $new_countrycode = $phoneutil
        ->getRegionCodeForCountryCode($phonenumber
        ->getCountryCode());

      // If the country code is empty, then set the new one,
      // if the country code is different, lets see if they have the same
      // calling code, if they do, use our original country code.
      // i.e. Calling Code of +1 is always the US, but the user might have
      // input Barbados for example.
      if (empty($countrycode) || $countrycode != $new_countrycode && $phonenumber
        ->getCountryCode() != $phoneutil
        ->getMetadataForRegion($countrycode)
        ->getCountryCode()) {
        $countrycode = $new_countrycode;
      }
    }

    // Check if this number is actually valid.
    if (!$phoneutil
      ->isValidNumber($phonenumber)) {
      $error = t('The number "%number" is not a valid phone number for @country.', array(
        '%number' => $number,
        '@country' => phone_countries($countrycode, 'country'),
      ));
      $example = $phoneutil
        ->getExampleNumber($countrycode);
      if (!empty($example)) {
        $error .= ' ' . t('The expected format is %example.', array(
          '%example' => $phoneutil
            ->format($example, libphonenumber\PhoneNumberFormat::NATIONAL),
        ));
      }
      return $error;
    }
    if (!$all_countries && !empty($allowed_countries) && !isset($allowed_countries[$countrycode])) {
      return t('Numbers from %country are not allowed.', array(
        '%country' => phone_countries($countrycode, 'country'),
      ));
    }
    if (!empty($extension) && preg_match('/[^0-9]/', $extension)) {
      return t('Phone extensions can only contain digits.');
    }
  } catch (libphonenumber\NumberParseException $e) {

    // Various error reasons why the number parsing might have failed.
    // Return a meaningful message.
    switch ($e
      ->getErrorType()) {
      case libphonenumber\NumberParseException::TOO_SHORT_AFTER_IDD:
        return t('The number "%number" appears to include an International Direct Dialing Code, but is not long enough after this code to be a viable phone number.', array(
          '%number' => $number,
        ));
      case libphonenumber\NumberParseException::NOT_A_NUMBER:
        return t('The supplied number "%number" does not seem to be a phone number.', array(
          '%number' => $number,
        ));
      case libphonenumber\NumberParseException::TOO_SHORT_NSN:
        return t('The number "%number" is too short to be a phone number.', array(
          '%number' => $number,
        ));
      case libphonenumber\NumberParseException::TOO_LONG:
        return t('The number "%number" is too long to be a phone number.', array(
          '%number' => $number,
        ));
      case libphonenumber\NumberParseException::INVALID_COUNTRY_CODE:
        return t('Invalid country code. Be sure to prefix the number with the plus sign and the direct dial country code you wish to use.');
    }
  }

  // All went well. Hooray.
  return TRUE;
}