libphonenumber\PhoneNumberUtil::isNumberGeographical PHP Method

isNumberGeographical() public method

Tests whether a phone number has a geographical association. It checks if the number is associated to a certain region in the country where it belongs to. Note that this doesn't verify if the number is actually in use. isNumberGeographical(PhoneNumberType, $countryCallingCode) Tests whether a phone number has a geographical association, as represented by its type and the country it belongs to. This version exists since calculating the phone number type is expensive; if we have already done this, we don't want to do it again.
public isNumberGeographical ( PhoneNumber | integer $phoneNumberObjOrType, integer | null $countryCallingCode = null ) : boolean
$phoneNumberObjOrType PhoneNumber | integer A PhoneNumber object, or a PhoneNumberType integer
$countryCallingCode integer | null Used when passing a PhoneNumberType
return boolean
    public function isNumberGeographical($phoneNumberObjOrType, $countryCallingCode = null)
    {
        if ($phoneNumberObjOrType instanceof PhoneNumber) {
            return $this->isNumberGeographical($this->getNumberType($phoneNumberObjOrType), $phoneNumberObjOrType->getCountryCode());
        }
        return $phoneNumberObjOrType == PhoneNumberType::FIXED_LINE || $phoneNumberObjOrType == PhoneNumberType::FIXED_LINE_OR_MOBILE || in_array($countryCallingCode, static::$GEO_MOBILE_COUNTRIES) && $phoneNumberObjOrType == PhoneNumberType::MOBILE;
    }

Usage Example

 /**
  * As per getDescriptionForValidNumber, but explicitly checks the validity of the number
  * passed in.
  *
  *
  * @see getDescriptionForValidNumber
  * @param PhoneNumber $number a valid phone number for which we want to get a text description
  * @param string $locale the language code for which the description should be written
  * @param string $userRegion the region code for a given user. This region will be omitted from the
  *     description if the phone number comes from this region. It is a two-letter uppercase ISO
  *     country code as defined by ISO 3166-1.
  * @return string a text description for the given language code for the given phone number, or empty
  *     string if the number passed in is invalid
  */
 public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null)
 {
     $numberType = $this->phoneUtil->getNumberType($number);
     if ($numberType === PhoneNumberType::UNKNOWN) {
         return "";
     } elseif (!$this->phoneUtil->isNumberGeographical($numberType, $number->getCountryCode())) {
         return $this->getCountryNameForNumber($number, $locale);
     }
     return $this->getDescriptionForValidNumber($number, $locale, $userRegion);
 }
All Usage Examples Of libphonenumber\PhoneNumberUtil::isNumberGeographical
PhoneNumberUtil