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 ( |
||
$number | PhoneNumber the short number for which we want to test the validity | |
return | boolean | whether the short number matches a valid pattern |
/** * @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 ); } } }
public function testEmergencyNumberForSharedCountryCallingCode() { // Test the emergency number 112, which is valid in both Australia and the Christmas Islands. $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::AU)); $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse("112", RegionCode::AU), RegionCode::AU)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCostForRegion($this->parse("112", RegionCode::AU), RegionCode::AU)); $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::CX)); $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse("112", RegionCode::CX), RegionCode::CX)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCostForRegion($this->parse("112", RegionCode::CX), RegionCode::CX)); $sharedEmergencyNumber = new PhoneNumber(); $sharedEmergencyNumber->setCountryCode(61)->setNationalNumber(112); $this->assertTrue($this->shortInfo->isValidShortNumber($sharedEmergencyNumber)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($sharedEmergencyNumber)); }