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

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.
public format ( PhoneNumber $number, integer $numberFormat ) : string
$number PhoneNumber the phone number to be formatted
$numberFormat integer the PhoneNumberFormat the phone number should be formatted into
Результат string the formatted phone number
Пример #1
0
 /**
  * @return string
  */
 public function __toString()
 {
     $phoneNumber = $this->getPhoneNumber();
     $format = $this->getFormat();
     if (!$this->getPhoneNumber() instanceof PhoneNumber) {
         $phoneNumber = $this->parsePhoneNumber();
     }
     return $this->libPhoneNumber->format($phoneNumber, $format);
 }
Пример #2
0
 public function filter($value)
 {
     try {
         $NumberProto = $this->libPhoneNumber->parse($value, $this->getCountry());
     } catch (NumberParseException $e) {
         return $value;
     }
     return $this->libPhoneNumber->format($NumberProto, PhoneNumberFormat::E164);
 }
Пример #3
0
 public function testLongerNumber()
 {
     $number = "12345678901234567";
     $phoneNumber = $this->phoneUtil->parse($number, "DE");
     $this->assertEquals('+4912345678901234567', $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::E164));
     $this->assertEquals('+49 12345678901234567', $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::INTERNATIONAL));
     $this->assertEquals('12345678901234567', $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::NATIONAL));
     $this->assertEquals('011 49 12345678901234567', $this->phoneUtil->formatOutOfCountryCallingNumber($phoneNumber, 'US'));
     $this->assertEquals('00 49 12345678901234567', $this->phoneUtil->formatOutOfCountryCallingNumber($phoneNumber, 'CH'));
 }
 /**
  * Returns a plain phonenumber readable for mobile devices
  *
  * @param string $phoneNumber Formatted Phone Number
  * @return string
  */
 public function render($phoneNumber)
 {
     $plainPhoneNumber = '';
     if ($phoneNumber) {
         /** @var \libphonenumber\PhoneNumber $plainPhoneNumberPrototype */
         $plainPhoneNumberPrototype = $this->phoneNumberUtility->parse($phoneNumber, 'DE');
         $plainPhoneNumber = $this->phoneNumberUtility->format($plainPhoneNumberPrototype, \libphonenumber\PhoneNumberFormat::RFC3966);
     }
     return $plainPhoneNumber;
 }
 /**
  * Format a phone number.
  *
  * @param PhoneNumber $phoneNumber Phone number.
  * @param int|string  $format      Format, or format constant name.
  *
  * @return string Formatted phone number.
  *
  * @throws InvalidArgumentException If an argument is invalid.
  */
 public function format(PhoneNumber $phoneNumber, $format = PhoneNumberFormat::INTERNATIONAL)
 {
     if (true === is_string($format)) {
         $constant = '\\libphonenumber\\PhoneNumberFormat::' . $format;
         if (false === defined($constant)) {
             throw new InvalidArgumentException('The format must be either a constant value or name in libphonenumber\\PhoneNumberFormat');
         }
         $format = constant('\\libphonenumber\\PhoneNumberFormat::' . $format);
     }
     return $this->phoneNumberUtil->format($phoneNumber, $format);
 }
 /**
  * @dataProvider reverseTransformProvider
  */
 public function testReverseTransform($defaultRegion, $actual, $expected)
 {
     $transformer = new PhoneNumberToStringTransformer($defaultRegion);
     try {
         $transformed = $transformer->reverseTransform($actual);
     } catch (TransformationFailedException $e) {
         $transformed = self::TRANSFORMATION_FAILED;
     }
     if ($transformed instanceof PhoneNumber) {
         $transformed = $this->phoneNumberUtil->format($transformed, PhoneNumberFormat::E164);
     }
     $this->assertSame($expected, $transformed);
 }
 public function testUnknownCountryCallingCode()
 {
     $this->assertFalse($this->phoneUtil->isValidNumber(self::$unknownCountryCodeNoRawInput));
     // It's not very well defined as to what the E164 representation for a number with an invalid
     // country calling code is, but just prefixing the country code and national number is about
     // the best we can do.
     $this->assertEquals("+212345", $this->phoneUtil->format(self::$unknownCountryCodeNoRawInput, PhoneNumberFormat::E164));
 }
Пример #8
0
 /**
  * @param string $country
  * @param string $format
  *
  * @return string|NULL
  *
  * @throws Exceptions\NoValidCountryException
  */
 protected function getExampleNumber($country, $format)
 {
     // Check if country is valid
     $country = $this->validateCountry($country);
     // Create example number
     $number = $this->phoneNumberUtil->getExampleNumber($country);
     return $number !== NULL ? $this->phoneNumberUtil->format($number, $format) : NULL;
 }
Пример #9
0
 protected function normalizePhoneNumber($number, $externalId, $defaultRegion)
 {
     try {
         $number = $this->phoneNumberUtil->parse($number, $defaultRegion);
     } catch (NumberParseException $e) {
         $this->logger->warning("Error parsing phone number from source '%s' for entry '%s' (code %d): %s", $this->source, $externalId, $e->getCode(), $e->getMessage());
         return null;
     }
     return $this->phoneNumberUtil->format($number, PhoneNumberFormat::E164);
 }
 /**
  * @dataProvider regionList
  * @param string $regionCode
  */
 public function testEveryRegionHasExampleNumber($regionCode)
 {
     $exampleNumber = $this->phoneNumberUtil->getExampleNumber($regionCode);
     $this->assertNotNull($exampleNumber, "None found for region " . $regionCode);
     /*
      * Check the number is valid
      */
     $e164 = $this->phoneNumberUtil->format($exampleNumber, PhoneNumberFormat::E164);
     $phoneObject = $this->phoneNumberUtil->parse($e164, 'ZZ');
     $this->assertEquals($phoneObject, $exampleNumber);
     $this->assertTrue($this->phoneNumberUtil->isValidNumber($phoneObject));
     $this->assertTrue($this->phoneNumberUtil->isValidNumberForRegion($phoneObject, $regionCode));
 }
 public function testChildLine()
 {
     $number = '08001111';
     $phoneObject = $this->phoneUtil->parse($number, 'GB');
     $valid = $this->phoneUtil->isValidNumber($phoneObject);
     $this->assertTrue($valid, "Checking phone number is valid");
     $type = $this->phoneUtil->getNumberType($phoneObject);
     $this->assertEquals(PhoneNumberType::TOLL_FREE, $type, "Checking phone number is detected as TOLL FREE");
     $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164);
     $this->assertEquals("+448001111", $formattedE164, "Checking E164 format is correct");
     $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL);
     $this->assertEquals("0800 1111", $formattedNational, "Checking National format is correct");
 }
 public function testFormatNumberWithExtension()
 {
     $nzNumber = new PhoneNumber();
     $nzNumber->mergeFrom(self::$nzNumber)->setExtension("1234");
     // Uses default extension prefix:
     $this->assertEquals("03-331 6005 ext. 1234", $this->phoneUtil->format($nzNumber, PhoneNumberFormat::NATIONAL));
     // Uses RFC 3966 syntax.
     $this->assertEquals("+64-3-331-6005;ext=1234", $this->phoneUtil->format($nzNumber, PhoneNumberFormat::RFC3966));
     // Extension prefix overridden in the territory information for the US:
     $usNumberWithExtension = new PhoneNumber();
     $usNumberWithExtension->mergeFrom(self::$usNumber)->setExtension("4567");
     $this->assertEquals("650 253 0000 extn. 4567", $this->phoneUtil->format($usNumberWithExtension, PhoneNumberFormat::NATIONAL));
 }
 private function parse()
 {
     if ($this->phoneNumberProto) {
         // from phoneNumberProto
         $this->country_code = $this->phoneNumberProto->getCountryCode();
         $this->country_code_plus_sign = "+" . $this->country_code;
         $this->national_number = $this->phoneNumberProto->getNationalNumber();
         $this->extension = $this->phoneNumberProto->getExtension();
         $this->country_code_source = $this->phoneNumberProto->getCountryCodeSource();
         if (isset($this->countryCodeSourcesText[$this->country_code_source])) {
             $this->country_code_source_text = $this->countryCodeSourcesText[$this->country_code_source];
         }
         $this->raw_input = $this->phoneNumberProto->getRawInput();
         $this->preferred_domestic_carrier_code = $this->phoneNumberProto->getPreferredDomesticCarrierCode();
         // from validation
         $this->is_possible_number = $this->phoneUtil->isPossibleNumber($this->phoneNumberProto);
         $this->is_valid_number = $this->phoneUtil->isValidNumber($this->phoneNumberProto);
         $this->region_code_for_number = $this->phoneUtil->getRegionCodeForNumber($this->phoneNumberProto);
         $this->number_type = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         $this->number_type_text = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         if (isset($this->phoneNumberTypesText[$this->number_type])) {
             $this->number_type_text = $this->phoneNumberTypesText[$this->number_type];
         } else {
             $this->number_type_text = "OTHER";
         }
         $this->is_mobile_number = in_array($this->number_type, [PhoneNumberType::FIXED_LINE_OR_MOBILE, PhoneNumberType::MOBILE]);
         // from formatting
         $this->format_e164 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::E164);
         $this->format_international = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::INTERNATIONAL);
         $this->format_national = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::NATIONAL);
         $this->format_rfc3966 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::RFC3966);
         // from additional
         $phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder::getInstance();
         $this->description = $phoneNumberOfflineGeocoder->getDescriptionForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToCarrierMapper = PhoneNumberToCarrierMapper::getInstance();
         $this->carrier_name = $phoneNumberToCarrierMapper->getNameForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToTimeZonesMapper = PhoneNumberToTimeZonesMapper::getInstance();
         $this->timezones = $phoneNumberToTimeZonesMapper->getTimeZonesForNumber($this->phoneNumberProto);
     }
 }
Пример #14
0
 /**
  * @param $number
  * @dataProvider validPolishNumbers
  */
 public function testValidPolishNumbers($number)
 {
     $phoneNumber = $this->phoneUtil->parse($number, 'PL');
     $this->assertTrue($this->phoneUtil->isValidNumber($phoneNumber));
     $this->assertEquals($number, $this->phoneUtil->format($phoneNumber, PhoneNumberFormat::NATIONAL));
 }
 /**
  * Serialize a phone number.
  *
  * @param VisitorInterface $visitor     Serialization visitor.
  * @param PhoneNumber      $phoneNumber Phone number.
  * @param array            $type        Type.
  * @param mixed            $context     Context.
  *
  * @return mixed Serialized phone number.
  */
 public function serializePhoneNumber(VisitorInterface $visitor, PhoneNumber $phoneNumber, array $type, $context)
 {
     $formatted = $this->phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::E164);
     return $visitor->visitString($formatted, $type, $context);
 }
Пример #16
0
 public function testParseUSNumber()
 {
     $number = $this->phoneNumberUtil->parse('011543549480042', 'US');
     $this->assertEquals("+543549480042", $this->phoneNumberUtil->format($number, PhoneNumberFormat::E164));
 }
 public function testConvertToPHPValueWithPhoneNumber()
 {
     $phoneNumber = $this->type->convertToPHPValue('+441234567890', $this->platform);
     $this->assertInstanceOf('libphoneNumber\\PhoneNumber', $phoneNumber);
     $this->assertSame('+441234567890', $this->phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::E164));
 }
Пример #18
0
 /**
  * @return string
  */
 public function getInternational() : string
 {
     return $this->util->format($this->number, PhoneNumberFormat::INTERNATIONAL);
 }
Пример #19
0
 public function testParseUSNumber()
 {
     $number = $this->phoneNumberUtil->parse('0351-152-303-473', 'AR');
     $this->assertEquals("+5493512303473", $this->phoneNumberUtil->format($number, PhoneNumberFormat::E164));
 }