libphonenumber\PhoneNumberUtil::formatNumberForMobileDialing PHP Метод

formatNumberForMobileDialing() публичный Метод

Returns a number formatted in such a way that it can be dialed from a mobile phone in a specific region. If the number cannot be reached from the region (e.g. some countries block toll-free numbers from being called outside of the country), the method returns an empty string.
public formatNumberForMobileDialing ( PhoneNumber $number, string $regionCallingFrom, boolean $withFormatting ) : string
$number PhoneNumber the phone number to be formatted
$regionCallingFrom string the region where the call is being placed
$withFormatting boolean whether the number should be returned with formatting symbols, such as spaces and dashes.
Результат string the formatted phone number
    public function formatNumberForMobileDialing(PhoneNumber $number, $regionCallingFrom, $withFormatting)
    {
        $countryCallingCode = $number->getCountryCode();
        if (!$this->hasValidCountryCallingCode($countryCallingCode)) {
            return $number->hasRawInput() ? $number->getRawInput() : "";
        }
        $formattedNumber = "";
        // Clear the extension, as that part cannot normally be dialed together with the main number.
        $numberNoExt = new PhoneNumber();
        $numberNoExt->mergeFrom($number)->clearExtension();
        $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode);
        $numberType = $this->getNumberType($numberNoExt);
        $isValidNumber = $numberType !== PhoneNumberType::UNKNOWN;
        if ($regionCallingFrom == $regionCode) {
            $isFixedLineOrMobile = $numberType == PhoneNumberType::FIXED_LINE || $numberType == PhoneNumberType::MOBILE || $numberType == PhoneNumberType::FIXED_LINE_OR_MOBILE;
            // Carrier codes may be needed in some countries. We handle this here.
            if ($regionCode == "CO" && $numberType == PhoneNumberType::FIXED_LINE) {
                $formattedNumber = $this->formatNationalNumberWithCarrierCode($numberNoExt, static::COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX);
            } elseif ($regionCode == "BR" && $isFixedLineOrMobile) {
                // Historically, we set this to an empty string when parsing with raw input if none was
                // found in the input string. However, this doesn't result in a number we can dial. For this
                // reason, we treat the empty string the same as if it isn't set at all.
                $formattedNumber = mb_strlen($numberNoExt->getPreferredDomesticCarrierCode()) > 0 ? $this->formatNationalNumberWithPreferredCarrierCode($numberNoExt, "") : "";
            } elseif ($isValidNumber && $regionCode == "HU") {
                // The national format for HU numbers doesn't contain the national prefix, because that is
                // how numbers are normally written down. However, the national prefix is obligatory when
                // dialing from a mobile phone, except for short numbers. As a result, we add it back here
                // if it is a valid regular length phone number.
                $formattedNumber = $this->getNddPrefixForRegion($regionCode, true) . " " . $this->format($numberNoExt, PhoneNumberFormat::NATIONAL);
            } elseif ($countryCallingCode === static::NANPA_COUNTRY_CODE) {
                // For NANPA countries, we output international format for numbers that can be dialed
                // internationally, since that always works, except for numbers which might potentially be
                // short numbers, which are always dialled in national format.
                $regionMetadata = $this->getMetadataForRegion($regionCallingFrom);
                if ($this->canBeInternationallyDialled($numberNoExt) && $this->testNumberLength($this->getNationalSignificantNumber($numberNoExt), $regionMetadata->getGeneralDesc()) !== ValidationResult::TOO_SHORT) {
                    $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::INTERNATIONAL);
                } else {
                    $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::NATIONAL);
                }
            } else {
                // For non-geographical countries, Mexican and Chilean fixed line and mobile numbers, we
                // output international format for numbers that can be dialed internationally as that always
                // works.
                if (($regionCode == static::REGION_CODE_FOR_NON_GEO_ENTITY || ($regionCode == "MX" || $regionCode == "CL") && $isFixedLineOrMobile) && $this->canBeInternationallyDialled($numberNoExt)) {
                    $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::INTERNATIONAL);
                } else {
                    $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::NATIONAL);
                }
            }
        } elseif ($isValidNumber && $this->canBeInternationallyDialled($numberNoExt)) {
            // We assume that short numbers are not diallable from outside their region, so if a number
            // is not a valid regular length phone number, we treat it as if it cannot be internationally
            // dialled.
            return $withFormatting ? $this->format($numberNoExt, PhoneNumberFormat::INTERNATIONAL) : $this->format($numberNoExt, PhoneNumberFormat::E164);
        }
        return $withFormatting ? $formattedNumber : static::normalizeDiallableCharsOnly($formattedNumber);
    }

Usage Example

 public function testFormatNumberForMobileDialing()
 {
     // US toll free numbers are marked as noInternationalDialling in the test metadata for testing
     // purposes.
     $this->assertEquals("800 253 0000", $this->phoneUtil->formatNumberForMobileDialing(self::$usTollFree, RegionCode::US, true));
     $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing(self::$usTollFree, RegionCode::CN, true));
     $this->assertEquals("+1 650 253 0000", $this->phoneUtil->formatNumberForMobileDialing(self::$usNumber, RegionCode::US, true));
     $usNumberWithExtn = new PhoneNumber();
     $usNumberWithExtn->mergeFrom(self::$usNumber)->setExtension("1234");
     $this->assertEquals("+1 650 253 0000", $this->phoneUtil->formatNumberForMobileDialing($usNumberWithExtn, RegionCode::US, true));
     $this->assertEquals("8002530000", $this->phoneUtil->formatNumberForMobileDialing(self::$usTollFree, RegionCode::US, false));
     $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing(self::$usTollFree, RegionCode::CN, false));
     $this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing(self::$usNumber, RegionCode::US, false));
     $this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usNumberWithExtn, RegionCode::US, false));
     // An invalid US number, which is one digit too long.
     $this->assertEquals("+165025300001", $this->phoneUtil->formatNumberForMobileDialing(self::$usLongNumber, RegionCode::US, false));
     $this->assertEquals("+1 65025300001", $this->phoneUtil->formatNumberForMobileDialing(self::$usLongNumber, RegionCode::US, true));
     // Star numbers. In real life they appear in Israel, but we have them in JP in our test
     // metadata.
     $this->assertEquals("*2345", $this->phoneUtil->formatNumberForMobileDialing(self::$jpStarNumber, RegionCode::JP, false));
     $this->assertEquals("*2345", $this->phoneUtil->formatNumberForMobileDialing(self::$jpStarNumber, RegionCode::JP, true));
     $this->assertEquals("+80012345678", $this->phoneUtil->formatNumberForMobileDialing(self::$internationalTollFree, RegionCode::JP, false));
     $this->assertEquals("+800 1234 5678", $this->phoneUtil->formatNumberForMobileDialing(self::$internationalTollFree, RegionCode::JP, true));
 }
All Usage Examples Of libphonenumber\PhoneNumberUtil::formatNumberForMobileDialing
PhoneNumberUtil