From b18f2d003039dd19d6dde4b2108d7619c0495380 Mon Sep 17 00:00:00 2001 From: Joshua Gigg Date: Wed, 26 May 2021 10:52:12 +0100 Subject: [PATCH] Always use preferred intl prefix if present After this change, the API always use preferred intl prefix if present, not just for numbers with a non-unique IDD. This means we will output "8~10" as the prefix if calling formatOutOfCountryCallingNumber instead of "810" for some regions that have this tilde in their prefix [designates that the user should wait before continuing to dial]. Mirrors https://github.com/google/libphonenumber/pull/2621 --- src/PhoneNumberUtil.php | 23 +++++++++++-------- tests/buildtools/BuildMetadataFromXmlTest.php | 4 ++-- tests/core/PhoneNumberUtilTest.php | 4 ++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/PhoneNumberUtil.php b/src/PhoneNumberUtil.php index c4111da28..dd0052340 100644 --- a/src/PhoneNumberUtil.php +++ b/src/PhoneNumberUtil.php @@ -502,8 +502,8 @@ protected static function createExtnPattern($forParsing) $extLimitAfterLikelyLabel = 15; $extLimitAfterAmbiguousChar = 9; $extLimitWhenNotSure = 6; - - + + $possibleSeparatorsBetweenNumberAndExtLabel = "[ \xC2\xA0\\t,]*"; // Optional full stop (.) or colon, followed by zero or more spaces/tabs/commas. @@ -2769,23 +2769,28 @@ public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCall return $this->format($number, PhoneNumberFormat::NATIONAL); } // Metadata cannot be null because we checked 'isValidRegionCode()' above. + /** @var PhoneMetadata $metadataForRegionCallingFrom */ $metadataForRegionCallingFrom = $this->getMetadataForRegion($regionCallingFrom); $internationalPrefix = $metadataForRegionCallingFrom->getInternationalPrefix(); - // For regions that have multiple international prefixes, the international format of the - // number is returned, unless there is a preferred international prefix. + // In general, if there is a preferred international prefix, use that. Otherwise, for regions + // that have multiple international prefixes, the international format of the number is + // returned since we would not know which one to use. $internationalPrefixForFormatting = ''; - $uniqueInternationalPrefixMatcher = new Matcher(static::SINGLE_INTERNATIONAL_PREFIX, $internationalPrefix); - - if ($uniqueInternationalPrefixMatcher->matches()) { - $internationalPrefixForFormatting = $internationalPrefix; - } elseif ($metadataForRegionCallingFrom->hasPreferredInternationalPrefix()) { + if ($metadataForRegionCallingFrom->hasPreferredInternationalPrefix()) { $internationalPrefixForFormatting = $metadataForRegionCallingFrom->getPreferredInternationalPrefix(); + } else { + $uniqueInternationalPrefixMatcher = new Matcher(static::SINGLE_INTERNATIONAL_PREFIX, $internationalPrefix); + + if ($uniqueInternationalPrefixMatcher->matches()) { + $internationalPrefixForFormatting = $internationalPrefix; + } } $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode); // Metadata cannot be null because the country calling code is valid. + /** @var PhoneMetadata $metadataForRegion */ $metadataForRegion = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode); $formattedNationalNumber = $this->formatNsn( $nationalSignificantNumber, diff --git a/tests/buildtools/BuildMetadataFromXmlTest.php b/tests/buildtools/BuildMetadataFromXmlTest.php index 3d6fe729b..10277cf98 100644 --- a/tests/buildtools/BuildMetadataFromXmlTest.php +++ b/tests/buildtools/BuildMetadataFromXmlTest.php @@ -97,7 +97,7 @@ public function testLoadTerritoryTagMetadata() { $xmlInput = '" @@ -107,7 +107,7 @@ public function testLoadTerritoryTagMetadata() $this->assertEquals(33, $phoneMetadata->getCountryCode()); $this->assertEquals('2', $phoneMetadata->getLeadingDigits()); $this->assertEquals('00', $phoneMetadata->getInternationalPrefix()); - $this->assertEquals('0011', $phoneMetadata->getPreferredInternationalPrefix()); + $this->assertEquals('00~11', $phoneMetadata->getPreferredInternationalPrefix()); $this->assertEquals('0', $phoneMetadata->getNationalPrefixForParsing()); $this->assertEquals('9$1', $phoneMetadata->getNationalPrefixTransformRule()); $this->assertEquals('0', $phoneMetadata->getNationalPrefix()); diff --git a/tests/core/PhoneNumberUtilTest.php b/tests/core/PhoneNumberUtilTest.php index a75c1bc54..5b965bde3 100644 --- a/tests/core/PhoneNumberUtilTest.php +++ b/tests/core/PhoneNumberUtilTest.php @@ -829,6 +829,10 @@ public function testFormatOutOfCountryWithPreferredIntlPrefix() '0011 39 02 3661 8300', $this->phoneUtil->formatOutOfCountryCallingNumber(self::$itNumber, RegionCode::AU) ); + + // Testing preferred international prefixes with ~ are supported (designates waiting). + $this->assertEquals('8~10 39 02 3661 8300', + $this->phoneUtil->formatOutOfCountryCallingNumber(self::$itNumber, RegionCode::UZ)); } public function testFormatOutOfCountryKeepingAlphaChars()