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
giggsey committed Jul 1, 2024
1 parent f662a7e commit 339e0e8
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/PhoneNumberUtil.php
Original file line number Diff line number Diff line change
@@ -164,6 +164,13 @@ class PhoneNumberUtil
*/
protected static $GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES;

/**
* Set of country codes that doesn't have national prefix, but it has area codes.
*
* @var array
*/
protected static $COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES;

/**
* 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
@@ -400,6 +407,9 @@ protected function __construct(MetadataSourceInterface $metadataSource, $country
static::$GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES = array();
static::$GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES[] = 86; // China

static::$COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES = array();
static::$COUNTRIES_WITHOUT_NATIONAL_PREFIX_WITH_AREA_CODES[] = 52; // Mexico

static::$GEO_MOBILE_COUNTRIES = array();
static::$GEO_MOBILE_COUNTRIES[] = 52; // Mexico
static::$GEO_MOBILE_COUNTRIES[] = 54; // Argentina
@@ -873,14 +883,18 @@ public function getLengthOfGeographicalAreaCode(PhoneNumber $number)
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, self::$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
3 changes: 3 additions & 0 deletions tests/core/PhoneNumberUtilTest.php
Original file line number Diff line number Diff line change
@@ -333,6 +333,9 @@ public function testGetLengthOfGeographicalAreaCode()
$this->assertEquals(1, $this->phoneUtil->getLengthOfGeographicalAreaCode(self::$auNumber));

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

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

// Google Singapore. Singapore has no area code and no national prefix.

0 comments on commit 339e0e8

Please sign in to comment.