Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting.
public formatNationalNumberWithPreferredCarrierCode ( |
||
$number | the phone number to be formatted | |
$fallbackCarrierCode | string | the carrier selection code to be used, if none is found in the phone number itself |
return | string | the formatted phone number in national format for dialing using the number's {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if none is found |
public function formatNationalNumberWithPreferredCarrierCode(PhoneNumber $number, $fallbackCarrierCode)
{
return $this->formatNationalNumberWithCarrierCode($number, mb_strlen($number->getPreferredDomesticCarrierCode()) > 0 ? $number->getPreferredDomesticCarrierCode() : $fallbackCarrierCode);
}
public function testFormatWithPreferredCarrierCode() { // We only support this for AR in our test metadata. $arNumber = new PhoneNumber(); $arNumber->setCountryCode(54)->setNationalNumber(91234125678); // Test formatting with no preferred carrier code stored in the number itself. $this->assertEquals("01234 15 12-5678", $this->phoneUtil->formatNationalNumberWithPreferredCarrierCode($arNumber, "15")); $this->assertEquals("01234 12-5678", $this->phoneUtil->formatNationalNumberWithPreferredCarrierCode($arNumber, "")); // Test formatting with preferred carrier code present. $arNumber->setPreferredDomesticCarrierCode("19"); $this->assertEquals("01234 12-5678", $this->phoneUtil->format($arNumber, PhoneNumberFormat::NATIONAL)); $this->assertEquals("01234 19 12-5678", $this->phoneUtil->formatNationalNumberWithPreferredCarrierCode($arNumber, "15")); $this->assertEquals("01234 19 12-5678", $this->phoneUtil->formatNationalNumberWithPreferredCarrierCode($arNumber, "")); // When the preferred_domestic_carrier_code is present (even when it contains an empty string), // use it instead of the default carrier code passed in. $arNumber->setPreferredDomesticCarrierCode(""); $this->assertEquals("01234 12-5678", $this->phoneUtil->formatNationalNumberWithPreferredCarrierCode($arNumber, "15")); // We don't support this for the US so there should be no change. $usNumber = new PhoneNumber(); $usNumber->setCountryCode(1)->setNationalNumber(4241231234)->setPreferredDomesticCarrierCode("99"); $this->assertEquals("424 123 1234", $this->phoneUtil->format($usNumber, PhoneNumberFormat::NATIONAL)); $this->assertEquals("424 123 1234", $this->phoneUtil->formatNationalNumberWithPreferredCarrierCode($usNumber, "15")); }