getRegionCodeForNumber() public method

Returns the region where a phone number is from. This could be used for geocoding at the region level.
public getRegionCodeForNumber ( PhoneNumber $number ) : null | string
$number PhoneNumber the phone number whose origin we want to know
return null | string the region where the phone number is from, or null if no region matches this calling code
 /**
  * @dataProvider localeList
  * @param string $regionCode
  * @param string $countryName
  */
 public function testLocales($regionCode, $countryName)
 {
     if (!in_array($regionCode, $this->phoneUtil->getSupportedRegions())) {
         $this->markTestSkipped("{$regionCode} is not supported");
     }
     $phoneNumber = $this->phoneUtil->getExampleNumberForType($regionCode, PhoneNumberType::FIXED_LINE_OR_MOBILE);
     $this->assertContains($regionCode, CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap[$phoneNumber->getCountryCode()]);
     $this->assertEquals($regionCode, $this->phoneUtil->getRegionCodeForNumber($phoneNumber));
     $this->assertEquals($countryName, $this->geocoder->getDescriptionForValidNumber($phoneNumber, 'en', 'ZZ'), "Checking {$phoneNumber} is part of {$countryName}");
 }
 /**
  * Returns a text description for the given phone number, in the language provided. The
  * description might consist of the name of the country where the phone number is from, or the
  * name of the geographical area the phone number is from if more detailed information is
  * available.
  *
  * <p>This method assumes the validity of the number passed in has already been checked, and that
  * the number is suitable for geocoding. We consider fixed-line and mobile numbers possible
  * candidates for geocoding.
  *
  * <p>If $userRegion is set, we also consider the region of the user. If the phone number is from
  * the same region as the user, only a lower-level description will be returned, if one exists.
  * Otherwise, the phone number's region will be returned, with optionally some more detailed
  * information.
  *
  * <p>For example, for a user from the region "US" (United States), we would show "Mountain View,
  * CA" for a particular number, omitting the United States from the description. For a user from
  * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United
  * States" or even just "United States".
  *
  * @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
  */
 public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null)
 {
     // If the user region matches the number's region, then we just show the lower-level
     // description, if one exists - if no description exists, we will show the region(country) name
     // for the number.
     $regionCode = $this->phoneUtil->getRegionCodeForNumber($number);
     if ($userRegion == null || $userRegion == $regionCode) {
         $languageStr = Locale::getPrimaryLanguage($locale);
         $scriptStr = "";
         $regionStr = Locale::getRegion($locale);
         $mobileToken = PhoneNumberUtil::getCountryMobileToken($number->getCountryCode());
         $nationalNumber = $this->phoneUtil->getNationalSignificantNumber($number);
         if ($mobileToken !== "" && !strncmp($nationalNumber, $mobileToken, strlen($mobileToken))) {
             // In some countries, eg. Argentina, mobile numbers have a mobile token before the national
             // destination code, this should be removed before geocoding.
             $nationalNumber = substr($nationalNumber, strlen($mobileToken));
             $region = $this->phoneUtil->getRegionCodeForCountryCode($number->getCountryCode());
             try {
                 $copiedNumber = $this->phoneUtil->parse($nationalNumber, $region);
             } catch (NumberParseException $e) {
                 // If this happens, just reuse what we had.
                 $copiedNumber = $number;
             }
             $areaDescription = $this->prefixFileReader->getDescriptionForNumber($copiedNumber, $languageStr, $scriptStr, $regionStr);
         } else {
             $areaDescription = $this->prefixFileReader->getDescriptionForNumber($number, $languageStr, $scriptStr, $regionStr);
         }
         return strlen($areaDescription) > 0 ? $areaDescription : $this->getCountryNameForNumber($number, $locale);
     }
     // Otherwise, we just show the region(country) name for now.
     return $this->getRegionDisplayName($regionCode, $locale);
     // TODO: Concatenate the lower-level and country-name information in an appropriate
     // way for each language.
 }
 /**
  * Gets the name of the carrier for the given phone number only when it is 'safe' to display to
  * users. A carrier name is considered safe if the number is valid and for a region that doesn't
  * support
  * {@linkplain http://en.wikipedia.org/wiki/Mobile_number_portability mobile number portability}.
  *
  * @param $number PhoneNumber the phone number for which we want to get a carrier name
  * @param $languageCode String the language code in which the name should be written
  * @return string a carrier name that is safe to display to users, or the empty string
  */
 public function getSafeDisplayName(PhoneNumber $number, $languageCode)
 {
     if ($this->phoneUtil->isMobileNumberPortableRegion($this->phoneUtil->getRegionCodeForNumber($number))) {
         return "";
     }
     return $this->getNameForNumber($number, $languageCode);
 }
 public function testGetRegionCodeForNumber()
 {
     $this->assertEquals(RegionCode::BS, $this->phoneUtil->getRegionCodeForNumber(self::$bsNumber));
     $this->assertEquals(RegionCode::US, $this->phoneUtil->getRegionCodeForNumber(self::$usNumber));
     $this->assertEquals(RegionCode::GB, $this->phoneUtil->getRegionCodeForNumber(self::$gbMobile));
     $this->assertEquals(RegionCode::UN001, $this->phoneUtil->getRegionCodeForNumber(self::$internationalTollFree));
 }
Esempio n. 5
0
 /**
  * Parse msisdn
  * @param  string $number
  * @return Instance
  * @author Andraz <*****@*****.**>
  */
 public function parse($number)
 {
     try {
         $phoneNumber = $this->numberUtil->parse($number, null);
     } catch (NumberParseException $e) {
         $this->valid = false;
         return $this;
     }
     if (!($this->valid = $this->numberUtil->isValidNumber($phoneNumber))) {
         return $this;
     }
     $this->countryDiallingCode = (int) $phoneNumber->getCountryCode();
     $this->countryIdentifier = $this->numberUtil->getRegionCodeForNumber($phoneNumber);
     $this->mnoIdentifier = $this->carrierMapper->getNameForNumber($phoneNumber, 'en_US');
     $this->subscriberNumber = $phoneNumber->getNationalNumber();
     return $this;
 }
 private function parse()
 {
     if ($this->phoneNumberProto) {
         // from phoneNumberProto
         $this->country_code = $this->phoneNumberProto->getCountryCode();
         $this->country_code_plus_sign = "+" . $this->country_code;
         $this->national_number = $this->phoneNumberProto->getNationalNumber();
         $this->extension = $this->phoneNumberProto->getExtension();
         $this->country_code_source = $this->phoneNumberProto->getCountryCodeSource();
         if (isset($this->countryCodeSourcesText[$this->country_code_source])) {
             $this->country_code_source_text = $this->countryCodeSourcesText[$this->country_code_source];
         }
         $this->raw_input = $this->phoneNumberProto->getRawInput();
         $this->preferred_domestic_carrier_code = $this->phoneNumberProto->getPreferredDomesticCarrierCode();
         // from validation
         $this->is_possible_number = $this->phoneUtil->isPossibleNumber($this->phoneNumberProto);
         $this->is_valid_number = $this->phoneUtil->isValidNumber($this->phoneNumberProto);
         $this->region_code_for_number = $this->phoneUtil->getRegionCodeForNumber($this->phoneNumberProto);
         $this->number_type = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         $this->number_type_text = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         if (isset($this->phoneNumberTypesText[$this->number_type])) {
             $this->number_type_text = $this->phoneNumberTypesText[$this->number_type];
         } else {
             $this->number_type_text = "OTHER";
         }
         $this->is_mobile_number = in_array($this->number_type, [PhoneNumberType::FIXED_LINE_OR_MOBILE, PhoneNumberType::MOBILE]);
         // from formatting
         $this->format_e164 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::E164);
         $this->format_international = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::INTERNATIONAL);
         $this->format_national = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::NATIONAL);
         $this->format_rfc3966 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::RFC3966);
         // from additional
         $phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder::getInstance();
         $this->description = $phoneNumberOfflineGeocoder->getDescriptionForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToCarrierMapper = PhoneNumberToCarrierMapper::getInstance();
         $this->carrier_name = $phoneNumberToCarrierMapper->getNameForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToTimeZonesMapper = PhoneNumberToTimeZonesMapper::getInstance();
         $this->timezones = $phoneNumberToTimeZonesMapper->getTimeZonesForNumber($this->phoneNumberProto);
     }
 }
Esempio n. 7
0
 /**
  * @return string
  */
 public function getCountry() : string
 {
     return $this->util->getRegionCodeForNumber($this->number);
 }
 public function testTKGeoLocation()
 {
     $number = '+6903010';
     $phoneNumber = $this->phoneUtil->parse($number, RegionCode::ZZ);
     $this->assertEquals('TK', $this->phoneUtil->getRegionCodeForNumber($phoneNumber));
     $this->assertEquals('Tokelau', $this->geocoder->getDescriptionForNumber($phoneNumber, 'en'));
 }
Esempio n. 9
-1
 public function isValid($value)
 {
     if (!is_scalar($value)) {
         $this->error(self::INVALID);
         return false;
     }
     $country = $this->getCountry();
     $supportedCountries = $this->libPhoneNumber->getSupportedRegions();
     if (!in_array($country, $supportedCountries)) {
         $this->error(self::UNSUPPORTED);
         return false;
     }
     try {
         $numberProto = $this->libPhoneNumber->parse($value, $country);
     } catch (NumberParseException $e) {
         $this->error(self::INVALID_NUMBER);
         return false;
     }
     if (!$this->libPhoneNumber->isValidNumber($numberProto)) {
         $this->error(self::INVALID_NUMBER);
         return false;
     }
     $region = $this->libPhoneNumber->getRegionCodeForNumber($numberProto);
     if ($this->libPhoneNumber->isValidNumberForRegion($numberProto, $region)) {
         return true;
     }
     $this->error(self::NO_MATCH);
     return false;
 }