本文整理匯總了PHP中libphonenumber\PhoneNumberUtil::format方法的典型用法代碼示例。如果您正苦於以下問題:PHP PhoneNumberUtil::format方法的具體用法?PHP PhoneNumberUtil::format怎麽用?PHP PhoneNumberUtil::format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類libphonenumber\PhoneNumberUtil
的用法示例。
在下文中一共展示了PhoneNumberUtil::format方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __toString
/**
* @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: filter
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: testLongerNumber
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'));
}
示例4: render
/**
* 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;
}
示例5: format
/**
* 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);
}
示例6: testReverseTransform
/**
* @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);
}
示例7: testUnknownCountryCallingCode
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: getExampleNumber
/**
* @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: normalizePhoneNumber
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);
}
示例10: testEveryRegionHasExampleNumber
/**
* @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));
}
示例11: testChildLine
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");
}
示例12: testFormatNumberWithExtension
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));
}
示例13: parse
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: testValidPolishNumbers
/**
* @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));
}
示例15: serializePhoneNumber
/**
* 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);
}