Caveats:
public formatOutOfCountryKeepingAlphaChars ( |
||
$number | the phone number that needs to be formatted | |
$regionCallingFrom | String | the region where the call is being placed |
return | String | the formatted phone number |
public function formatOutOfCountryKeepingAlphaChars(PhoneNumber $number, $regionCallingFrom)
{
$rawInput = $number->getRawInput();
// If there is no raw input, then we can't keep alpha characters because there aren't any.
// In this case, we return formatOutOfCountryCallingNumber.
if (mb_strlen($rawInput) == 0) {
return $this->formatOutOfCountryCallingNumber($number, $regionCallingFrom);
}
$countryCode = $number->getCountryCode();
if (!$this->hasValidCountryCallingCode($countryCode)) {
return $rawInput;
}
// Strip any prefix such as country calling code, IDD, that was present. We do this by comparing
// the number in raw_input with the parsed number.
// To do this, first we normalize punctuation. We retain number grouping symbols such as " "
// only.
$rawInput = $this->normalizeHelper($rawInput, static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS, true);
// Now we trim everything before the first three digits in the parsed number. We choose three
// because all valid alpha numbers have 3 digits at the start - if it does not, then we don't
// trim anything at all. Similarly, if the national number was less than three digits, we don't
// trim anything at all.
$nationalNumber = $this->getNationalSignificantNumber($number);
if (mb_strlen($nationalNumber) > 3) {
$firstNationalNumberDigit = strpos($rawInput, substr($nationalNumber, 0, 3));
if ($firstNationalNumberDigit !== false) {
$rawInput = substr($rawInput, $firstNationalNumberDigit);
}
}
$metadataForRegionCallingFrom = $this->getMetadataForRegion($regionCallingFrom);
if ($countryCode == static::NANPA_COUNTRY_CODE) {
if ($this->isNANPACountry($regionCallingFrom)) {
return $countryCode . " " . $rawInput;
}
} elseif ($metadataForRegionCallingFrom !== null && $countryCode == $this->getCountryCodeForValidRegion($regionCallingFrom)) {
$formattingPattern = $this->chooseFormattingPatternForNumber($metadataForRegionCallingFrom->numberFormats(), $nationalNumber);
if ($formattingPattern === null) {
// If no pattern above is matched, we format the original input.
return $rawInput;
}
$newFormat = new NumberFormat();
$newFormat->mergeFrom($formattingPattern);
// The first group is the first group of digits that the user wrote together.
$newFormat->setPattern("(\\d+)(.*)");
// Here we just concatenate them back together after the national prefix has been fixed.
$newFormat->setFormat("\$1\$2");
// Now we format using this pattern instead of the default pattern, but with the national
// prefix prefixed if necessary.
// This will not work in the cases where the pattern (and not the leading digits) decide
// whether a national prefix needs to be used, since we have overridden the pattern to match
// anything, but that is not the case in the metadata to date.
return $this->formatNsnUsingPattern($rawInput, $newFormat, PhoneNumberFormat::NATIONAL);
}
$internationalPrefixForFormatting = "";
// If an unsupported region-calling-from is entered, or a country with multiple international
// prefixes, the international format of the number is returned, unless there is a preferred
// international prefix.
if ($metadataForRegionCallingFrom !== null) {
$internationalPrefix = $metadataForRegionCallingFrom->getInternationalPrefix();
$uniqueInternationalPrefixMatcher = new Matcher(static::UNIQUE_INTERNATIONAL_PREFIX, $internationalPrefix);
$internationalPrefixForFormatting = $uniqueInternationalPrefixMatcher->matches() ? $internationalPrefix : $metadataForRegionCallingFrom->getPreferredInternationalPrefix();
}
$formattedNumber = $rawInput;
$regionCode = $this->getRegionCodeForCountryCode($countryCode);
// Metadata cannot be null because the country calling code is valid.
$metadataForRegion = $this->getMetadataForRegionOrCallingCode($countryCode, $regionCode);
$this->maybeAppendFormattedExtension($number, $metadataForRegion, PhoneNumberFormat::INTERNATIONAL, $formattedNumber);
if (mb_strlen($internationalPrefixForFormatting) > 0) {
$formattedNumber = $internationalPrefixForFormatting . " " . $countryCode . " " . $formattedNumber;
} else {
// Invalid region entered as country-calling-from (so no metadata was found for it) or the
// region chosen has multiple international dialling prefixes.
$this->prefixNumberWithCountryCallingCode($countryCode, PhoneNumberFormat::INTERNATIONAL, $formattedNumber);
}
return $formattedNumber;
}
public function testFormatOutOfCountryKeepingAlphaChars() { $alphaNumericNumber = new PhoneNumber(); $alphaNumericNumber->setCountryCode(1)->setNationalNumber(8007493524)->setRawInput("1800 six-flag"); $this->assertEquals("0011 1 800 SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); $alphaNumericNumber->setRawInput("1-800-SIX-flag"); $this->assertEquals("0011 1 800-SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); $alphaNumericNumber->setRawInput("Call us from UK: 00 1 800 SIX-flag"); $this->assertEquals("0011 1 800 SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); $alphaNumericNumber->setRawInput("800 SIX-flag"); $this->assertEquals("0011 1 800 SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); // Formatting from within the NANPA region. $this->assertEquals("1 800 SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::US)); $this->assertEquals("1 800 SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::BS)); // Testing that if the raw input doesn't exist, it is formatted using // formatOutOfCountryCallingNumber. $alphaNumericNumber->clearRawInput(); $this->assertEquals("00 1 800 749 3524", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::DE)); // Testing AU alpha number formatted from Australia. $alphaNumericNumber->setCountryCode(61)->setNationalNumber(827493524)->setRawInput("+61 82749-FLAG"); // This number should have the national prefix fixed. $this->assertEquals("082749-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); $alphaNumericNumber->setRawInput("082749-FLAG"); $this->assertEquals("082749-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); $alphaNumericNumber->setNationalNumber(18007493524)->setRawInput("1-800-SIX-flag"); // This number should not have the national prefix prefixed, in accordance with the override for // this specific formatting rule. $this->assertEquals("1-800-SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AU)); // The metadata should not be permanently changed, since we copied it before modifying patterns. // Here we check this. $alphaNumericNumber->setNationalNumber(1800749352); $this->assertEquals("1800 749 352", $this->phoneUtil->formatOutOfCountryCallingNumber($alphaNumericNumber, RegionCode::AU)); // Testing a region with multiple international prefixes. $this->assertEquals("+61 1-800-SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::SG)); // Testing the case of calling from a non-supported region. $this->assertEquals("+61 1-800-SIX-FLAG", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AQ)); // Testing the case with an invalid country calling code. $alphaNumericNumber->setCountryCode(0)->setNationalNumber(18007493524)->setRawInput("1-800-SIX-flag"); // Uses the raw input only. $this->assertEquals("1-800-SIX-flag", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::DE)); // Testing the case of an invalid alpha number. $alphaNumericNumber->setCountryCode(1)->setNationalNumber(80749)->setRawInput("180-SIX"); // No country-code stripping can be done. $this->assertEquals("00 1 180-SIX", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::DE)); // Testing the case of calling from a non-supported region. $alphaNumericNumber->setCountryCode(1)->setNationalNumber(80749)->setRawInput("180-SIX"); // No country-code stripping can be done since the number is invalid. $this->assertEquals("+1 180-SIX", $this->phoneUtil->formatOutOfCountryKeepingAlphaChars($alphaNumericNumber, RegionCode::AQ)); }