libphonenumber\PhoneNumberUtil::isPossibleNumber PHP Méthode

isPossibleNumber() public méthode

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).
Résultat boolean true if the number is possible
    public function isPossibleNumber($number, $regionDialingFrom = null)
    {
        if ($regionDialingFrom !== null && is_string($number)) {
            try {
                return $this->isPossibleNumberWithReason($this->parse($number, $regionDialingFrom)) === ValidationResult::IS_POSSIBLE;
            } catch (NumberParseException $e) {
                return false;
            }
        } else {
            return $this->isPossibleNumberWithReason($number) === ValidationResult::IS_POSSIBLE;
        }
    }

Usage Example

 /**
  * 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;
 }
All Usage Examples Of libphonenumber\PhoneNumberUtil::isPossibleNumber
PhoneNumberUtil