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 == static::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);
}
} elseif ($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(static::UNIQUE_INTERNATIONAL_PREFIX, $internationalPrefix);
if ($uniqueInternationalPrefixMatcher->matches()) {
$internationalPrefixForFormatting = $internationalPrefix;
} elseif ($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;
}