libphonenumber\PhoneNumberUtil::formatNationalNumberWithCarrierCode PHP Method

formatNationalNumberWithCarrierCode() public method

Formats a phone number in national format for dialing using the carrier as specified in the {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the phone number already has a preferred domestic carrier code stored. If {@code carrierCode} contains an empty string, returns the number in national format without any carrier code.
public formatNationalNumberWithCarrierCode ( PhoneNumber $number, string $carrierCode ) : string
$number PhoneNumber the phone number to be formatted
$carrierCode string the carrier selection code to be used
return string the formatted phone number in national format for dialing using the carrier as specified in the {@code carrierCode}
    public function formatNationalNumberWithCarrierCode(PhoneNumber $number, $carrierCode)
    {
        $countryCallingCode = $number->getCountryCode();
        $nationalSignificantNumber = $this->getNationalSignificantNumber($number);
        if (!$this->hasValidCountryCallingCode($countryCallingCode)) {
            return $nationalSignificantNumber;
        }
        // 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.
        $metadata = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode);
        $formattedNumber = $this->formatNsn($nationalSignificantNumber, $metadata, PhoneNumberFormat::NATIONAL, $carrierCode);
        $this->maybeAppendFormattedExtension($number, $metadata, PhoneNumberFormat::NATIONAL, $formattedNumber);
        $this->prefixNumberWithCountryCallingCode($countryCallingCode, PhoneNumberFormat::NATIONAL, $formattedNumber);
        return $formattedNumber;
    }

Usage Example

 public function testFormatWithCarrierCode()
 {
     // We only support this for AR in our test metadata, and only for mobile numbers starting with
     // certain values.
     $arMobile = new PhoneNumber();
     $arMobile->setCountryCode(54)->setNationalNumber(92234654321);
     $this->assertEquals("02234 65-4321", $this->phoneUtil->format($arMobile, PhoneNumberFormat::NATIONAL));
     // Here we force 14 as the carrier code.
     $this->assertEquals("02234 14 65-4321", $this->phoneUtil->formatNationalNumberWithCarrierCode($arMobile, "14"));
     // Here we force the number to be shown with no carrier code.
     $this->assertEquals("02234 65-4321", $this->phoneUtil->formatNationalNumberWithCarrierCode($arMobile, ""));
     // Here the international rule is used, so no carrier code should be present.
     $this->assertEquals("+5492234654321", $this->phoneUtil->format($arMobile, PhoneNumberFormat::E164));
     // We don't support this for the US so there should be no change.
     $this->assertEquals("650 253 0000", $this->phoneUtil->formatNationalNumberWithCarrierCode(self::$usNumber, "15"));
 }
PhoneNumberUtil