getDescriptionForNumber() public method

As per getDescriptionForValidNumber, but explicitly checks the validity of the number passed in.
See also: getDescriptionForValidNumber
public getDescriptionForNumber ( PhoneNumber $number, string $locale, string $userRegion = null ) : string
$number libphonenumber\PhoneNumber a valid phone number for which we want to get a text description
$locale string the language code for which the description should be written
$userRegion string 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
Beispiel #1
0
 /**
  * @param string $number
  * @param string $country
  * @param string|NULL $locale
  * @param string|NULL $userCountry
  *
  * @return string
  *
  * @throws Exceptions\NoValidCountryException
  * @throws Exceptions\NoValidPhoneException
  */
 public function getLocation($number, $country = 'AUTO', $locale = NULL, $userCountry = NULL)
 {
     if ($this->isValid((string) $number, $country)) {
         $country = strtoupper($country);
         if ($userCountry !== NULL) {
             // Check for valid user country
             $userCountry = $this->validateCountry($userCountry);
         }
         // Parse phone number
         $parsed = $this->phoneNumberUtil->parse((string) $number, $country);
         // Determine locale
         $locale = $locale === NULL && $this->translator && method_exists($this->translator, 'getLocale') ? $this->translator->getLocale() : 'en_US';
         // Get phone number location
         return $this->phoneNumberGeocoder->getDescriptionForNumber($parsed, $locale, $userCountry);
     } else {
         throw new Exceptions\NoValidPhoneException('Provided phone number "' . $number . '" is not valid phone number. Provide valid phone number.');
     }
 }
 public function testIsleOfManLocale()
 {
     $number = "447624806000";
     $phoneNumber = $this->phoneUtil->parse($number, 'GB');
     $this->assertEquals("Isle of Man", $this->geocoder->getDescriptionForNumber($phoneNumber, 'en'));
 }
 public function testGetDescriptionForInvalidNumber()
 {
     $this->assertEquals("", $this->geocoder->getDescriptionForNumber(self::$KO_InvalidNumber, "en"));
     $this->assertEquals("", $this->geocoder->getDescriptionForNumber(self::$US_InvalidNumber, "en"));
 }
 public function testChineseGeolocation()
 {
     $number = $this->phoneUtil->parse("+86 150 3657 7264", "CN");
     $location = $this->geocoder->getDescriptionForNumber($number, "en");
     $this->assertEquals("Luoyang, Henan", $location);
 }
 public function testIsleOfManLocale()
 {
     $number = "447797752305";
     $phoneNumber = $this->phoneUtil->parse($number, 'GB');
     $this->assertEquals("Jersey", $this->geocoder->getDescriptionForNumber($phoneNumber, 'en'));
 }
 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'));
 }