7 public static function format(
PhoneNumber $number, $formatType, $forceNationalPrefix =
false)
19 $format = static::selectFormatForNumber($number, $formatType, $countryMetadata);
23 $formattedNationalNumber = static::formatNationalNumber(
43 return '+' . $number->
getCountryCode() .
' ' . $formattedNationalNumber;
47 return $formattedNationalNumber;
63 $format = static::selectOriginalFormatForNumber($number);
67 $formattedNationalNumber = static::formatNationalNumberWithOriginalFormat(
79 $formattedNumber = ($number->
hasPlus() ?
'+' :
'') . $number->
getCountryCode() .
' ' . $formattedNationalNumber;
83 $formattedNumber = $formattedNationalNumber;
87 $normalizedFormattedNumber = static::stripNonNumbers($formattedNumber);
88 $normalizedRawInput = static::stripNonNumbers($number->
getRawNumber());
89 if ($normalizedFormattedNumber !== $normalizedRawInput)
94 return $formattedNumber;
101 $availableFormats = static::getAvailableFormats($countryMetadata);
103 foreach ($availableFormats as $format)
105 if($isInternational && isset($format[
'intlFormat']) && $format[
'intlFormat'] ===
'NA')
108 if(isset($format[
'leadingDigits']) && !static::matchLeadingDigits($nationalNumber, $format[
'leadingDigits']))
113 $formatPatternRegex =
'/^' . $format[
'pattern'] .
'$/';
114 if(preg_match($formatPatternRegex, $nationalNumber))
128 $availableFormats = static::getAvailableFormats($countryMetadata);
130 foreach ($availableFormats as $format)
134 if(isset($format[
'intlFormat']) && $format[
'intlFormat'] ===
'NA')
141 if($hasNationalPrefix && !static::_isNationalPrefixSupported($format, $countryMetadata))
147 if(isset($format[
'leadingDigits']) && !static::matchLeadingDigits($nationalNumber, $format[
'leadingDigits']))
152 $formatPatternRegex =
'/^' . $format[
'pattern'] .
'$/';
153 if(preg_match($formatPatternRegex, $nationalNumber))
169 if(is_array($leadingDigits))
171 foreach ($leadingDigits as $leadingDigitsSample)
173 $re =
'/' . $leadingDigitsSample .
'/A';
174 if(preg_match($re, $phoneNumber, $matches))
182 $re =
'/' . $leadingDigits .
'/A';
183 if(preg_match($re, $phoneNumber, $matches))
199 protected static function formatNationalNumber($nationalNumber, $formatType, $countryMetadata, $format, $forceNationalPrefix)
202 $replaceFormat = (isset($format[
'intlFormat']) && $isInternational) ? $format[
'intlFormat'] : $format[
'format'];
203 $patternRegex =
'/' . $format[
'pattern'] .
'/';
205 if(!$isInternational)
207 $nationalPrefixFormattingRule = static::getNationalPrefixFormattingRule($format, $countryMetadata);
208 if($nationalPrefixFormattingRule !=
'')
210 $nationalPrefixFormattingRule = str_replace(array(
'$NP',
'$FG'), array($countryMetadata[
'nationalPrefix'],
'$1'), $nationalPrefixFormattingRule);
211 $replaceFormat = preg_replace(
'/(\\$\\d)/', $nationalPrefixFormattingRule, $replaceFormat, 1);
215 $replaceFormat = $countryMetadata[
'nationalPrefix'] .
' ' . $replaceFormat;
219 return preg_replace($patternRegex, $replaceFormat, $nationalNumber);
225 $replaceFormat = (isset($format[
'intlFormat']) && $isInternational) ? $format[
'intlFormat'] : $format[
'format'];
226 $patternRegex =
'/' . $format[
'pattern'] .
'/';
229 $nationalPrefix = static::getNationalPrefix($countryMetadata,
true);
230 $hasNationalPrefix = static::numberContainsNationalPrefix($number->
getRawNumber(), $nationalPrefix, $countryMetadata);
232 if(!$isInternational && $hasNationalPrefix)
234 $nationalPrefixFormattingRule = static::getNationalPrefixFormattingRule($format, $countryMetadata);
235 if($nationalPrefixFormattingRule !=
'')
237 $nationalPrefixFormattingRule = str_replace(array(
'$NP',
'$FG'), array($nationalPrefix,
'$1'), $nationalPrefixFormattingRule);
238 $replaceFormat = preg_replace(
'/(\\$\\d)/', $nationalPrefixFormattingRule, $replaceFormat, 1);
242 $replaceFormat = $nationalPrefix .
' ' . $replaceFormat;
246 return preg_replace($patternRegex, $replaceFormat, $nationalNumber);
298 if($nationalPrefix ==
'')
303 if (mb_strpos($phoneNumber, $nationalPrefix) === 0)
309 $a = mb_substr($phoneNumber, mb_strlen($nationalPrefix));
326 if (isset($countryMetadata[
'availableFormats']) && is_array($countryMetadata[
'availableFormats']))
328 return $countryMetadata[
'availableFormats'];
331 $countryCode = $countryMetadata[
'countryCode'] ??
'';
333 $mainCountry = $countriesForCode[0] ??
'';
337 isset($mainCountryMetadata[
'availableFormats']) && is_array($mainCountryMetadata[
'availableFormats'])
338 ? $mainCountryMetadata[
'availableFormats']
354 $nationalPrefixFormattingRule = static::getNationalPrefixFormattingRule($format, $countryMetadata);
356 $result = (!$nationalPrefixFormattingRule || preg_match(
'/\$NP/', $nationalPrefixFormattingRule));