libphonenumber\ShortNumberInfo::isValidShortNumberForRegion PHP Méthode

isValidShortNumberForRegion() public méthode

Tests whether a short number matches a valid pattern in a region. Note that this doesn't verify the number is actually in use, which is impossible to tell by just looking at the number itself.
public isValidShortNumberForRegion ( PhoneNumber | string $number, string $regionDialingFrom ) : boolean
$number PhoneNumber | string The Short number for which we want to test the validity
$regionDialingFrom string the region from which the number is dialed
Résultat boolean whether the short number matches a valid pattern
    public function isValidShortNumberForRegion($number, $regionDialingFrom)
    {
        if ($number instanceof PhoneNumber) {
            if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) {
                return false;
            }
        }
        $phoneMetadata = $this->getMetadataForRegion($regionDialingFrom);
        if ($phoneMetadata === null) {
            return false;
        }
        if ($number instanceof PhoneNumber) {
            $shortNumber = $this->getNationalSignificantNumber($number);
        } else {
            /**
             * @deprecated Anyone who was using it and passing in a string with whitespace (or other
             *             formatting characters) would have been getting the wrong result. You should parse
             *             the string to PhoneNumber and use the method
             *             {@code #isValidShortNumberForRegion(PhoneNumber, String)}. This method will be
             *             removed in the next release.
             */
            $shortNumber = $number;
        }
        $generalDesc = $phoneMetadata->getGeneralDesc();
        if (!$this->matchesPossibleNumberAndNationalNumber($shortNumber, $generalDesc)) {
            return false;
        }
        $shortNumberDesc = $phoneMetadata->getShortCode();
        return $this->matchesPossibleNumberAndNationalNumber($shortNumber, $shortNumberDesc);
    }

Usage Example

 public function testCountryCallingCodeIsNotIgnored()
 {
     // +46 is the country calling code for Sweden (SE), and 40404 is a valid short number in the US.
     $this->assertFalse($this->shortInfo->isPossibleShortNumberForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US));
     $this->assertFalse($this->shortInfo->isValidShortNumberForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US));
     $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCostForRegion($this->parse('+4640404', RegionCode::SE), RegionCode::US));
 }
All Usage Examples Of libphonenumber\ShortNumberInfo::isValidShortNumberForRegion