libphonenumber\ShortNumberInfo::isPossibleShortNumber PHP Méthode

isPossibleShortNumber() public méthode

Check whether a short number is a possible number. If a country calling code is shared by multiple regions, this returns true if it's possible in any of them. This provides a more lenient check than {@link #isValidShortNumber}. See {@link #IsPossibleShortNumberForRegion(PhoneNumber, String)} for details.
public isPossibleShortNumber ( PhoneNumber $number ) : boolean
$number PhoneNumber PhoneNumber the short number to check
Résultat boolean whether the number is a possible short number
    public function isPossibleShortNumber(PhoneNumber $number)
    {
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
        $shortNumberLength = strlen($this->getNationalSignificantNumber($number));
        foreach ($regionCodes as $region) {
            $phoneMetadata = $this->getMetadataForRegion($region);
            if ($phoneMetadata === null) {
                continue;
            }
            if (in_array($shortNumberLength, $phoneMetadata->getGeneralDesc()->getPossibleLength())) {
                return true;
            }
        }
        return false;
    }

Usage Example

 public function testIsPossibleShortNumber()
 {
     $possibleNumber = new PhoneNumber();
     $possibleNumber->setCountryCode(33)->setNationalNumber(123456);
     $this->assertTrue($this->shortInfo->isPossibleShortNumber($possibleNumber));
     $this->assertTrue($this->shortInfo->isPossibleShortNumberForRegion($this->parse(123456, RegionCode::FR), RegionCode::FR));
     $impossibleNumber = new PhoneNumber();
     $impossibleNumber->setCountryCode(33)->setNationalNumber(9);
     $this->assertFalse($this->shortInfo->isPossibleShortNumber($impossibleNumber));
     // Note that GB and GG share the country calling code 44, and that this number is possible but
     // not valid.
     $gbNumber = new PhoneNumber();
     $gbNumber->setCountryCode(44)->setNationalNumber(11001);
     $this->assertTrue($this->shortInfo->isPossibleShortNumber($gbNumber));
 }