libphonenumber\PhoneNumberUtil::formatInOriginalFormat PHP Method

formatInOriginalFormat() public method

The original format is embedded in the country_code_source field of the PhoneNumber object passed in. If such information is missing, the number will be formatted into the NATIONAL format by default. When the number contains a leading zero and this is unexpected for this country, or we don't have a formatting pattern for the number, the method returns the raw input when it is available. Note this method guarantees no digit will be inserted, removed or modified as a result of formatting.
public formatInOriginalFormat ( PhoneNumber $number, string $regionCallingFrom ) : string
$number PhoneNumber the phone number that needs to be formatted in its original number format
$regionCallingFrom string the region whose IDD needs to be prefixed if the original number has one
return string the formatted phone number in its original number format
    public function formatInOriginalFormat(PhoneNumber $number, $regionCallingFrom)
    {
        if ($number->hasRawInput() && ($this->hasUnexpectedItalianLeadingZero($number) || !$this->hasFormattingPatternForNumber($number))) {
            // We check if we have the formatting pattern because without that, we might format the number
            // as a group without national prefix.
            return $number->getRawInput();
        }
        if (!$number->hasCountryCodeSource()) {
            return $this->format($number, PhoneNumberFormat::NATIONAL);
        }
        switch ($number->getCountryCodeSource()) {
            case CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN:
                $formattedNumber = $this->format($number, PhoneNumberFormat::INTERNATIONAL);
                break;
            case CountryCodeSource::FROM_NUMBER_WITH_IDD:
                $formattedNumber = $this->formatOutOfCountryCallingNumber($number, $regionCallingFrom);
                break;
            case CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN:
                $formattedNumber = substr($this->format($number, PhoneNumberFormat::INTERNATIONAL), 1);
                break;
            case CountryCodeSource::FROM_DEFAULT_COUNTRY:
                // Fall-through to default case.
            // Fall-through to default case.
            default:
                $regionCode = $this->getRegionCodeForCountryCode($number->getCountryCode());
                // We strip non-digits from the NDD here, and from the raw input later, so that we can
                // compare them easily.
                $nationalPrefix = $this->getNddPrefixForRegion($regionCode, true);
                $nationalFormat = $this->format($number, PhoneNumberFormat::NATIONAL);
                if ($nationalPrefix === null || mb_strlen($nationalPrefix) == 0) {
                    // If the region doesn't have a national prefix at all, we can safely return the national
                    // format without worrying about a national prefix being added.
                    $formattedNumber = $nationalFormat;
                    break;
                }
                // Otherwise, we check if the original number was entered with a national prefix.
                if ($this->rawInputContainsNationalPrefix($number->getRawInput(), $nationalPrefix, $regionCode)) {
                    // If so, we can safely return the national format.
                    $formattedNumber = $nationalFormat;
                    break;
                }
                // Metadata cannot be null here because getNddPrefixForRegion() (above) returns null if
                // there is no metadata for the region.
                $metadata = $this->getMetadataForRegion($regionCode);
                $nationalNumber = $this->getNationalSignificantNumber($number);
                $formatRule = $this->chooseFormattingPatternForNumber($metadata->numberFormats(), $nationalNumber);
                // The format rule could still be null here if the national number was 0 and there was no
                // raw input (this should not be possible for numbers generated by the phonenumber library
                // as they would also not have a country calling code and we would have exited earlier).
                if ($formatRule === null) {
                    $formattedNumber = $nationalFormat;
                    break;
                }
                // When the format we apply to this number doesn't contain national prefix, we can just
                // return the national format.
                // TODO: Refactor the code below with the code in isNationalPrefixPresentIfRequired.
                $candidateNationalPrefixRule = $formatRule->getNationalPrefixFormattingRule();
                // We assume that the first-group symbol will never be _before_ the national prefix.
                $indexOfFirstGroup = strpos($candidateNationalPrefixRule, '$1');
                if ($indexOfFirstGroup <= 0) {
                    $formattedNumber = $nationalFormat;
                    break;
                }
                $candidateNationalPrefixRule = substr($candidateNationalPrefixRule, 0, $indexOfFirstGroup);
                $candidateNationalPrefixRule = static::normalizeDigitsOnly($candidateNationalPrefixRule);
                if (mb_strlen($candidateNationalPrefixRule) == 0) {
                    // National prefix not used when formatting this number.
                    $formattedNumber = $nationalFormat;
                    break;
                }
                // Otherwise, we need to remove the national prefix from our output.
                $numFormatCopy = new NumberFormat();
                $numFormatCopy->mergeFrom($formatRule);
                $numFormatCopy->clearNationalPrefixFormattingRule();
                $numberFormats = array();
                $numberFormats[] = $numFormatCopy;
                $formattedNumber = $this->formatByPattern($number, PhoneNumberFormat::NATIONAL, $numberFormats);
                break;
        }
        $rawInput = $number->getRawInput();
        // If no digit is inserted/removed/modified as a result of our formatting, we return the
        // formatted phone number; otherwise we return the raw input the user entered.
        if ($formattedNumber !== null && mb_strlen($rawInput) > 0) {
            $normalizedFormattedNumber = static::normalizeDiallableCharsOnly($formattedNumber);
            $normalizedRawInput = static::normalizeDiallableCharsOnly($rawInput);
            if ($normalizedFormattedNumber != $normalizedRawInput) {
                $formattedNumber = $rawInput;
            }
        }
        return $formattedNumber;
    }

Usage Example

 public function testFormatInOriginalFormat()
 {
     $number1 = $this->phoneUtil->parseAndKeepRawInput("+442087654321", RegionCode::GB);
     $this->assertEquals("+44 20 8765 4321", $this->phoneUtil->formatInOriginalFormat($number1, RegionCode::GB));
     $number2 = $this->phoneUtil->parseAndKeepRawInput("02087654321", RegionCode::GB);
     $this->assertEquals("(020) 8765 4321", $this->phoneUtil->formatInOriginalFormat($number2, RegionCode::GB));
     $number3 = $this->phoneUtil->parseAndKeepRawInput("011442087654321", RegionCode::US);
     $this->assertEquals("011 44 20 8765 4321", $this->phoneUtil->formatInOriginalFormat($number3, RegionCode::US));
     $number4 = $this->phoneUtil->parseAndKeepRawInput("442087654321", RegionCode::GB);
     $this->assertEquals("44 20 8765 4321", $this->phoneUtil->formatInOriginalFormat($number4, RegionCode::GB));
     $number5 = $this->phoneUtil->parse("+442087654321", RegionCode::GB);
     $this->assertEquals("(020) 8765 4321", $this->phoneUtil->formatInOriginalFormat($number5, RegionCode::GB));
     // Invalid numbers that we have a formatting pattern for should be formatted properly. Note area
     // codes starting with 7 are intentionally excluded in the test metadata for testing purposes.
     $number6 = $this->phoneUtil->parseAndKeepRawInput("7345678901", RegionCode::US);
     $this->assertEquals("734 567 8901", $this->phoneUtil->formatInOriginalFormat($number6, RegionCode::US));
     // US is not a leading zero country, and the presence of the leading zero leads us to format the
     // number using raw_input.
     $number7 = $this->phoneUtil->parseAndKeepRawInput("0734567 8901", RegionCode::US);
     $this->assertEquals("0734567 8901", $this->phoneUtil->formatInOriginalFormat($number7, RegionCode::US));
     // This number is valid, but we don't have a formatting pattern for it. Fall back to the raw
     // input.
     $number8 = $this->phoneUtil->parseAndKeepRawInput("02-4567-8900", RegionCode::KR);
     $this->assertEquals("02-4567-8900", $this->phoneUtil->formatInOriginalFormat($number8, RegionCode::KR));
     $number9 = $this->phoneUtil->parseAndKeepRawInput("01180012345678", RegionCode::US);
     $this->assertEquals("011 800 1234 5678", $this->phoneUtil->formatInOriginalFormat($number9, RegionCode::US));
     $number10 = $this->phoneUtil->parseAndKeepRawInput("+80012345678", RegionCode::KR);
     $this->assertEquals("+800 1234 5678", $this->phoneUtil->formatInOriginalFormat($number10, RegionCode::KR));
     // US local numbers are formatted correctly, as we have formatting patterns for them.
     $localNumberUS = $this->phoneUtil->parseAndKeepRawInput("2530000", RegionCode::US);
     $this->assertEquals("253 0000", $this->phoneUtil->formatInOriginalFormat($localNumberUS, RegionCode::US));
     $numberWithNationalPrefixUS = $this->phoneUtil->parseAndKeepRawInput("18003456789", RegionCode::US);
     $this->assertEquals("1 800 345 6789", $this->phoneUtil->formatInOriginalFormat($numberWithNationalPrefixUS, RegionCode::US));
     $numberWithoutNationalPrefixGB = $this->phoneUtil->parseAndKeepRawInput("2087654321", RegionCode::GB);
     $this->assertEquals("20 8765 4321", $this->phoneUtil->formatInOriginalFormat($numberWithoutNationalPrefixGB, RegionCode::GB));
     // Make sure no metadata is modified as a result of the previous function call.
     $this->assertEquals("(020) 8765 4321", $this->phoneUtil->formatInOriginalFormat($number5, RegionCode::GB));
     $numberWithNationalPrefixMX = $this->phoneUtil->parseAndKeepRawInput("013312345678", RegionCode::MX);
     $this->assertEquals("01 33 1234 5678", $this->phoneUtil->formatInOriginalFormat($numberWithNationalPrefixMX, RegionCode::MX));
     $numberWithoutNationalPrefixMX = $this->phoneUtil->parseAndKeepRawInput("3312345678", RegionCode::MX);
     $this->assertEquals("33 1234 5678", $this->phoneUtil->formatInOriginalFormat($numberWithoutNationalPrefixMX, RegionCode::MX));
     $italianFixedLineNumber = $this->phoneUtil->parseAndKeepRawInput("0212345678", RegionCode::IT);
     $this->assertEquals("02 1234 5678", $this->phoneUtil->formatInOriginalFormat($italianFixedLineNumber, RegionCode::IT));
     $numberWithNationalPrefixJP = $this->phoneUtil->parseAndKeepRawInput("00777012", RegionCode::JP);
     $this->assertEquals("0077-7012", $this->phoneUtil->formatInOriginalFormat($numberWithNationalPrefixJP, RegionCode::JP));
     $numberWithoutNationalPrefixJP = $this->phoneUtil->parseAndKeepRawInput("0777012", RegionCode::JP);
     $this->assertEquals("0777012", $this->phoneUtil->formatInOriginalFormat($numberWithoutNationalPrefixJP, RegionCode::JP));
     $numberWithCarrierCodeBR = $this->phoneUtil->parseAndKeepRawInput("012 3121286979", RegionCode::BR);
     $this->assertEquals("012 3121286979", $this->phoneUtil->formatInOriginalFormat($numberWithCarrierCodeBR, RegionCode::BR));
     // The default national prefix used in this case is 045. When a number with national prefix 044
     // is entered, we return the raw input as we don't want to change the number entered.
     $numberWithNationalPrefixMX1 = $this->phoneUtil->parseAndKeepRawInput("044(33)1234-5678", RegionCode::MX);
     $this->assertEquals("044(33)1234-5678", $this->phoneUtil->formatInOriginalFormat($numberWithNationalPrefixMX1, RegionCode::MX));
     $numberWithNationalPrefixMX2 = $this->phoneUtil->parseAndKeepRawInput("045(33)1234-5678", RegionCode::MX);
     $this->assertEquals("045 33 1234 5678", $this->phoneUtil->formatInOriginalFormat($numberWithNationalPrefixMX2, RegionCode::MX));
     // The default international prefix used in this case is 0011. When a number with international
     // prefix 0012 is entered, we return the raw input as we don't want to change the number
     // entered.
     $outOfCountryNumberFromAU1 = $this->phoneUtil->parseAndKeepRawInput("0012 16502530000", RegionCode::AU);
     $this->assertEquals("0012 16502530000", $this->phoneUtil->formatInOriginalFormat($outOfCountryNumberFromAU1, RegionCode::AU));
     $outOfCountryNumberFromAU2 = $this->phoneUtil->parseAndKeepRawInput("0011 16502530000", RegionCode::AU);
     $this->assertEquals("0011 1 650 253 0000", $this->phoneUtil->formatInOriginalFormat($outOfCountryNumberFromAU2, RegionCode::AU));
     // Test the star sign is not removed from or added to the original input by this method.
     $starNumber = $this->phoneUtil->parseAndKeepRawInput("*1234", RegionCode::JP);
     $this->assertEquals("*1234", $this->phoneUtil->formatInOriginalFormat($starNumber, RegionCode::JP));
     $numberWithoutStar = $this->phoneUtil->parseAndKeepRawInput("1234", RegionCode::JP);
     $this->assertEquals("1234", $this->phoneUtil->formatInOriginalFormat($numberWithoutStar, RegionCode::JP));
     // Test an invalid national number without raw input is just formatted as the national number.
     $this->assertEquals("650253000", $this->phoneUtil->formatInOriginalFormat(self::$usShortByOneNumber, RegionCode::US));
 }
PhoneNumberUtil