Example usage:
{@code $shortInfo = ShortNumberInfo::getInstance(); $shortNumber = "110"; $regionCode = "FR"; if ($shortInfo->isValidShortNumberForRegion($shortNumber, $regionCode)) { $cost = $shortInfo->getExpectedCostForRegion($shortNumber, $regionCode); Do something with the cost information here.}}
public getExpectedCostForRegion ( |
||
$number | the short number for which we want to know the expected cost category, as a string | |
$regionDialingFrom | string | the region from which the number is dialed |
Résultat | integer | the expected cost category for that region of the short number. Returns UNKNOWN_COST if the number does not match a cost category. Note that an invalid number may match any cost category. |
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)); }
public function testOverlappingNANPANumber() { // 211 is an emergency number in Barbados, while it is a toll-free information line in Canada // and the USA. $this->assertTrue($this->shortInfo->isEmergencyNumber("211", RegionCode::BB)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCostForRegion("211", RegionCode::BB)); $this->assertFalse($this->shortInfo->isEmergencyNumber("211", RegionCode::US)); $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCostForRegion("211", RegionCode::US)); $this->assertFalse($this->shortInfo->isEmergencyNumber("211", RegionCode::CA)); $this->assertEquals(ShortNumberCost::UNKNOWN_COST, $this->shortInfo->getExpectedCostForRegion("211", RegionCode::CA)); }
/** * @dataProvider shortNumberRegionList */ public function testEmergency($regionCode) { $desc = $this->shortNumberInfo->getMetadataForRegion($regionCode)->getEmergency(); if ($desc->hasExampleNumber()) { $exampleNumber = $desc->getExampleNumber(); $possibleNumberPattern = new Matcher($desc->getPossibleNumberPattern(), $exampleNumber); if (!$possibleNumberPattern->matches() || !$this->shortNumberInfo->isEmergencyNumber($exampleNumber, $regionCode)) { $this->fail("Emergency example number test failed for " . $regionCode); } elseif ($this->shortNumberInfo->getExpectedCostForRegion($exampleNumber, $regionCode) !== ShortNumberCost::TOLL_FREE) { $this->fail("Emergency example number not toll free for " . $regionCode); } } }
/** * @dataProvider shortNumberRegionList */ public function testEmergency($regionCode) { $desc = $this->shortNumberInfo->getMetadataForRegion($regionCode)->getEmergency(); if ($desc->hasExampleNumber()) { $exampleNumber = $desc->getExampleNumber(); $phoneNumber = $this->phoneNumberUtil->parse($exampleNumber, $regionCode); if (!$this->shortNumberInfo->isPossibleShortNumberForRegion($phoneNumber, $regionCode) || !$this->shortNumberInfo->isEmergencyNumber($exampleNumber, $regionCode)) { $this->fail("Emergency example number test failed for " . $regionCode); } elseif ($this->shortNumberInfo->getExpectedCostForRegion($phoneNumber, $regionCode) !== ShortNumberCost::TOLL_FREE) { $this->fail("Emergency example number not toll free for " . $regionCode); } } }