Moodle PHP Documentation 5.0
Moodle 5.0.1+ (Build: 20250711) (61af040cd5f)
libphonenumber\ShortNumberInfo Class Reference

Public Member Functions

 connectsToEmergencyNumber (string $number, string $regionCode)
 Returns true if the given number, exactly as dialed, might be used to connect to an emergency service in the given region.
 
 getExampleShortNumber (string $regionCode)
 Gets a valid short number for the specified region.
 
 getExampleShortNumberForCost (string $regionCode, int $cost)
 Gets a valid short number for the specified cost category.
 
 getExpectedCost (PhoneNumber $number)
 Gets the expected cost category of a short number (however, nothing is implied about its validity).
 
 getExpectedCostForRegion (PhoneNumber $number, string $regionDialingFrom)
 Gets the expected cost category of a short number when dialled from a region (however, nothing is implied about its validity).
 
 getMetadataForRegion (string $regionCode)
 
 getSupportedRegions ()
 
 isCarrierSpecific (PhoneNumber $number)
 Given a valid short number, determines whether it is carrier-specific (however, nothing is implied about its validity).
 
 isCarrierSpecificForRegion (PhoneNumber $number, string $regionDialingFrom)
 Given a valid short number, determines whether it is carrier-specific when dialed from the given region (however, nothing is implied about its validity).
 
 isEmergencyNumber (string $number, string $regionCode)
 Returns true if the given number exactly matches an emergency service number in the given region.
 
 isPossibleShortNumber (PhoneNumber $number)
 Check whether a short number is a possible number.
 
 isPossibleShortNumberForRegion (PhoneNumber $shortNumber, string $regionDialingFrom)
 Check whether a short number is a possible number when dialled from a region, given the number in the form of a string, and the region where the number is dialled from.
 
 isSmsServiceForRegion (PhoneNumber $number, string $regionDialingFrom)
 Given a valid short number, determines whether it is an SMS service (however, nothing is implied about its validity).
 
 isValidShortNumber (PhoneNumber $number)
 Tests whether a short number matches a valid pattern.
 
 isValidShortNumberForRegion (PhoneNumber $number, string $regionDialingFrom)
 Tests whether a short number matches a valid pattern in a region.
 

Static Public Member Functions

static getInstance ()
 
static resetInstance ()
 

Protected Member Functions

 __construct (MatcherAPIInterface $matcherAPI)
 
 getNationalSignificantNumber (PhoneNumber $number)
 Gets the national significant number of the a phone number.
 
 getRegionCodeForShortNumberFromRegionList (PhoneNumber $number, array $regionCodes)
 Helper method to get the region code for a given phone number, from a list of possible region codes.
 
 getRegionCodesForCountryCode (int $countryCallingCode)
 Returns a list with the region codes that match the specific country calling code.
 
 loadMetadataFromFile (string $filePrefix, string $regionCode, int $countryCallingCode)
 
 matchesEmergencyNumberHelper (string $number, string $regionCode, bool $allowPrefixMatch)
 
 matchesPossibleNumberAndNationalNumber (string $number, PhoneNumberDesc $numberDesc)
 TODO: Once we have benchmarked ShortnumberInfo, consider if it is worth keeping this performance optimization.
 
 regionDialingFromMatchesNumber (PhoneNumber $number, string $regionDialingFrom)
 Helper method to check that the country calling code of the number matches the region it's being dialed from.
 

Protected Attributes

array< int, string[]> array $countryCallingCodeToRegionCodeMap = []
 
array< int, PhoneMetadata > array $countryCodeToNonGeographicalMetadataMap = []
 
string $currentFilePrefix
 
MatcherAPIInterface $matcherAPI
 
array< string, PhoneMetadata > array $regionToMetadataMap = []
 
const REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT
 

Static Protected Attributes

static ShortNumberInfo $instance
 

Member Function Documentation

◆ connectsToEmergencyNumber()

libphonenumber\ShortNumberInfo::connectsToEmergencyNumber ( string $number,
string $regionCode )

Returns true if the given number, exactly as dialed, might be used to connect to an emergency service in the given region.

This method accepts a string, rather than a PhoneNumber, because it needs to distinguish cases such as "+1 911" and "911", where the former may not connect to an emergency service in all cases but the latter would. This method takes into account cases where the number might contain formatting, or might have additional digits appended (when it is okay to do that in the specified region).

Parameters
string$numberthe phone number to test
string$regionCodethe region where the phone number if being dialled
Return values
booleanwhether the number might be used to connect to an emergency service in the given region

◆ getExampleShortNumber()

libphonenumber\ShortNumberInfo::getExampleShortNumber ( string $regionCode)

Gets a valid short number for the specified region.

Parameters
$regionCodestring the region for which an example short number is needed
Return values
stringa valid short number for the specified region. Returns an empty string when the metadata does not contain such information.

◆ getExampleShortNumberForCost()

libphonenumber\ShortNumberInfo::getExampleShortNumberForCost ( string $regionCode,
int $cost )

Gets a valid short number for the specified cost category.

Parameters
string$regionCodethe region for which an example short number is needed
int$costthe ShortNumberCost category of number that is needed
Return values
stringa valid short number for the specified region and cost category. Returns an empty string when the metadata does not contain such information, or the cost is UNKNOWN_COST.

◆ getExpectedCost()

libphonenumber\ShortNumberInfo::getExpectedCost ( PhoneNumber $number)

Gets the expected cost category of a short number (however, nothing is implied about its validity).

If the country calling code is unique to a region, this method behaves exactly the same as getExpectedCostForRegion(PhoneNumber,String). However, if the country calling code is shared by multiple regions, then it returns the highest cost in the sequence PREMIUM_RATE, UNKNOWN_COST, STANDARD_RATE, TOLL_FREE. The reason for the position of UNKNOWN_COST in this order is that if a number is UNKNOWN_COST in one region but STANDARD_RATE or TOLL_FREE in another, its expected cost cannot be estimated as one of the latter since it might be a PREMIUM_RATE number.

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 getExpectedCostForRegion(PhoneNumber,String) instead.

Parameters
PhoneNumber$numberthe short number for which we want to know the expected cost category
Return values
intthe highest expected cost category of the short number in the region(s) with the given country calling code
See also
ShortNumberCost

◆ getExpectedCostForRegion()

libphonenumber\ShortNumberInfo::getExpectedCostForRegion ( PhoneNumber $number,
string $regionDialingFrom )

Gets the expected cost category of a short number when dialled from a region (however, nothing is implied about its validity).

If it is important that the number is valid, then its validity must first be checked using isValidShortNumberForRegion. Note that emergency numbers are always considered toll-free. Example usage:


$shortInfo = ShortNumberInfo::getInstance();
$shortNumber = PhoneNumberUtil::parse("110", "US);
$regionCode = "FR";
if ($shortInfo->isValidShortNumberForRegion($shortNumber, $regionCode)) {
$cost = $shortInfo->getExpectedCostForRegion($shortNumber, $regionCode);
// Do something with the cost information here.
}
Parameters
PhoneNumber$numberthe short number for which we want to know the expected cost category, as a string
string$regionDialingFromthe region from which the number is dialed
Return values
intthe 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.
See also
ShortNumberCost

◆ getNationalSignificantNumber()

libphonenumber\ShortNumberInfo::getNationalSignificantNumber ( PhoneNumber $number)
protected

Gets the national significant number of the a phone number.

Note a national significant number doesn't contain a national prefix or any formatting.

This is a temporary duplicate of the getNationalSignificantNumber method from PhoneNumberUtil. Ultimately a canonical static version should exist in a separate utility class (to prevent ShortNumberInfo needing to depend on PhoneNumberUtil).

Parameters
PhoneNumber$numberthe phone number for which the national significant number is needed
Return values
stringthe national significant number of the PhoneNumber object passed in

◆ getRegionCodeForShortNumberFromRegionList()

libphonenumber\ShortNumberInfo::getRegionCodeForShortNumberFromRegionList ( PhoneNumber $number,
array $regionCodes )
protected

Helper method to get the region code for a given phone number, from a list of possible region codes.

If the list contains more than one region, the first region for which the number is valid is returned.

Parameters
string[]$regionCodes
Return values
string|nullRegion Code (or null if none are found)

◆ getRegionCodesForCountryCode()

libphonenumber\ShortNumberInfo::getRegionCodesForCountryCode ( int $countryCallingCode)
protected

Returns a list with the region codes that match the specific country calling code.

For non-geographical country calling codes, the region code 001 is returned. Also, in the case of no region code being found, an empty list is returned.

Return values
string[]

◆ getSupportedRegions()

libphonenumber\ShortNumberInfo::getSupportedRegions ( )
Return values
string[]

◆ isCarrierSpecific()

libphonenumber\ShortNumberInfo::isCarrierSpecific ( PhoneNumber $number)

Given a valid short number, determines whether it is carrier-specific (however, nothing is implied about its validity).

Carrier-specific numbers may connect to a different end-point, or not connect at all, depending on the user's carrier. If it is important that the number is valid, then its validity must first be checked using isValidShortNumber or isValidShortNumberForRegion.

Parameters
PhoneNumber$numberthe valid short number to check
Return values
booleanwhether the short number is carrier-specific, assuming the input was a valid short number

◆ isCarrierSpecificForRegion()

libphonenumber\ShortNumberInfo::isCarrierSpecificForRegion ( PhoneNumber $number,
string $regionDialingFrom )

Given a valid short number, determines whether it is carrier-specific when dialed from the given region (however, nothing is implied about its validity).

Carrier-specific numbers may connect to a different end-point, or not connect at all, depending on the user's carrier. If it is important that the number is valid, then its validity must first be checked using isValidShortNumber or isValidShortNumberForRegion. Returns false if the number doesn't match the region provided.

Parameters
PhoneNumber$numberThe valid short number to check
string$regionDialingFromThe region from which the number is dialed
Return values
boolWhether the short number is carrier-specific in the provided region, assuming the input was a valid short number

◆ isEmergencyNumber()

libphonenumber\ShortNumberInfo::isEmergencyNumber ( string $number,
string $regionCode )

Returns true if the given number exactly matches an emergency service number in the given region.

This method takes into account cases where the number might contain formatting, but doesn't allow additional digits to be appended. Note that isEmergencyNumber(number, region) implies connectsToEmergencyNumber(number, region).

Parameters
string$numberthe phone number to test
string$regionCodethe region where the phone number is being dialled
Return values
booleanwhether the number exactly matches an emergency services number in the given region

◆ isPossibleShortNumber()

libphonenumber\ShortNumberInfo::isPossibleShortNumber ( PhoneNumber $number)

Check whether a short number is a possible number.

If a country calling code is shared by multiple regions, this returns true if it's possible in any of them. This provides a more lenient check than isValidShortNumber. See IsPossibleShortNumberForRegion(PhoneNumber, String) for details.

Parameters
$numberPhoneNumber the short number to check
Return values
booleanwhether the number is a possible short number

◆ isPossibleShortNumberForRegion()

libphonenumber\ShortNumberInfo::isPossibleShortNumberForRegion ( PhoneNumber $shortNumber,
string $regionDialingFrom )

Check whether a short number is a possible number when dialled from a region, given the number in the form of a string, and the region where the number is dialled from.

This provides a more lenient check than isValidShortNumber.

Parameters
PhoneNumber$shortNumberThe short number to check
string$regionDialingFromRegion dialing From
Return values
booleanwhether the number is a possible short number

◆ isSmsServiceForRegion()

libphonenumber\ShortNumberInfo::isSmsServiceForRegion ( PhoneNumber $number,
string $regionDialingFrom )

Given a valid short number, determines whether it is an SMS service (however, nothing is implied about its validity).

An SMS service is where the primary or only intended usage is to receive and/or send text messages (SMSs). This includes MMS as MMS numbers downgrade to SMS if the other party isn't MMS-capable. If it is important that the number is valid, then its validity must first be checked using isValidShortNumber or *#isValidShortNumberForRegion. Returns false if the number doesn't match the region provided.

Parameters
PhoneNumber$numberThe valid short number to check
string$regionDialingFromThe region from which the number is dialed
Return values
boolWhether the short number is an SMS service in the provided region, assuming the input was a valid short number.

◆ isValidShortNumber()

libphonenumber\ShortNumberInfo::isValidShortNumber ( PhoneNumber $number)

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 isValidShortNumberForRegion(PhoneNumber,String) for details.

Parameters
$numberPhoneNumber the short number for which we want to test the validity
Return values
booleanwhether the short number matches a valid pattern

◆ isValidShortNumberForRegion()

libphonenumber\ShortNumberInfo::isValidShortNumberForRegion ( PhoneNumber $number,
string $regionDialingFrom )

Tests whether a short number matches a valid pattern in a region.

Note that this doesn't verify the number is actually in use, which is impossible to tell by just looking at the number itself.

Parameters
PhoneNumber$numberThe Short number for which we want to test the validity
string$regionDialingFromthe region from which the number is dialed
Return values
booleanwhether the short number matches a valid pattern

Member Data Documentation

◆ REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT

const libphonenumber\ShortNumberInfo::REGIONS_WHERE_EMERGENCY_NUMBERS_MUST_BE_EXACT
protected
Initial value:
= [
'BR',
'CL',
'NI',
]

The documentation for this class was generated from the following file: