Overview
ThePhoneNumberToCarrierMapper class identifies the mobile carrier (network operator) for a given phone number. This works best for mobile numbers and returns the carrier name in the specified language.
Important: The carrier returned is the original carrier the number was allocated to. Due to mobile number portability in many countries, the number may have been moved to a different carrier. See
getSafeDisplayName() for a method that accounts for this.Getting an Instance
The carrier mapper uses a singleton pattern. Get an instance using the staticgetInstance() method:
Optional namespace for custom carrier mapping data. Only needed for advanced use cases.
Methods
getNameForNumber()
Returns the carrier name for a phone number in the specified language. Validates that the number is suitable for carrier lookup (mobile, pager, or fixed-line-or-mobile types).The phone number to identify the carrier for.
The language code for the carrier name (e.g., “en”, “de”, “fr”). The method will attempt to return the carrier name in this language if available.
The carrier name in the requested language, or an empty string if:
- The number is not a mobile/pager type
- No carrier mapping data is available for this number
- The number is invalid
Example
getNameForValidNumber()
Returns the carrier name without checking if the number type is suitable for carrier lookup. Assumes you’ve already validated the number.A valid phone number assumed to be suitable for carrier mapping.
The language code for the carrier name.
The carrier name, or an empty string if no mapping is found.
Use this method only if you’ve already verified the number is a mobile/pager type. For most cases, prefer
getNameForNumber() which includes validation.getSafeDisplayName()
Returns the carrier name only if it’s safe to display to users. A carrier name is considered “safe” when:- The number is valid
- The number is a mobile/pager type
- The country does not support mobile number portability
The phone number to get the carrier for.
The language code for the carrier name.
The carrier name if safe to display, or an empty string if the region supports number portability or no mapping is available.
Example
Complete Example
Supported Number Types
Carrier mapping works for these phone number types:PhoneNumberType::MOBILE- Mobile numbersPhoneNumberType::FIXED_LINE_OR_MOBILE- Numbers that could be either fixed-line or mobilePhoneNumberType::PAGER- Pager numbers
Data Availability
Carrier data limitations:
- Carrier information is not available for all countries
- Data coverage is best for mobile numbers in major markets
- The carrier returned is the original allocation and may not reflect the current carrier due to number portability
- In regions with mobile number portability, use
getSafeDisplayName()to avoid showing potentially incorrect information
Mobile Number Portability
Many countries allow users to keep their phone number when switching carriers. This means:- The returned carrier name might be outdated
getSafeDisplayName()returns empty for regions with portability- The data shows the original carrier allocation, not current ownership
- Historical data analysis
- Initial carrier identification
- Regions without number portability
- Critical business logic dependent on current carrier
- Real-time carrier verification
- Regions with active mobile number portability