本文整理匯總了PHP中libphonenumber\PhoneNumberUtil::parse方法的典型用法代碼示例。如果您正苦於以下問題:PHP PhoneNumberUtil::parse方法的具體用法?PHP PhoneNumberUtil::parse怎麽用?PHP PhoneNumberUtil::parse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類libphonenumber\PhoneNumberUtil
的用法示例。
在下文中一共展示了PhoneNumberUtil::parse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testChineseCarrierLookup
public function testChineseCarrierLookup()
{
$number = $this->phoneUtil->parse("+86 150 3657 7264", "CN");
$carrier = PhoneNumberToCarrierMapper::getInstance();
$location = $carrier->getNameForNumber($number, "en");
$this->assertEquals("China Mobile", $location);
}
示例2: testDenormalize
public function testDenormalize()
{
$obj = $this->numberUtil->parse('+31508100210', PhoneNumberUtil::UNKNOWN_REGION);
$str = '+31508100210';
self::assertEquals($obj, $this->normalizer->denormalize($str, 'json'));
self::assertEquals($obj, $this->normalizer->denormalize($str, 'xml'));
}
示例3: testIsValidNumberForRegion
public function testIsValidNumberForRegion()
{
$number = "+33 6 76 83 51 85";
$region = "DE";
$phoneNumber = $this->phoneUtil->parse($number, $region);
$this->assertFalse($this->phoneUtil->isValidNumberForRegion($phoneNumber, "DE"));
}
示例4: testKWMobileNumber
public function testKWMobileNumber()
{
$number = "51440519";
$phoneNumber = $this->phoneUtil->parse($number, "KW");
$this->assertTrue($this->phoneUtil->isValidNumber($phoneNumber));
$this->assertEquals(PhoneNumberType::MOBILE, $this->phoneUtil->getNumberType($phoneNumber));
}
示例5: testSerializingPhoneNumber
public function testSerializingPhoneNumber()
{
$number = "+441174900000";
$region = "GB";
$phoneNumber = $this->phoneUtil->parse($number, $region);
$serializedString = serialize($phoneNumber);
$phoneObject2 = unserialize($serializedString);
$this->assertTrue($phoneObject2->equals($phoneNumber));
}
示例6: 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);
}
示例7: 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'));
}
示例8: 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;
}
示例9: testTooShortNumber
public function testTooShortNumber()
{
try {
$this->phoneUtil->parse("+441", "GB");
} catch (\Exception $e) {
$this->assertEquals("libphonenumber\\NumberParseException", get_class($e));
$this->assertEquals("The string supplied is too short to be a phone number.", $e->getMessage());
$this->assertEquals(3, $e->getCode());
$this->assertEquals("Error type: 3. The string supplied is too short to be a phone number.", (string) $e);
}
}
示例10: testLocales
/**
* @dataProvider localeList
* @param string $regionCode
* @param string $countryName
*/
public function testLocales($regionCode, $countryName)
{
if (!in_array($regionCode, $this->phoneUtil->getSupportedRegions())) {
$this->markTestSkipped("{$regionCode} is not supported");
}
$number = $this->phoneUtil->getExampleNumberForType($regionCode, PhoneNumberType::FIXED_LINE_OR_MOBILE);
$phoneNumber = $this->phoneUtil->parse($number, 'ZZ');
$this->assertContains($regionCode, CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap[$phoneNumber->getCountryCode()]);
$this->assertEquals($regionCode, $this->phoneUtil->getRegionCodeForNumber($number));
$this->assertEquals($countryName, $this->geocoder->getDescriptionForValidNumber($phoneNumber, 'en', 'ZZ'), "Checking {$number} is part of {$countryName}");
}
示例11: isValidNumber
/**
* Performs the actual validation of the phone number.
*
* @param mixed $number
* @param null $country
* @return bool
*/
protected function isValidNumber($number, $country = null)
{
try {
// Throws NumberParseException if not parsed correctly against the supplied country.
// If no country was given, tries to discover the country code from the number itself.
$phoneNumber = $this->lib->parse($number, $country);
// Lenient validation; doesn't need a country code.
if ($this->lenient) {
return $this->lib->isPossibleNumber($phoneNumber);
}
// For automatic detection, the number should have a country code.
// Check if type is allowed.
if ($phoneNumber->hasCountryCode() && (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types))) {
// Automatic detection:
if ($this->autodetect) {
// Validate if the international phone number is valid for its contained country.
return $this->lib->isValidNumber($phoneNumber);
}
// Validate number against the specified country.
return $this->lib->isValidNumberForRegion($phoneNumber, $country);
}
} catch (NumberParseException $e) {
// Proceed to default validation error.
}
return false;
}
示例12: getDescriptionForValidNumber
/**
* Returns a text description for the given phone number, in the language provided. The
* description might consist of the name of the country where the phone number is from, or the
* name of the geographical area the phone number is from if more detailed information is
* available.
*
* <p>This method assumes the validity of the number passed in has already been checked, and that
* the number is suitable for geocoding. We consider fixed-line and mobile numbers possible
* candidates for geocoding.
*
* <p>If $userRegion is set, we also consider the region of the user. If the phone number is from
* the same region as the user, only a lower-level description will be returned, if one exists.
* Otherwise, the phone number's region will be returned, with optionally some more detailed
* information.
*
* <p>For example, for a user from the region "US" (United States), we would show "Mountain View,
* CA" for a particular number, omitting the United States from the description. For a user from
* the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United
* States" or even just "United States".
*
* @param PhoneNumber $number a valid phone number for which we want to get a text description
* @param string $locale the language code for which the description should be written
* @param string $userRegion the region code for a given user. This region will be omitted from the
* description if the phone number comes from this region. It is a two-letter uppercase ISO
* country code as defined by ISO 3166-1.
* @return string a text description for the given language code for the given phone number
*/
public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null)
{
// If the user region matches the number's region, then we just show the lower-level
// description, if one exists - if no description exists, we will show the region(country) name
// for the number.
$regionCode = $this->phoneUtil->getRegionCodeForNumber($number);
if ($userRegion == null || $userRegion == $regionCode) {
$languageStr = Locale::getPrimaryLanguage($locale);
$scriptStr = "";
$regionStr = Locale::getRegion($locale);
$mobileToken = PhoneNumberUtil::getCountryMobileToken($number->getCountryCode());
$nationalNumber = $this->phoneUtil->getNationalSignificantNumber($number);
if ($mobileToken !== "" && !strncmp($nationalNumber, $mobileToken, strlen($mobileToken))) {
// In some countries, eg. Argentina, mobile numbers have a mobile token before the national
// destination code, this should be removed before geocoding.
$nationalNumber = substr($nationalNumber, strlen($mobileToken));
$region = $this->phoneUtil->getRegionCodeForCountryCode($number->getCountryCode());
try {
$copiedNumber = $this->phoneUtil->parse($nationalNumber, $region);
} catch (NumberParseException $e) {
// If this happens, just reuse what we had.
$copiedNumber = $number;
}
$areaDescription = $this->prefixFileReader->getDescriptionForNumber($copiedNumber, $languageStr, $scriptStr, $regionStr);
} else {
$areaDescription = $this->prefixFileReader->getDescriptionForNumber($number, $languageStr, $scriptStr, $regionStr);
}
return strlen($areaDescription) > 0 ? $areaDescription : $this->getCountryNameForNumber($number, $locale);
}
// Otherwise, we just show the region(country) name for now.
return $this->getRegionDisplayName($regionCode, $locale);
// TODO: Concatenate the lower-level and country-name information in an appropriate
// way for each language.
}
示例13: testInvalidNumber
public function testInvalidNumber()
{
$number = '123401234512345';
$phoneObject = $this->phoneUtil->parse($number, 'GB');
$valid = $this->phoneUtil->isValidNumber($phoneObject);
$this->assertFalse($valid, "Checking phone number is invalid");
}
示例14: parse
/**
* Parse msisdn
* @param string $number
* @return Instance
* @author Andraz <andraz.krascek@gmail.com>
*/
public function parse($number)
{
try {
$phoneNumber = $this->numberUtil->parse($number, null);
} catch (NumberParseException $e) {
$this->valid = false;
return $this;
}
if (!($this->valid = $this->numberUtil->isValidNumber($phoneNumber))) {
return $this;
}
$this->countryDiallingCode = (int) $phoneNumber->getCountryCode();
$this->countryIdentifier = $this->numberUtil->getRegionCodeForNumber($phoneNumber);
$this->mnoIdentifier = $this->carrierMapper->getNameForNumber($phoneNumber, 'en_US');
$this->subscriberNumber = $phoneNumber->getNationalNumber();
return $this;
}
示例15: parseAndFormat
/**
* 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 parseAndFormat($phoneNumber, $defaultRegion, $format = PhoneNumberFormat::INTERNATIONAL)
{
if ($phoneNumber instanceof PhoneNumber) {
return $this->format($phoneNumber, $format);
}
$phoneNumber = $this->phoneNumberUtil->parse($phoneNumber, $defaultRegion);
return $this->format($phoneNumber, $format);
}