Skip to content

Commit

Permalink
Area code information lost for all MX numbers in last metadata update
Browse files Browse the repository at this point in the history
  • Loading branch information
giggsey committed Jul 1, 2024
1 parent 5915119 commit 19c0665
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/PhoneNumberUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ class PhoneNumberUtil
86, // China
];

/**
* Set of country codes that doesn't have national prefix, but it has area codes.
*/
protected const COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES = [
52, // Mexico
];

/**
* Set of country calling codes that have geographically assigned mobile numbers. This may not be
* complete; we add calling codes case by case, as we find geographical mobile numbers or hear
Expand Down Expand Up @@ -787,20 +794,25 @@ public function getLengthOfGeographicalAreaCode(PhoneNumber $number): int
if ($metadata === null) {
return 0;
}

$countryCallingCode = $number->getCountryCode();


// If a country doesn't use a national prefix, and this number doesn't have an Italian leading
// zero, we assume it is a closed dialling plan with no area codes.
if (!$metadata->hasNationalPrefix() && !$number->isItalianLeadingZero()) {
// Note:this is our general assumption, but there are exceptions which are tracked in
// COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES.
if (!$metadata->hasNationalPrefix() && !$number->isItalianLeadingZero() && !in_array($countryCallingCode, static::COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES)) {
return 0;
}

$type = $this->getNumberType($number);
$countryCallingCode = $number->getCountryCode();

if ($type === PhoneNumberType::MOBILE
// Note this is a rough heuristic; it doesn't cover Indonesia well, for example, where area
// codes are present for some mobile phones but not for others. We have no better way of
// representing this in the metadata at this point.
&& in_array($countryCallingCode, self::GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES)
&& in_array($countryCallingCode, static::GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES)
) {
return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions tests/core/PhoneNumberUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ public function testGetLengthOfGeographicalAreaCode(): void
// Italian numbers - there is no national prefix, but it still has an area code.
$this->assertEquals(2, $this->phoneUtil->getLengthOfGeographicalAreaCode(self::$itNumber));

// Mexico numbers - there is no national prefix, but it still has an area code.
$this->assertEquals(2, $this->phoneUtil->getLengthOfGeographicalAreaCode(self::$mxNumber1));

// Google Singapore. Singapore has no area code and no national prefix.
$this->assertEquals(0, $this->phoneUtil->getLengthOfGeographicalAreaCode(self::$sgNumber));

Expand Down

0 comments on commit 19c0665

Please sign in to comment.