isPossibleNumber() public method

This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)} with the resultant PhoneNumber object.

public isPossibleNumber ( PhoneNumber | string $number, string $regionDialingFrom = null ) : boolean
$number PhoneNumber | string the number that needs to be checked, in the form of a string
$regionDialingFrom string the region that we are expecting the number to be dialed from. Note this is different from the region where the number belongs. For example, the number +1 650 253 0000 is a number that belongs to US. When written in this form, it can be dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any region which uses an international dialling prefix of 00. When it is written as 650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it can only be dialed from within a smaller area in the US (Mountain View, CA, to be more specific).
return boolean true if the number is possible
Beispiel #1
0
 /**
  * Performs the actual validation of the phone number.
  *
  * @param mixed $number
  * @param null  $country
  * @return bool
  */
 protected function isValidNumber($number, $country = null)
 {
     try {
         // Throws NumberParseException if not parsed correctly against the supplied country.
         // If no country was given, tries to discover the country code from the number itself.
         $phoneNumber = $this->lib->parse($number, $country);
         // Lenient validation; doesn't need a country code.
         if ($this->lenient) {
             return $this->lib->isPossibleNumber($phoneNumber);
         }
         // For automatic detection, the number should have a country code.
         // Check if type is allowed.
         if ($phoneNumber->hasCountryCode() && (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types))) {
             // Automatic detection:
             if ($this->autodetect) {
                 // Validate if the international phone number is valid for its contained country.
                 return $this->lib->isValidNumber($phoneNumber);
             }
             // Validate number against the specified country.
             return $this->lib->isValidNumberForRegion($phoneNumber, $country);
         }
     } catch (NumberParseException $e) {
         // Proceed to default validation error.
     }
     return false;
 }
 public function testIsNotPossibleNumber()
 {
     $this->assertFalse($this->phoneUtil->isPossibleNumber(self::$usLongNumber));
     $this->assertFalse($this->phoneUtil->isPossibleNumber(self::$internationalTollFreeTooLong));
     $number = new PhoneNumber();
     $number->setCountryCode(1)->setNationalNumber(253000);
     $this->assertFalse($this->phoneUtil->isPossibleNumber($number));
     $number->clear();
     $number->setCountryCode(44)->setNationalNumber(300);
     $this->assertFalse($this->phoneUtil->isPossibleNumber($number));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("+1 650 253 00000", RegionCode::US));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("(650) 253-00000", RegionCode::US));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("I want a Pizza", RegionCode::US));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("253-000", RegionCode::US));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("1 3000", RegionCode::GB));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("+44 300", RegionCode::GB));
     $this->assertFalse($this->phoneUtil->isPossibleNumber("+800 1234 5678 9", RegionCode::UN001));
 }
 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);
     }
 }