本文整理汇总了PHP中PhoneNumber::hasRawInput方法的典型用法代码示例。如果您正苦于以下问题:PHP PhoneNumber::hasRawInput方法的具体用法?PHP PhoneNumber::hasRawInput怎么用?PHP PhoneNumber::hasRawInput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PhoneNumber
的用法示例。
在下文中一共展示了PhoneNumber::hasRawInput方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: formatInOriginalFormat
/**
* Formats a phone number using the original phone number format that the number is parsed from.
* The original format is embedded in the country_code_source field of the PhoneNumber object
* passed in. If such information is missing, the number will be formatted into the NATIONAL
* format by default. When the number contains a leading zero and this is unexpected for this
* country, or we don't have a formatting pattern for the number, the method returns the raw input
* when it is available.
*
* Note this method guarantees no digit will be inserted, removed or modified as a result of
* formatting.
*
* @param PhoneNumber $number the phone number that needs to be formatted in its original number format
* @param string $regionCallingFrom the region whose IDD needs to be prefixed if the original number
* has one
* @return string the formatted phone number in its original number format
*/
public function formatInOriginalFormat(PhoneNumber $number, $regionCallingFrom)
{
if ($number->hasRawInput() && ($this->hasUnexpectedItalianLeadingZero($number) || !$this->hasFormattingPatternForNumber($number))) {
// We check if we have the formatting pattern because without that, we might format the number
// as a group without national prefix.
return $number->getRawInput();
}
if (!$number->hasCountryCodeSource()) {
return $this->format($number, PhoneNumberFormat::NATIONAL);
}
switch ($number->getCountryCodeSource()) {
case CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN:
$formattedNumber = $this->format($number, PhoneNumberFormat::INTERNATIONAL);
break;
case CountryCodeSource::FROM_NUMBER_WITH_IDD:
$formattedNumber = $this->formatOutOfCountryCallingNumber($number, $regionCallingFrom);
break;
case CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN:
$formattedNumber = substr($this->format($number, PhoneNumberFormat::INTERNATIONAL), 1);
break;
case CountryCodeSource::FROM_DEFAULT_COUNTRY:
// Fall-through to default case.
// Fall-through to default case.
default:
$regionCode = $this->getRegionCodeForCountryCode($number->getCountryCode());
// We strip non-digits from the NDD here, and from the raw input later, so that we can
// compare them easily.
$nationalPrefix = $this->getNddPrefixForRegion($regionCode, true);
$nationalFormat = $this->format($number, PhoneNumberFormat::NATIONAL);
if ($nationalPrefix === null || mb_strlen($nationalPrefix) == 0) {
// If the region doesn't have a national prefix at all, we can safely return the national
// format without worrying about a national prefix being added.
$formattedNumber = $nationalFormat;
break;
}
// Otherwise, we check if the original number was entered with a national prefix.
if ($this->rawInputContainsNationalPrefix($number->getRawInput(), $nationalPrefix, $regionCode)) {
// If so, we can safely return the national format.
$formattedNumber = $nationalFormat;
break;
}
// Metadata cannot be null here because getNddPrefixForRegion() (above) returns null if
// there is no metadata for the region.
$metadata = $this->getMetadataForRegion($regionCode);
$nationalNumber = $this->getNationalSignificantNumber($number);
$formatRule = $this->chooseFormattingPatternForNumber($metadata->numberFormats(), $nationalNumber);
// The format rule could still be null here if the national number was 0 and there was no
// raw input (this should not be possible for numbers generated by the phonenumber library
// as they would also not have a country calling code and we would have exited earlier).
if ($formatRule === null) {
$formattedNumber = $nationalFormat;
break;
}
// When the format we apply to this number doesn't contain national prefix, we can just
// return the national format.
// TODO: Refactor the code below with the code in isNationalPrefixPresentIfRequired.
$candidateNationalPrefixRule = $formatRule->getNationalPrefixFormattingRule();
// We assume that the first-group symbol will never be _before_ the national prefix.
$indexOfFirstGroup = strpos($candidateNationalPrefixRule, '$1');
if ($indexOfFirstGroup <= 0) {
$formattedNumber = $nationalFormat;
break;
}
$candidateNationalPrefixRule = substr($candidateNationalPrefixRule, 0, $indexOfFirstGroup);
$candidateNationalPrefixRule = $this->normalizeDigitsOnly($candidateNationalPrefixRule);
if (mb_strlen($candidateNationalPrefixRule) == 0) {
// National prefix not used when formatting this number.
$formattedNumber = $nationalFormat;
break;
}
// Otherwise, we need to remove the national prefix from our output.
$numFormatCopy = new NumberFormat();
$numFormatCopy->mergeFrom($formatRule);
$numFormatCopy->clearNationalPrefixFormattingRule();
$numberFormats = array();
$numberFormats[] = $numFormatCopy;
$formattedNumber = $this->formatByPattern($number, PhoneNumberFormat::NATIONAL, $numberFormats);
break;
}
$rawInput = $number->getRawInput();
// If no digit is inserted/removed/modified as a result of our formatting, we return the
// formatted phone number; otherwise we return the raw input the user entered.
if ($formattedNumber !== null && mb_strlen($rawInput) > 0) {
$normalizedFormattedNumber = $this->normalizeDiallableCharsOnly($formattedNumber);
//.........这里部分代码省略.........
示例2: mergeFrom
/**
* Merges the information from another phone number into this phone number.
*
* @param PhoneNumber $other The phone number to copy.
*
* @return PhoneNumber This PhoneNumber instance, for chaining method calls.
*/
public function mergeFrom(PhoneNumber $other)
{
if ($other->hasCountryCode()) {
$this->setCountryCode($other->getCountryCode());
}
if ($other->hasNationalNumber()) {
$this->setNationalNumber($other->getNationalNumber());
}
if ($other->hasExtension()) {
$this->setExtension($other->getExtension());
}
if ($other->hasItalianLeadingZero()) {
$this->setItalianLeadingZero($other->isItalianLeadingZero());
}
if ($other->hasNumberOfLeadingZeros()) {
$this->setNumberOfLeadingZeros($other->getNumberOfLeadingZeros());
}
if ($other->hasRawInput()) {
$this->setRawInput($other->getRawInput());
}
if ($other->hasCountryCodeSource()) {
$this->setCountryCodeSource($other->getCountryCodeSource());
}
if ($other->hasPreferredDomesticCarrierCode()) {
$this->setPreferredDomesticCarrierCode($other->getPreferredDomesticCarrierCode());
}
return $this;
}