libphonenumber\ShortNumberInfo::getExpectedCostForRegion PHP Méthode

getExpectedCostForRegion() public méthode

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 ( PhoneNumber | string $number, string $regionDialingFrom ) : integer
$number PhoneNumber | string 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 getExpectedCostForRegion($number, $regionDialingFrom)
    {
        if ($number instanceof PhoneNumber) {
            if (!$this->regionDialingFromMatchesNumber($number, $regionDialingFrom)) {
                return ShortNumberCost::UNKNOWN_COST;
            }
        }
        // Note that regionDialingFrom may be null, in which case phoneMetadata will also be null.
        $phoneMetadata = $this->getMetadataForRegion($regionDialingFrom);
        if ($phoneMetadata === null) {
            return ShortNumberCost::UNKNOWN_COST;
        }
        if ($number instanceof PhoneNumber) {
            $shortNumber = $this->getNationalSignificantNumber($number);
        } else {
            /**
             * @deprecated Anyone who was using it and passing in a string with whitespace (or other
             *             formatting characters) would have been getting the wrong result. You should parse
             *             the string to PhoneNumber and use the method
             *             {@code #getExpectedCostForRegion(PhoneNumber, String)}. This method will be
             *             removed in the next release.
             */
            $shortNumber = $number;
        }
        // The possible lengths are not present for a particular sub-type if they match the general
        // description; for this reason, we check the possible lengths against the general description
        // first to allow an early exit if possible.
        if (!in_array(strlen($shortNumber), $phoneMetadata->getGeneralDesc()->getPossibleLength())) {
            return ShortNumberCost::UNKNOWN_COST;
        }
        // The cost categories are tested in order of decreasing expense, since if for some reason the
        // patterns overlap the most expensive matching cost category should be returned.
        if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getPremiumRate())) {
            return ShortNumberCost::PREMIUM_RATE;
        }
        if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getStandardRate())) {
            return ShortNumberCost::STANDARD_RATE;
        }
        if ($this->matchesPossibleNumberAndNationalNumber($shortNumber, $phoneMetadata->getTollFree())) {
            return ShortNumberCost::TOLL_FREE;
        }
        if ($this->isEmergencyNumber($shortNumber, $regionDialingFrom)) {
            // Emergency numbers are implicitly toll-free.
            return ShortNumberCost::TOLL_FREE;
        }
        return ShortNumberCost::UNKNOWN_COST;
    }

Usage Example

 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));
 }
All Usage Examples Of libphonenumber\ShortNumberInfo::getExpectedCostForRegion