/** * @dataProvider shortNumberRegionList */ public function testCarrierSpecificShortNumbers($regionCode) { // Test the carrier-specific tag. $desc = $this->shortNumberInfo->getMetadataForRegion($regionCode)->getCarrierSpecific(); if ($desc->hasExampleNumber()) { $exampleNumber = $desc->getExampleNumber(); $carrierSpecificNumber = $this->phoneNumberUtil->parse($exampleNumber, $regionCode); $exampleNumberMatcher = new Matcher($desc->getPossibleNumberPattern(), $exampleNumber); if (!$exampleNumberMatcher->matches() || !$this->shortNumberInfo->isCarrierSpecific($carrierSpecificNumber)) { $this->fail("Carrier-specific test failed for " . $regionCode); } } }
/** * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the * same as that of the region where the number is from, then NATIONAL formatting will be applied. * * <p>If the number itself has a country calling code of zero or an otherwise invalid country * calling code, then we return the number with no formatting applied. * * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and * Kazakhstan (who share the same country calling code). In those cases, no international prefix * is used. For regions which have multiple international prefixes, the number in its * INTERNATIONAL format will be returned instead. * * @param PhoneNumber $number the phone number to be formatted * @param string $regionCallingFrom the region where the call is being placed * @return string the formatted phone number */ public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCallingFrom) { if (!$this->isValidRegionCode($regionCallingFrom)) { return $this->format($number, PhoneNumberFormat::INTERNATIONAL); } $countryCallingCode = $number->getCountryCode(); $nationalSignificantNumber = $this->getNationalSignificantNumber($number); if (!$this->hasValidCountryCallingCode($countryCallingCode)) { return $nationalSignificantNumber; } if ($countryCallingCode == self::NANPA_COUNTRY_CODE) { if ($this->isNANPACountry($regionCallingFrom)) { // For NANPA regions, return the national format for these regions but prefix it with the // country calling code. return $countryCallingCode . " " . $this->format($number, PhoneNumberFormat::NATIONAL); } } else { if ($countryCallingCode == $this->getCountryCodeForValidRegion($regionCallingFrom)) { // If regions share a country calling code, the country calling code need not be dialled. // This also applies when dialling within a region, so this if clause covers both these cases. // Technically this is the case for dialling from La Reunion to other overseas departments of // France (French Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover this // edge case for now and for those cases return the version including country calling code. // Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion return $this->format($number, PhoneNumberFormat::NATIONAL); } } // Metadata cannot be null because we checked 'isValidRegionCode()' above. $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. $internationalPrefixForFormatting = ""; $uniqueInternationalPrefixMatcher = new Matcher(self::UNIQUE_INTERNATIONAL_PREFIX, $internationalPrefix); if ($uniqueInternationalPrefixMatcher->matches()) { $internationalPrefixForFormatting = $internationalPrefix; } else { if ($metadataForRegionCallingFrom->hasPreferredInternationalPrefix()) { $internationalPrefixForFormatting = $metadataForRegionCallingFrom->getPreferredInternationalPrefix(); } } $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode); // Metadata cannot be null because the country calling code is valid. $metadataForRegion = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode); $formattedNationalNumber = $this->formatNsn($nationalSignificantNumber, $metadataForRegion, PhoneNumberFormat::INTERNATIONAL); $formattedNumber = $formattedNationalNumber; $this->maybeAppendFormattedExtension($number, $metadataForRegion, PhoneNumberFormat::INTERNATIONAL, $formattedNumber); if (mb_strlen($internationalPrefixForFormatting) > 0) { $formattedNumber = $internationalPrefixForFormatting . " " . $countryCallingCode . " " . $formattedNumber; } else { $this->prefixNumberWithCountryCallingCode($countryCallingCode, PhoneNumberFormat::INTERNATIONAL, $formattedNumber); } return $formattedNumber; }