libphonenumber\ShortNumberInfo::getExpectedCost PHP Méthode

getExpectedCost() public méthode

For example, if a number is STANDARD_RATE in the US, but TOLL_FREE in Canada, the expected cost returned by this method will be STANDARD_RATE, since the NANPA countries share the same country calling code.

Note: If the region from which the number is dialed is known, it is highly preferable to call {@link #getExpectedCostForRegion(PhoneNumber, String)} instead.
public getExpectedCost ( PhoneNumber $number ) : integer
$number PhoneNumber the short number for which we want to know the expected cost category
Résultat integer the highest expected cost category of the short number in the region(s) with the given country calling code
    public function getExpectedCost(PhoneNumber $number)
    {
        $regionCodes = $this->getRegionCodesForCountryCode($number->getCountryCode());
        if (count($regionCodes) == 0) {
            return ShortNumberCost::UNKNOWN_COST;
        }
        if (count($regionCodes) == 1) {
            return $this->getExpectedCostForRegion($number, $regionCodes[0]);
        }
        $cost = ShortNumberCost::TOLL_FREE;
        foreach ($regionCodes as $regionCode) {
            $costForRegion = $this->getExpectedCostForRegion($number, $regionCode);
            switch ($costForRegion) {
                case ShortNumberCost::PREMIUM_RATE:
                    return ShortNumberCost::PREMIUM_RATE;
                case ShortNumberCost::UNKNOWN_COST:
                    $cost = ShortNumberCost::UNKNOWN_COST;
                    break;
                case ShortNumberCost::STANDARD_RATE:
                    if ($cost != ShortNumberCost::UNKNOWN_COST) {
                        $cost = ShortNumberCost::STANDARD_RATE;
                    }
                    break;
                case ShortNumberCost::TOLL_FREE:
                    // Do nothing
                    break;
            }
        }
        return $cost;
    }

Usage Example

 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));
 }