src/PhoneNumberUtil.php
public function format(PhoneNumber $number, $numberFormat)
    {
        if ($number->getNationalNumber() == 0 && $number->hasRawInput()) {
            // Unparseable numbers that kept their raw input just use that.
            // This is the only case where a number can be formatted as E164 without a
            // leading '+' symbol (but the original number wasn't parseable anyway).
            // TODO: Consider removing the 'if' above so that unparseable
            // strings without raw input format to the empty string instead of "+00"
            $rawInput = $number->getRawInput();
            if (mb_strlen($rawInput) > 0) {
                return $rawInput;
            }
        }

        $formattedNumber = '';
        $countryCallingCode = $number->getCountryCode();
        $nationalSignificantNumber = $this->getNationalSignificantNumber($number);

        if ($numberFormat == PhoneNumberFormat::E164) {
            // Early exit for E164 case (even if the country calling code is invalid) since no formatting
            // of the national number needs to be applied. Extensions are not formatted.
            $formattedNumber .= $nationalSignificantNumber;
            $this->prefixNumberWithCountryCallingCode($countryCallingCode, PhoneNumberFormat::E164, $formattedNumber);
            return $formattedNumber;
        }

        if (!$this->hasValidCountryCallingCode($countryCallingCode)) {
            $formattedNumber .= $nationalSignificantNumber;
            return $formattedNumber;
        }

        // Note getRegionCodeForCountryCode() is used because formatting information for regions which
        // share a country calling code is contained by only one region for performance reasons. For
        // example, for NANPA regions it will be contained in the metadata for US.
        $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode);
        // Metadata cannot be null because the country calling code is valid (which means that the
        // region code cannot be ZZ and must be one of our supported region codes).
        $metadata = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode);
        $formattedNumber .= $this->formatNsn($nationalSignificantNumber, $metadata, $numberFormat);
        $this->maybeAppendFormattedExtension($number, $metadata, $numberFormat, $formattedNumber);
        $this->prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, $formattedNumber);
        return $formattedNumber;
    }
public function format(PhoneNumber $number, $numberFormat)
    {
        if ($number->getNationalNumber() == 0 && $number->hasRawInput()) {
            // Unparseable numbers that kept their raw input just use that.
            // This is the only case where a number can be formatted as E164 without a
            // leading '+' symbol (but the original number wasn't parseable anyway).
            // TODO: Consider removing the 'if' above so that unparseable
            // strings without raw input format to the empty string instead of "+00"
            $rawInput = $number->getRawInput();
            if (mb_strlen($rawInput) > 0) {
                return $rawInput;
            }
        }

        $formattedNumber = '';
        $countryCallingCode = $number->getCountryCode();
        $nationalSignificantNumber = $this->getNationalSignificantNumber($number);

        if ($numberFormat == PhoneNumberFormat::E164) {
            // Early exit for E164 case (even if the country calling code is invalid) since no formatting
            // of the national number needs to be applied. Extensions are not formatted.
            $formattedNumber .= $nationalSignificantNumber;
            $this->prefixNumberWithCountryCallingCode($countryCallingCode, PhoneNumberFormat::E164, $formattedNumber);
            return $formattedNumber;
        }

        if (!$this->hasValidCountryCallingCode($countryCallingCode)) {
            $formattedNumber .= $nationalSignificantNumber;
            return $formattedNumber;
        }

        // Note getRegionCodeForCountryCode() is used because formatting information for regions which
        // share a country calling code is contained by only one region for performance reasons. For
        // example, for NANPA regions it will be contained in the metadata for US.
        $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode);
        // Metadata cannot be null because the country calling code is valid (which means that the
        // region code cannot be ZZ and must be one of our supported region codes).
        $metadata = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode);
        $formattedNumber .= $this->formatNsn($nationalSignificantNumber, $metadata, $numberFormat);
        $this->maybeAppendFormattedExtension($number, $metadata, $numberFormat, $formattedNumber);
        $this->prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, $formattedNumber);
        return $formattedNumber;
    }
public function format ( PhoneNumber $ number , $ numberFormat ) { if ( $ number -> getNationalNumber ( ) == 0 && $ number -> hasRawInput ( ) ) { // Unparseable numbers that kept their raw input just use that. // This is the only case where a number can be formatted as E164 without a // leading '+' symbol (but the original number wasn't parseable anyway). // TODO: Consider removing the 'if' above so that unparseable // strings without raw input format to the empty string instead of "+00" $ rawInput = $ number -> getRawInput ( ) ; if ( mb_strlen ( $ rawInput ) > 0 ) { return $ rawInput ; } } $ formattedNumber = '' ; $ countryCallingCode = $ number -> getCountryCode ( ) ; $ nationalSignificantNumber = $ this -> getNationalSignificantNumber ( $ number ) ; if ( $ numberFormat == PhoneNumberFormat :: E164 ) { // Early exit for E164 case (even if the country calling code is invalid) since no formatting // of the national number needs to be applied. Extensions are not formatted. $ formattedNumber .= $ nationalSignificantNumber ; $ this -> prefixNumberWithCountryCallingCode ( $ countryCallingCode , PhoneNumberFormat :: E164 , $ formattedNumber ) ; return $ formattedNumber ; } if ( ! $ this -> hasValidCountryCallingCode ( $ countryCallingCode ) ) { $ formattedNumber .= $ nationalSignificantNumber ; return $ formattedNumber ; } // Note getRegionCodeForCountryCode() is used because formatting information for regions which // share a country calling code is contained by only one region for performance reasons. For // example, for NANPA regions it will be contained in the metadata for US. $ regionCode = $ this -> getRegionCodeForCountryCode ( $ countryCallingCode ) ; // Metadata cannot be null because the country calling code is valid (which means that the // region code cannot be ZZ and must be one of our supported region codes). $ metadata = $ this -> getMetadataForRegionOrCallingCode ( $ countryCallingCode , $ regionCode ) ; $ formattedNumber .= $ this -> formatNsn ( $ nationalSignificantNumber , $ metadata , $ numberFormat ) ; $ this -> maybeAppendFormattedExtension ( $ number , $ metadata , $ numberFormat , $ formattedNumber ) ; $ this -> prefixNumberWithCountryCallingCode ( $ countryCallingCode , $ numberFormat , $ formattedNumber ) ; return $ formattedNumber ; }
Formats a phone number in the specified format using default rules. Note that this does not
promise to produce a phone number that the user can dial from where they are - although we do
format in either 'national' or 'international' format depending on what the client asks for, we
do not currently support a more abbreviated format, such as for users in the same "area" who
could potentially dial the number without area code. Note that if the phone number has a
country calling code of 0 or an otherwise invalid country calling code, we cannot work out
which formatting rules to apply so we return the national significant number with no formatting
applied.

@param PhoneNumber $number the phone number to be formatted
@param int $numberFormat the PhoneNumberFormat the phone number should be formatted into
@return string the formatted phone number
Formats a phone number in the specified format using default rules . Note that this does not promise to produce a phone number that the user can dial from where they are - although we do format in either national or international format depending on what the client asks for we do not currently support a more abbreviated format such as for users in the same area who could potentially dial the number without area code . Note that if the phone number has a country calling code of 0 or an otherwise invalid country calling code we cannot work out which formatting rules to apply so we return the national significant number with no formatting applied .