diff --git a/src/PhoneNumberUtil.php b/src/PhoneNumberUtil.php index b900ebb4..e12915ef 100644 --- a/src/PhoneNumberUtil.php +++ b/src/PhoneNumberUtil.php @@ -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 diff --git a/tests/core/PhoneNumberUtilTest.php b/tests/core/PhoneNumberUtilTest.php index e66c7fe7..7f459c12 100644 --- a/tests/core/PhoneNumberUtilTest.php +++ b/tests/core/PhoneNumberUtilTest.php @@ -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.