libphonenumber\ShortNumberInfo::isValidShortNumber PHP Method

isValidShortNumber() public method

Tests whether a short number matches a valid pattern. If a country calling code is shared by multiple regions, this returns true if it's valid in any of them. Note that this doesn't verify the number is actually in use, which is impossible to tell by just looking at the number itself. See {@link #isValidShortNumberForRegion(PhoneNumber, String)} for details.
public isValidShortNumber ( PhoneNumber $number ) : boolean
$number PhoneNumber PhoneNumber the short number for which we want to test the validity
return boolean whether the short number matches a valid pattern
    public function isValidShortNumber(PhoneNumber $number)
    {
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
        $regionCode = $this->getRegionCodeForShortNumberFromRegionList($number, $regionCodes);
        if (count($regionCodes) > 1 && $regionCode !== null) {
            // If a matching region had been found for the phone number from among two or more regions,
            // then we have already implicitly verified its validity for that region.
            return true;
        }
        return $this->isValidShortNumberForRegion($number, $regionCode);
    }

Usage Example

Exemplo n.º 1
0
    /**
     * @dataProvider shortNumberRegionList
     */
    public function testShortNumbersValidAndCorrectCost($regionCode)
    {
        $exampleShortNumber = $this->shortNumberInfo->getExampleShortNumber($regionCode);
        if (!$this->shortNumberInfo->isValidShortNumberForRegion(
            $this->phoneNumberUtil->parse($exampleShortNumber, $regionCode),
            $regionCode
        )
        ) {
            $this->fail(
                "Failed validation for string region_code: {$regionCode}, national_number: {$exampleShortNumber}"
            );
        }
        $phoneNumber = $this->phoneNumberUtil->parse($exampleShortNumber, $regionCode);
        if (!$this->shortNumberInfo->isValidShortNumber($phoneNumber)) {
            $this->fail("Failed validation for " . (string)$phoneNumber);
        }

        $costArray = array(
            ShortNumberCost::PREMIUM_RATE,
            ShortNumberCost::STANDARD_RATE,
            ShortNumberCost::TOLL_FREE,
            ShortNumberCost::UNKNOWN_COST
        );

        foreach ($costArray as $cost) {
            $exampleShortNumber = $this->shortNumberInfo->getExampleShortNumberForCost($regionCode, $cost);
            if ($exampleShortNumber != '') {
                $this->assertEquals(
                    $cost,
                    $this->shortNumberInfo->getExpectedCostForRegion($this->phoneNumberUtil->parse($exampleShortNumber, $regionCode), $regionCode),
                    "Wrong cost for " . (string)$phoneNumber
                );
            }
        }
    }
All Usage Examples Of libphonenumber\ShortNumberInfo::isValidShortNumber