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 ( |
||
$number | 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 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)); }
public function testFormatNumberForMobileDialing() { // Numbers are normally dialed in national format in-country, and international format from // outside the country. $this->assertEquals("030123456", $this->phoneUtil->formatNumberForMobileDialing(self::$deNumber, RegionCode::DE, false)); $this->assertEquals("+4930123456", $this->phoneUtil->formatNumberForMobileDialing(self::$deNumber, RegionCode::CH, false)); $this->assertEquals("+4930123456", $this->phoneUtil->formatNumberForMobileDialing(self::$deNumber, RegionCode::CH, false)); $deNumberWithExtn = new PhoneNumber(); $deNumberWithExtn->mergeFrom(self::$deNumber)->setExtension("1234"); $this->assertEquals("030123456", $this->phoneUtil->formatNumberForMobileDialing($deNumberWithExtn, RegionCode::DE, false)); $this->assertEquals("+4930123456", $this->phoneUtil->formatNumberForMobileDialing($deNumberWithExtn, RegionCode::CH, false)); // US toll free numbers are marked as noInternationalDialling in the test metadata for testing // purposes. For such numbers, we expect nothing to be returned when the region code is not the // same one. $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)); // UAE numbers beginning with 600 (classified as UAN) need to be dialled without +971 locally. $this->assertEquals("+971600123456", $this->phoneUtil->formatNumberForMobileDialing(self::$aeUAN, RegionCode::JP, false)); $this->assertEquals("600123456", $this->phoneUtil->formatNumberForMobileDialing(self::$aeUAN, RegionCode::AE, false)); $this->assertEquals("+523312345678", $this->phoneUtil->formatNumberForMobileDialing(self::$mxNumber1, RegionCode::MX, false)); $this->assertEquals("+523312345678", $this->phoneUtil->formatNumberForMobileDialing(self::$mxNumber1, RegionCode::US, false)); // Non-geographical numbers should always be dialed in international format. $this->assertEquals("+80012345678", $this->phoneUtil->formatNumberForMobileDialing(self::$internationalTollFree, RegionCode::US, false)); $this->assertEquals("+80012345678", $this->phoneUtil->formatNumberForMobileDialing(self::$internationalTollFree, RegionCode::UN001, false)); // Test that a short number is formatted correctly for mobile dialing within the region, // and is not diallable from outside the region. $deShortNumber = new PhoneNumber(); $deShortNumber->setCountryCode(49)->setNationalNumber(123); $this->assertEquals("123", $this->phoneUtil->formatNumberForMobileDialing($deShortNumber, RegionCode::DE, false)); $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($deShortNumber, RegionCode::IT, false)); // Test the special logic for Hungary, where the national prefix must be added before dialing // from a mobile phone for regular length numbers, but not for short numbers. $huRegularNumber = new PhoneNumber(); $huRegularNumber->setCountryCode(36)->setNationalNumber(301234567); $this->assertEquals("06301234567", $this->phoneUtil->formatNumberForMobileDialing($huRegularNumber, RegionCode::HU, false)); $this->assertEquals("+36301234567", $this->phoneUtil->formatNumberForMobileDialing($huRegularNumber, RegionCode::JP, false)); $huShortNumber = new PhoneNumber(); $huShortNumber->setCountryCode(36)->setNationalNumber(104); $this->assertEquals("104", $this->phoneUtil->formatNumberForMobileDialing($huShortNumber, RegionCode::HU, false)); $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($huShortNumber, RegionCode::JP, false)); // Test the special logic for NANPA countries, for which regular length phone numbers are always // output in international format, but short numbers are in national format. $usRegularNumber = new PhoneNumber(); $usRegularNumber->setCountryCode(1)->setNationalNumber(6502530000); $this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usRegularNumber, RegionCode::US, false)); $this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usRegularNumber, RegionCode::CA, false)); $this->assertEquals("+16502530000", $this->phoneUtil->formatNumberForMobileDialing($usRegularNumber, RegionCode::BR, false)); $usShortNumber = new PhoneNumber(); $usShortNumber->setCountryCode(1)->setNationalNumber(911); $this->assertEquals("911", $this->phoneUtil->formatNumberForMobileDialing($usShortNumber, RegionCode::US, false)); $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($usShortNumber, RegionCode::CA, false)); $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($usShortNumber, RegionCode::BR, false)); // Test that the Australian emergency number 000 is formatted correctly. $auNumber = new PhoneNumber(); $auNumber->setCountryCode(61)->setNationalNumber(0)->setItalianLeadingZero(true)->setNumberOfLeadingZeros(2); $this->assertEquals("000", $this->phoneUtil->formatNumberForMobileDialing($auNumber, RegionCode::AU, false)); $this->assertEquals("", $this->phoneUtil->formatNumberForMobileDialing($auNumber, RegionCode::NZ, false)); }