libphonenumber\PhoneNumberUtil::isPossibleNumberWithReason PHP Method

isPossibleNumberWithReason() public method

  • It doesn't attempt to figure out the type of the number, but uses general rules which applies to all types of phone numbers in a region. Therefore, it is much faster than isValidNumber.
  • For fixed line numbers, many regions have the concept of area code, which together with subscriber number constitute the national significant number. It is sometimes okay to dial the subscriber number only when dialing in the same area. This function will return true if the subscriber-number-only version is passed in. On the other hand, because isValidNumber validates using information on both starting digits (for fixed line numbers, that would most likely be area codes) and length (obviously includes the length of area codes for fixed line numbers), it will return false for the subscriber-number-only version.
  • public isPossibleNumberWithReason ( PhoneNumber $number ) : integer
    $number PhoneNumber the number that needs to be checked
    return integer a ValidationResult object which indicates whether the number is possible
        public function isPossibleNumberWithReason(PhoneNumber $number)
        {
            $nationalNumber = $this->getNationalSignificantNumber($number);
            $countryCode = $number->getCountryCode();
            // Note: For Russian Fed and NANPA numbers, we just use the rules from the default region (US or
            // Russia) since the getRegionCodeForNumber will not work if the number is possible but not
            // valid. This would need to be revisited if the possible number pattern ever differed between
            // various regions within those plans.
            if (!$this->hasValidCountryCallingCode($countryCode)) {
                return ValidationResult::INVALID_COUNTRY_CODE;
            }
            $regionCode = $this->getRegionCodeForCountryCode($countryCode);
            // Metadata cannot be null because the country calling code is valid.
            $metadata = $this->getMetadataForRegionOrCallingCode($countryCode, $regionCode);
            return $this->testNumberLength($nationalNumber, $metadata->getGeneralDesc());
        }

    Usage Example

    コード例 #1
    0
     public function testIsPossibleNumberWithReason()
     {
         // National numbers for country calling code +1 that are within 7 to 10 digits are possible.
         $this->assertEquals(ValidationResult::IS_POSSIBLE, $this->phoneUtil->isPossibleNumberWithReason(self::$usNumber));
         $this->assertEquals(ValidationResult::IS_POSSIBLE, $this->phoneUtil->isPossibleNumberWithReason(self::$usLocalNumber));
         $this->assertEquals(ValidationResult::TOO_LONG, $this->phoneUtil->isPossibleNumberWithReason(self::$usLongNumber));
         $number = new PhoneNumber();
         $number->setCountryCode(0)->setNationalNumber(2530000);
         $this->assertEquals(ValidationResult::INVALID_COUNTRY_CODE, $this->phoneUtil->isPossibleNumberWithReason($number));
         $number->clear();
         $number->setCountryCode(1)->setNationalNumber(253000);
         $this->assertEquals(ValidationResult::TOO_SHORT, $this->phoneUtil->isPossibleNumberWithReason($number));
         $number->clear();
         $number->setCountryCode(65)->setNationalNumber(1234567890);
         $this->assertEquals(ValidationResult::IS_POSSIBLE, $this->phoneUtil->isPossibleNumberWithReason($number));
         $this->assertEquals(ValidationResult::TOO_LONG, $this->phoneUtil->isPossibleNumberWithReason(self::$internationalTollFreeTooLong));
         // Try with number that we don't have metadata for.
         $adNumber = new PhoneNumber();
         $adNumber->setCountryCode(376)->setNationalNumber(12345);
         $this->assertEquals(ValidationResult::IS_POSSIBLE, $this->phoneUtil->isPossibleNumberWithReason($adNumber));
         $adNumber->setCountryCode(376)->setNationalNumber(1);
         $this->assertEquals(ValidationResult::TOO_SHORT, $this->phoneUtil->isPossibleNumberWithReason($adNumber));
         $adNumber->setCountryCode(376)->setNationalNumber(12345678901234567);
         $this->assertEquals(ValidationResult::TOO_LONG, $this->phoneUtil->isPossibleNumberWithReason($adNumber));
     }
    PhoneNumberUtil