Complex classes like PhoneNumberUtil often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PhoneNumberUtil, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class PhoneNumberUtil |
||
| 23 | { |
||
| 24 | /** Flags to use when compiling regular expressions for phone numbers */ |
||
| 25 | const REGEX_FLAGS = 'ui'; //Unicode and case insensitive |
||
| 26 | // The minimum and maximum length of the national significant number. |
||
| 27 | const MIN_LENGTH_FOR_NSN = 2; |
||
| 28 | // The ITU says the maximum length should be 15, but we have found longer numbers in Germany. |
||
| 29 | const MAX_LENGTH_FOR_NSN = 17; |
||
| 30 | |||
| 31 | // We don't allow input strings for parsing to be longer than 250 chars. This prevents malicious |
||
| 32 | // input from overflowing the regular-expression engine. |
||
| 33 | const MAX_INPUT_STRING_LENGTH = 250; |
||
| 34 | |||
| 35 | // The maximum length of the country calling code. |
||
| 36 | const MAX_LENGTH_COUNTRY_CODE = 3; |
||
| 37 | |||
| 38 | const REGION_CODE_FOR_NON_GEO_ENTITY = "001"; |
||
| 39 | const META_DATA_FILE_PREFIX = 'PhoneNumberMetadata'; |
||
| 40 | const TEST_META_DATA_FILE_PREFIX = 'PhoneNumberMetadataForTesting'; |
||
| 41 | |||
| 42 | // Region-code for the unknown region. |
||
| 43 | const UNKNOWN_REGION = "ZZ"; |
||
| 44 | |||
| 45 | const NANPA_COUNTRY_CODE = 1; |
||
| 46 | /* |
||
| 47 | * The prefix that needs to be inserted in front of a Colombian landline number when dialed from |
||
| 48 | * a mobile number in Colombia. |
||
| 49 | */ |
||
| 50 | const COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = "3"; |
||
| 51 | // The PLUS_SIGN signifies the international prefix. |
||
| 52 | const PLUS_SIGN = '+'; |
||
| 53 | const PLUS_CHARS = '++'; |
||
| 54 | const STAR_SIGN = '*'; |
||
| 55 | |||
| 56 | const RFC3966_EXTN_PREFIX = ";ext="; |
||
| 57 | const RFC3966_PREFIX = "tel:"; |
||
| 58 | const RFC3966_PHONE_CONTEXT = ";phone-context="; |
||
| 59 | const RFC3966_ISDN_SUBADDRESS = ";isub="; |
||
| 60 | |||
| 61 | // We use this pattern to check if the phone number has at least three letters in it - if so, then |
||
| 62 | // we treat it as a number where some phone-number digits are represented by letters. |
||
| 63 | const VALID_ALPHA_PHONE_PATTERN = "(?:.*?[A-Za-z]){3}.*"; |
||
| 64 | // We accept alpha characters in phone numbers, ASCII only, upper and lower case. |
||
| 65 | const VALID_ALPHA = "A-Za-z"; |
||
| 66 | |||
| 67 | |||
| 68 | // Default extension prefix to use when formatting. This will be put in front of any extension |
||
| 69 | // component of the number, after the main national number is formatted. For example, if you wish |
||
| 70 | // the default extension formatting to be " extn: 3456", then you should specify " extn: " here |
||
| 71 | // as the default extension prefix. This can be overridden by region-specific preferences. |
||
| 72 | const DEFAULT_EXTN_PREFIX = " ext. "; |
||
| 73 | |||
| 74 | // Regular expression of acceptable punctuation found in phone numbers. This excludes punctuation |
||
| 75 | // found as a leading character only. |
||
| 76 | // This consists of dash characters, white space characters, full stops, slashes, |
||
| 77 | // square brackets, parentheses and tildes. It also includes the letter 'x' as that is found as a |
||
| 78 | // placeholder for carrier information in some phone numbers. Full-width variants are also |
||
| 79 | // present. |
||
| 80 | const VALID_PUNCTUATION = "-x\xE2\x80\x90-\xE2\x80\x95\xE2\x88\x92\xE3\x83\xBC\xEF\xBC\x8D-\xEF\xBC\x8F \xC2\xA0\xC2\xAD\xE2\x80\x8B\xE2\x81\xA0\xE3\x80\x80()\xEF\xBC\x88\xEF\xBC\x89\xEF\xBC\xBB\xEF\xBC\xBD.\\[\\]/~\xE2\x81\x93\xE2\x88\xBC"; |
||
| 81 | const DIGITS = "\\p{Nd}"; |
||
| 82 | |||
| 83 | // Pattern that makes it easy to distinguish whether a region has a unique international dialing |
||
| 84 | // prefix or not. If a region has a unique international prefix (e.g. 011 in USA), it will be |
||
| 85 | // represented as a string that contains a sequence of ASCII digits. If there are multiple |
||
| 86 | // available international prefixes in a region, they will be represented as a regex string that |
||
| 87 | // always contains character(s) other than ASCII digits. |
||
| 88 | // Note this regex also includes tilde, which signals waiting for the tone. |
||
| 89 | const UNIQUE_INTERNATIONAL_PREFIX = "[\\d]+(?:[~\xE2\x81\x93\xE2\x88\xBC\xEF\xBD\x9E][\\d]+)?"; |
||
| 90 | const NON_DIGITS_PATTERN = "(\\D+)"; |
||
| 91 | |||
| 92 | // The FIRST_GROUP_PATTERN was originally set to $1 but there are some countries for which the |
||
| 93 | // first group is not used in the national pattern (e.g. Argentina) so the $1 group does not match |
||
| 94 | // correctly. Therefore, we use \d, so that the first group actually used in the pattern will be |
||
| 95 | // matched. |
||
| 96 | const FIRST_GROUP_PATTERN = "(\\$\\d)"; |
||
| 97 | const NP_PATTERN = '\\$NP'; |
||
| 98 | const FG_PATTERN = '\\$FG'; |
||
| 99 | const CC_PATTERN = '\\$CC'; |
||
| 100 | |||
| 101 | // A pattern that is used to determine if the national prefix formatting rule has the first group |
||
| 102 | // only, i.e., does not start with the national prefix. Note that the pattern explicitly allows |
||
| 103 | // for unbalanced parentheses. |
||
| 104 | const FIRST_GROUP_ONLY_PREFIX_PATTERN = '\\(?\\$1\\)?'; |
||
| 105 | public static $PLUS_CHARS_PATTERN; |
||
| 106 | protected static $SEPARATOR_PATTERN; |
||
| 107 | protected static $CAPTURING_DIGIT_PATTERN; |
||
| 108 | protected static $VALID_START_CHAR_PATTERN = null; |
||
| 109 | public static $SECOND_NUMBER_START_PATTERN = '[\\\\/] *x'; |
||
| 110 | public static $UNWANTED_END_CHAR_PATTERN = "[[\\P{N}&&\\P{L}]&&[^#]]+$"; |
||
| 111 | protected static $DIALLABLE_CHAR_MAPPINGS = array(); |
||
| 112 | protected static $CAPTURING_EXTN_DIGITS; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var PhoneNumberUtil |
||
| 116 | */ |
||
| 117 | protected static $instance = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Only upper-case variants of alpha characters are stored. |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | protected static $ALPHA_MAPPINGS = array( |
||
| 124 | 'A' => '2', |
||
| 125 | 'B' => '2', |
||
| 126 | 'C' => '2', |
||
| 127 | 'D' => '3', |
||
| 128 | 'E' => '3', |
||
| 129 | 'F' => '3', |
||
| 130 | 'G' => '4', |
||
| 131 | 'H' => '4', |
||
| 132 | 'I' => '4', |
||
| 133 | 'J' => '5', |
||
| 134 | 'K' => '5', |
||
| 135 | 'L' => '5', |
||
| 136 | 'M' => '6', |
||
| 137 | 'N' => '6', |
||
| 138 | 'O' => '6', |
||
| 139 | 'P' => '7', |
||
| 140 | 'Q' => '7', |
||
| 141 | 'R' => '7', |
||
| 142 | 'S' => '7', |
||
| 143 | 'T' => '8', |
||
| 144 | 'U' => '8', |
||
| 145 | 'V' => '8', |
||
| 146 | 'W' => '9', |
||
| 147 | 'X' => '9', |
||
| 148 | 'Y' => '9', |
||
| 149 | 'Z' => '9', |
||
| 150 | ); |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Map of country calling codes that use a mobile token before the area code. One example of when |
||
| 154 | * this is relevant is when determining the length of the national destination code, which should |
||
| 155 | * be the length of the area code plus the length of the mobile token. |
||
| 156 | * @var array |
||
| 157 | */ |
||
| 158 | protected static $MOBILE_TOKEN_MAPPINGS = array(); |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Set of country codes that have geographically assigned mobile numbers (see GEO_MOBILE_COUNTRIES |
||
| 162 | * below) which are not based on *area codes*. For example, in China mobile numbers start with a |
||
| 163 | * carrier indicator, and beyond that are geographically assigned: this carrier indicator is not |
||
| 164 | * considered to be an area code. |
||
| 165 | * |
||
| 166 | * @var array |
||
| 167 | */ |
||
| 168 | protected static $GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set of country calling codes that have geographically assigned mobile numbers. This may not be |
||
| 172 | * complete; we add calling codes case by case, as we find geographical mobile numbers or hear |
||
| 173 | * from user reports. Note that countries like the US, where we can't distinguish between |
||
| 174 | * fixed-line or mobile numbers, are not listed here, since we consider FIXED_LINE_OR_MOBILE to be |
||
| 175 | * a possibly geographically-related type anyway (like FIXED_LINE). |
||
| 176 | * |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | protected static $GEO_MOBILE_COUNTRIES; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * For performance reasons, amalgamate both into one map. |
||
| 183 | * @var array |
||
| 184 | */ |
||
| 185 | protected static $ALPHA_PHONE_MAPPINGS = null; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Separate map of all symbols that we wish to retain when formatting alpha numbers. This |
||
| 189 | * includes digits, ASCII letters and number grouping symbols such as "-" and " ". |
||
| 190 | * @var array |
||
| 191 | */ |
||
| 192 | protected static $ALL_PLUS_NUMBER_GROUPING_SYMBOLS; |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and |
||
| 196 | * ALL_PLUS_NUMBER_GROUPING_SYMBOLS. |
||
| 197 | * @var array |
||
| 198 | */ |
||
| 199 | protected static $asciiDigitMappings = array( |
||
| 200 | '0' => '0', |
||
| 201 | '1' => '1', |
||
| 202 | '2' => '2', |
||
| 203 | '3' => '3', |
||
| 204 | '4' => '4', |
||
| 205 | '5' => '5', |
||
| 206 | '6' => '6', |
||
| 207 | '7' => '7', |
||
| 208 | '8' => '8', |
||
| 209 | '9' => '9', |
||
| 210 | ); |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Regexp of all possible ways to write extensions, for use when parsing. This will be run as a |
||
| 214 | * case-insensitive regexp match. Wide character versions are also provided after each ASCII |
||
| 215 | * version. |
||
| 216 | * @var String |
||
| 217 | */ |
||
| 218 | protected static $EXTN_PATTERNS_FOR_PARSING; |
||
| 219 | /** |
||
| 220 | * @var string |
||
| 221 | * @internal |
||
| 222 | */ |
||
| 223 | public static $EXTN_PATTERNS_FOR_MATCHING; |
||
| 224 | protected static $EXTN_PATTERN = null; |
||
| 225 | protected static $VALID_PHONE_NUMBER_PATTERN = null; |
||
| 226 | protected static $MIN_LENGTH_PHONE_NUMBER_PATTERN; |
||
| 227 | /** |
||
| 228 | * Regular expression of viable phone numbers. This is location independent. Checks we have at |
||
| 229 | * least three leading digits, and only valid punctuation, alpha characters and |
||
| 230 | * digits in the phone number. Does not include extension data. |
||
| 231 | * The symbol 'x' is allowed here as valid punctuation since it is often used as a placeholder for |
||
| 232 | * carrier codes, for example in Brazilian phone numbers. We also allow multiple "+" characters at |
||
| 233 | * the start. |
||
| 234 | * Corresponds to the following: |
||
| 235 | * [digits]{minLengthNsn}| |
||
| 236 | * plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])* |
||
| 237 | * |
||
| 238 | * The first reg-ex is to allow short numbers (two digits long) to be parsed if they are entered |
||
| 239 | * as "15" etc, but only if there is no punctuation in them. The second expression restricts the |
||
| 240 | * number of digits to three or more, but then allows them to be in international form, and to |
||
| 241 | * have alpha-characters and punctuation. |
||
| 242 | * |
||
| 243 | * Note VALID_PUNCTUATION starts with a -, so must be the first in the range. |
||
| 244 | * @var string |
||
| 245 | */ |
||
| 246 | protected static $VALID_PHONE_NUMBER; |
||
| 247 | protected static $numericCharacters = array( |
||
| 248 | "\xef\xbc\x90" => 0, |
||
| 249 | "\xef\xbc\x91" => 1, |
||
| 250 | "\xef\xbc\x92" => 2, |
||
| 251 | "\xef\xbc\x93" => 3, |
||
| 252 | "\xef\xbc\x94" => 4, |
||
| 253 | "\xef\xbc\x95" => 5, |
||
| 254 | "\xef\xbc\x96" => 6, |
||
| 255 | "\xef\xbc\x97" => 7, |
||
| 256 | "\xef\xbc\x98" => 8, |
||
| 257 | "\xef\xbc\x99" => 9, |
||
| 258 | |||
| 259 | "\xd9\xa0" => 0, |
||
| 260 | "\xd9\xa1" => 1, |
||
| 261 | "\xd9\xa2" => 2, |
||
| 262 | "\xd9\xa3" => 3, |
||
| 263 | "\xd9\xa4" => 4, |
||
| 264 | "\xd9\xa5" => 5, |
||
| 265 | "\xd9\xa6" => 6, |
||
| 266 | "\xd9\xa7" => 7, |
||
| 267 | "\xd9\xa8" => 8, |
||
| 268 | "\xd9\xa9" => 9, |
||
| 269 | |||
| 270 | "\xdb\xb0" => 0, |
||
| 271 | "\xdb\xb1" => 1, |
||
| 272 | "\xdb\xb2" => 2, |
||
| 273 | "\xdb\xb3" => 3, |
||
| 274 | "\xdb\xb4" => 4, |
||
| 275 | "\xdb\xb5" => 5, |
||
| 276 | "\xdb\xb6" => 6, |
||
| 277 | "\xdb\xb7" => 7, |
||
| 278 | "\xdb\xb8" => 8, |
||
| 279 | "\xdb\xb9" => 9, |
||
| 280 | |||
| 281 | "\xe1\xa0\x90" => 0, |
||
| 282 | "\xe1\xa0\x91" => 1, |
||
| 283 | "\xe1\xa0\x92" => 2, |
||
| 284 | "\xe1\xa0\x93" => 3, |
||
| 285 | "\xe1\xa0\x94" => 4, |
||
| 286 | "\xe1\xa0\x95" => 5, |
||
| 287 | "\xe1\xa0\x96" => 6, |
||
| 288 | "\xe1\xa0\x97" => 7, |
||
| 289 | "\xe1\xa0\x98" => 8, |
||
| 290 | "\xe1\xa0\x99" => 9, |
||
| 291 | ); |
||
| 292 | |||
| 293 | /** |
||
| 294 | * The set of county calling codes that map to the non-geo entity region ("001"). |
||
| 295 | * @var array |
||
| 296 | */ |
||
| 297 | protected $countryCodesForNonGeographicalRegion = array(); |
||
| 298 | /** |
||
| 299 | * The set of regions the library supports. |
||
| 300 | * @var array |
||
| 301 | */ |
||
| 302 | protected $supportedRegions = array(); |
||
| 303 | |||
| 304 | /** |
||
| 305 | * A mapping from a country calling code to the region codes which denote the region represented |
||
| 306 | * by that country calling code. In the case of multiple regions sharing a calling code, such as |
||
| 307 | * the NANPA regions, the one indicated with "isMainCountryForCode" in the metadata should be |
||
| 308 | * first. |
||
| 309 | * @var array |
||
| 310 | */ |
||
| 311 | protected $countryCallingCodeToRegionCodeMap = array(); |
||
| 312 | /** |
||
| 313 | * The set of regions that share country calling code 1. |
||
| 314 | * @var array |
||
| 315 | */ |
||
| 316 | protected $nanpaRegions = array(); |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @var MetadataSourceInterface |
||
| 320 | */ |
||
| 321 | protected $metadataSource; |
||
| 322 | |||
| 323 | /** |
||
| 324 | * This class implements a singleton, so the only constructor is protected. |
||
| 325 | * @param MetadataSourceInterface $metadataSource |
||
| 326 | * @param $countryCallingCodeToRegionCodeMap |
||
| 327 | */ |
||
| 328 | 666 | protected function __construct(MetadataSourceInterface $metadataSource, $countryCallingCodeToRegionCodeMap) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting, |
||
| 388 | * parsing, or validation. The instance is loaded with phone number metadata for a number of most |
||
| 389 | * commonly used regions. |
||
| 390 | * |
||
| 391 | * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance |
||
| 392 | * multiple times will only result in one instance being created. |
||
| 393 | * |
||
| 394 | * @param string $baseFileLocation |
||
| 395 | * @param array|null $countryCallingCodeToRegionCodeMap |
||
| 396 | * @param MetadataLoaderInterface|null $metadataLoader |
||
| 397 | * @param MetadataSourceInterface|null $metadataSource |
||
| 398 | * @return PhoneNumberUtil instance |
||
| 399 | */ |
||
| 400 | 6041 | public static function getInstance($baseFileLocation = self::META_DATA_FILE_PREFIX, array $countryCallingCodeToRegionCodeMap = null, MetadataLoaderInterface $metadataLoader = null, MetadataSourceInterface $metadataSource = null) |
|
| 419 | |||
| 420 | 666 | protected function init() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @internal |
||
| 445 | */ |
||
| 446 | 668 | public static function initCapturingExtnDigits() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @internal |
||
| 453 | */ |
||
| 454 | 668 | public static function initExtnPatterns() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Helper initialiser method to create the regular-expression pattern to match extensions, |
||
| 469 | * allowing the one-char extension symbols provided by {@code singleExtnSymbols}. |
||
| 470 | * @param string $singleExtnSymbols |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | 668 | protected static function createExtnPattern($singleExtnSymbols) |
|
| 491 | |||
| 492 | 666 | protected static function initExtnPattern() |
|
| 496 | |||
| 497 | 668 | protected static function initValidPhoneNumberPatterns() |
|
| 505 | |||
| 506 | 668 | protected static function initAlphaPhoneMappings() |
|
| 510 | |||
| 511 | 667 | protected static function initValidStartCharPattern() |
|
| 515 | |||
| 516 | 667 | protected static function initMobileTokenMappings() |
|
| 522 | |||
| 523 | 667 | protected static function initDiallableCharMappings() |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Used for testing purposes only to reset the PhoneNumberUtil singleton to null. |
||
| 533 | */ |
||
| 534 | 672 | public static function resetInstance() |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Converts all alpha characters in a number to their respective digits on a keypad, but retains |
||
| 541 | * existing formatting. |
||
| 542 | * @param string $number |
||
| 543 | * @return string |
||
| 544 | */ |
||
| 545 | 2 | public static function convertAlphaCharactersInNumber($number) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Normalizes a string of characters representing a phone number by replacing all characters found |
||
| 556 | * in the accompanying map with the values therein, and stripping all other characters if |
||
| 557 | * removeNonMatches is true. |
||
| 558 | * |
||
| 559 | * @param string $number a string of characters representing a phone number |
||
| 560 | * @param array $normalizationReplacements a mapping of characters to what they should be replaced by in |
||
| 561 | * the normalized version of the phone number |
||
| 562 | * @param bool $removeNonMatches indicates whether characters that are not able to be replaced |
||
| 563 | * should be stripped from the number. If this is false, they will be left unchanged in the number. |
||
| 564 | * @return string the normalized string version of the phone number |
||
| 565 | */ |
||
| 566 | 15 | protected static function normalizeHelper($number, array $normalizationReplacements, $removeNonMatches) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Helper function to check if the national prefix formatting rule has the first group only, i.e., |
||
| 586 | * does not start with the national prefix. |
||
| 587 | * @param string $nationalPrefixFormattingRule |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | 41 | public static function formattingRuleHasFirstGroupOnly($nationalPrefixFormattingRule) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Returns all regions the library has metadata for. |
||
| 601 | * |
||
| 602 | * @return array An unordered array of the two-letter region codes for every geographical region the |
||
| 603 | * library supports |
||
| 604 | */ |
||
| 605 | 246 | public function getSupportedRegions() |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Returns all global network calling codes the library has metadata for. |
||
| 612 | * |
||
| 613 | * @return array An unordered array of the country calling codes for every non-geographical entity |
||
| 614 | * the library supports |
||
| 615 | */ |
||
| 616 | 1 | public function getSupportedGlobalNetworkCallingCodes() |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Returns true if there is any possible number data set for a particular PhoneNumberDesc. |
||
| 623 | * |
||
| 624 | * @param PhoneNumberDesc $desc |
||
| 625 | * @return bool |
||
| 626 | */ |
||
| 627 | 5 | protected static function descHasPossibleNumberData(PhoneNumberDesc $desc) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * Returns true if there is any data set for a particular PhoneNumberDesc. |
||
| 637 | * |
||
| 638 | * @param PhoneNumberDesc $desc |
||
| 639 | * @return bool |
||
| 640 | */ |
||
| 641 | 2 | protected static function descHasData(PhoneNumberDesc $desc) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Returns the types we have metadata for based on the PhoneMetadata object passed in |
||
| 654 | * |
||
| 655 | * @param PhoneMetadata $metadata |
||
| 656 | * @return array |
||
| 657 | */ |
||
| 658 | 2 | private function getSupportedTypesForMetadata(PhoneMetadata $metadata) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * Returns the types for a given region which the library has metadata for. Will not include |
||
| 678 | * FIXED_LINE_OR_MOBILE (if the numbers in this region could be classified as FIXED_LINE_OR_MOBILE, |
||
| 679 | * both FIXED_LINE and MOBILE would be present) and UNKNOWN. |
||
| 680 | * |
||
| 681 | * No types will be returned for invalid or unknown region codes. |
||
| 682 | * |
||
| 683 | * @param string $regionCode |
||
| 684 | * @return array |
||
| 685 | */ |
||
| 686 | 1 | public function getSupportedTypesForRegion($regionCode) |
|
| 694 | |||
| 695 | /** |
||
| 696 | * Returns the types for a country-code belonging to a non-geographical entity which the library |
||
| 697 | * has metadata for. Will not include FIXED_LINE_OR_MOBILE (if numbers for this non-geographical |
||
| 698 | * entity could be classified as FIXED_LINE_OR_MOBILE, both FIXED_LINE and MOBILE would be |
||
| 699 | * present) and UNKNOWN. |
||
| 700 | * |
||
| 701 | * @param int $countryCallingCode |
||
| 702 | * @return array |
||
| 703 | */ |
||
| 704 | 1 | public function getSupportedTypesForNonGeoEntity($countryCallingCode) |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Gets the length of the geographical area code from the {@code nationalNumber} field of the |
||
| 716 | * PhoneNumber object passed in, so that clients could use it to split a national significant |
||
| 717 | * number into geographical area code and subscriber number. It works in such a way that the |
||
| 718 | * resultant subscriber number should be diallable, at least on some devices. An example of how |
||
| 719 | * this could be used: |
||
| 720 | * |
||
| 721 | * <code> |
||
| 722 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 723 | * $number = $phoneUtil->parse("16502530000", "US"); |
||
| 724 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 725 | * |
||
| 726 | * $areaCodeLength = $phoneUtil->getLengthOfGeographicalAreaCode($number); |
||
| 727 | * if ($areaCodeLength > 0) |
||
| 728 | * { |
||
| 729 | * $areaCode = substr($nationalSignificantNumber, 0,$areaCodeLength); |
||
| 730 | * $subscriberNumber = substr($nationalSignificantNumber, $areaCodeLength); |
||
| 731 | * } else { |
||
| 732 | * $areaCode = ""; |
||
| 733 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 734 | * } |
||
| 735 | * </code> |
||
| 736 | * |
||
| 737 | * N.B.: area code is a very ambiguous concept, so the I18N team generally recommends against |
||
| 738 | * using it for most purposes, but recommends using the more general {@code nationalNumber} |
||
| 739 | * instead. Read the following carefully before deciding to use this method: |
||
| 740 | * <ul> |
||
| 741 | * <li> geographical area codes change over time, and this method honors those changes; |
||
| 742 | * therefore, it doesn't guarantee the stability of the result it produces. |
||
| 743 | * <li> subscriber numbers may not be diallable from all devices (notably mobile devices, which |
||
| 744 | * typically requires the full national_number to be dialled in most regions). |
||
| 745 | * <li> most non-geographical numbers have no area codes, including numbers from non-geographical |
||
| 746 | * entities |
||
| 747 | * <li> some geographical numbers have no area codes. |
||
| 748 | * </ul> |
||
| 749 | * @param PhoneNumber $number PhoneNumber object for which clients want to know the length of the area code. |
||
| 750 | * @return int the length of area code of the PhoneNumber object passed in. |
||
| 751 | */ |
||
| 752 | 1 | public function getLengthOfGeographicalAreaCode(PhoneNumber $number) |
|
| 782 | |||
| 783 | /** |
||
| 784 | * Returns the metadata for the given region code or {@code null} if the region code is invalid |
||
| 785 | * or unknown. |
||
| 786 | * @param string $regionCode |
||
| 787 | * @return PhoneMetadata |
||
| 788 | */ |
||
| 789 | 4904 | public function getMetadataForRegion($regionCode) |
|
| 797 | |||
| 798 | /** |
||
| 799 | * Helper function to check region code is not unknown or null. |
||
| 800 | * @param string $regionCode |
||
| 801 | * @return bool |
||
| 802 | */ |
||
| 803 | 4905 | protected function isValidRegionCode($regionCode) |
|
| 807 | |||
| 808 | /** |
||
| 809 | * Returns the region where a phone number is from. This could be used for geocoding at the region |
||
| 810 | * level. Only guarantees correct results for valid, full numbers (not short-codes, or invalid |
||
| 811 | * numbers). |
||
| 812 | * |
||
| 813 | * @param PhoneNumber $number the phone number whose origin we want to know |
||
| 814 | * @return null|string the region where the phone number is from, or null if no region matches this calling |
||
| 815 | * code |
||
| 816 | */ |
||
| 817 | 2287 | public function getRegionCodeForNumber(PhoneNumber $number) |
|
| 830 | |||
| 831 | /** |
||
| 832 | * @param PhoneNumber $number |
||
| 833 | * @param array $regionCodes |
||
| 834 | * @return null|string |
||
| 835 | */ |
||
| 836 | 596 | protected function getRegionCodeForNumberFromRegionList(PhoneNumber $number, array $regionCodes) |
|
| 859 | |||
| 860 | /** |
||
| 861 | * Gets the national significant number of the a phone number. Note a national significant number |
||
| 862 | * doesn't contain a national prefix or any formatting. |
||
| 863 | * |
||
| 864 | * @param PhoneNumber $number the phone number for which the national significant number is needed |
||
| 865 | * @return string the national significant number of the PhoneNumber object passed in |
||
| 866 | */ |
||
| 867 | 2186 | public function getNationalSignificantNumber(PhoneNumber $number) |
|
| 878 | |||
| 879 | /** |
||
| 880 | * @param string $nationalNumber |
||
| 881 | * @param PhoneMetadata $metadata |
||
| 882 | * @return int PhoneNumberType constant |
||
| 883 | */ |
||
| 884 | 2068 | protected function getNumberTypeHelper($nationalNumber, PhoneMetadata $metadata) |
|
| 933 | |||
| 934 | /** |
||
| 935 | * @param string $nationalNumber |
||
| 936 | * @param PhoneNumberDesc $numberDesc |
||
| 937 | * @return bool |
||
| 938 | */ |
||
| 939 | 2095 | public function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) |
|
| 954 | |||
| 955 | /** |
||
| 956 | * isNumberGeographical(PhoneNumber) |
||
| 957 | * |
||
| 958 | * Tests whether a phone number has a geographical association. It checks if the number is |
||
| 959 | * associated to a certain region in the country where it belongs to. Note that this doesn't |
||
| 960 | * verify if the number is actually in use. |
||
| 961 | * |
||
| 962 | * isNumberGeographical(PhoneNumberType, $countryCallingCode) |
||
| 963 | * |
||
| 964 | * Tests whether a phone number has a geographical association, as represented by its type and the |
||
| 965 | * country it belongs to. |
||
| 966 | * |
||
| 967 | * This version exists since calculating the phone number type is expensive; if we have already |
||
| 968 | * done this, we don't want to do it again. |
||
| 969 | * |
||
| 970 | * @param PhoneNumber|int $phoneNumberObjOrType A PhoneNumber object, or a PhoneNumberType integer |
||
| 971 | * @param int|null $countryCallingCode Used when passing a PhoneNumberType |
||
| 972 | * @return bool |
||
| 973 | */ |
||
| 974 | 21 | public function isNumberGeographical($phoneNumberObjOrType, $countryCallingCode = null) |
|
| 985 | |||
| 986 | /** |
||
| 987 | * Gets the type of a phone number. |
||
| 988 | * @param PhoneNumber $number the number the phone number that we want to know the type |
||
| 989 | * @return int PhoneNumberType the type of the phone number |
||
| 990 | */ |
||
| 991 | 1368 | public function getNumberType(PhoneNumber $number) |
|
| 1001 | |||
| 1002 | /** |
||
| 1003 | * @param int $countryCallingCode |
||
| 1004 | * @param string $regionCode |
||
| 1005 | * @return PhoneMetadata |
||
| 1006 | */ |
||
| 1007 | 2107 | protected function getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode) |
|
| 1012 | |||
| 1013 | /** |
||
| 1014 | * @param int $countryCallingCode |
||
| 1015 | * @return PhoneMetadata |
||
| 1016 | */ |
||
| 1017 | 35 | public function getMetadataForNonGeographicalRegion($countryCallingCode) |
|
| 1024 | |||
| 1025 | /** |
||
| 1026 | * Gets the length of the national destination code (NDC) from the PhoneNumber object passed in, |
||
| 1027 | * so that clients could use it to split a national significant number into NDC and subscriber |
||
| 1028 | * number. The NDC of a phone number is normally the first group of digit(s) right after the |
||
| 1029 | * country calling code when the number is formatted in the international format, if there is a |
||
| 1030 | * subscriber number part that follows. An example of how this could be used: |
||
| 1031 | * |
||
| 1032 | * <code> |
||
| 1033 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 1034 | * $number = $phoneUtil->parse("18002530000", "US"); |
||
| 1035 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 1036 | * |
||
| 1037 | * $nationalDestinationCodeLength = $phoneUtil->getLengthOfNationalDestinationCode($number); |
||
| 1038 | * if ($nationalDestinationCodeLength > 0) { |
||
| 1039 | * $nationalDestinationCode = substr($nationalSignificantNumber, 0, $nationalDestinationCodeLength); |
||
| 1040 | * $subscriberNumber = substr($nationalSignificantNumber, $nationalDestinationCodeLength); |
||
| 1041 | * } else { |
||
| 1042 | * $nationalDestinationCode = ""; |
||
| 1043 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 1044 | * } |
||
| 1045 | * </code> |
||
| 1046 | * |
||
| 1047 | * Refer to the unit tests to see the difference between this function and |
||
| 1048 | * {@link #getLengthOfGeographicalAreaCode}. |
||
| 1049 | * |
||
| 1050 | * @param PhoneNumber $number the PhoneNumber object for which clients want to know the length of the NDC. |
||
| 1051 | * @return int the length of NDC of the PhoneNumber object passed in. |
||
| 1052 | */ |
||
| 1053 | 2 | public function getLengthOfNationalDestinationCode(PhoneNumber $number) |
|
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Formats a phone number in the specified format using default rules. Note that this does not |
||
| 1093 | * promise to produce a phone number that the user can dial from where they are - although we do |
||
| 1094 | * format in either 'national' or 'international' format depending on what the client asks for, we |
||
| 1095 | * do not currently support a more abbreviated format, such as for users in the same "area" who |
||
| 1096 | * could potentially dial the number without area code. Note that if the phone number has a |
||
| 1097 | * country calling code of 0 or an otherwise invalid country calling code, we cannot work out |
||
| 1098 | * which formatting rules to apply so we return the national significant number with no formatting |
||
| 1099 | * applied. |
||
| 1100 | * |
||
| 1101 | * @param PhoneNumber $number the phone number to be formatted |
||
| 1102 | * @param int $numberFormat the PhoneNumberFormat the phone number should be formatted into |
||
| 1103 | * @return string the formatted phone number |
||
| 1104 | */ |
||
| 1105 | 342 | public function format(PhoneNumber $number, $numberFormat) |
|
| 1148 | |||
| 1149 | /** |
||
| 1150 | * A helper function that is used by format and formatByPattern. |
||
| 1151 | * @param int $countryCallingCode |
||
| 1152 | * @param int $numberFormat PhoneNumberFormat |
||
| 1153 | * @param string $formattedNumber |
||
| 1154 | */ |
||
| 1155 | 343 | protected function prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, &$formattedNumber) |
|
| 1172 | |||
| 1173 | /** |
||
| 1174 | * Helper function to check the country calling code is valid. |
||
| 1175 | * @param int $countryCallingCode |
||
| 1176 | * @return bool |
||
| 1177 | */ |
||
| 1178 | 164 | protected function hasValidCountryCallingCode($countryCallingCode) |
|
| 1182 | |||
| 1183 | /** |
||
| 1184 | * Returns the region code that matches the specific country calling code. In the case of no |
||
| 1185 | * region code being found, ZZ will be returned. In the case of multiple regions, the one |
||
| 1186 | * designated in the metadata as the "main" region for this calling code will be returned. If the |
||
| 1187 | * countryCallingCode entered is valid but doesn't match a specific region (such as in the case of |
||
| 1188 | * non-geographical calling codes like 800) the value "001" will be returned (corresponding to |
||
| 1189 | * the value for World in the UN M.49 schema). |
||
| 1190 | * |
||
| 1191 | * @param int $countryCallingCode |
||
| 1192 | * @return string |
||
| 1193 | */ |
||
| 1194 | 551 | public function getRegionCodeForCountryCode($countryCallingCode) |
|
| 1199 | |||
| 1200 | /** |
||
| 1201 | * Note in some regions, the national number can be written in two completely different ways |
||
| 1202 | * depending on whether it forms part of the NATIONAL format or INTERNATIONAL format. The |
||
| 1203 | * numberFormat parameter here is used to specify which format to use for those cases. If a |
||
| 1204 | * carrierCode is specified, this will be inserted into the formatted string to replace $CC. |
||
| 1205 | * @param string $number |
||
| 1206 | * @param PhoneMetadata $metadata |
||
| 1207 | * @param int $numberFormat PhoneNumberFormat |
||
| 1208 | * @param null|string $carrierCode |
||
| 1209 | * @return string |
||
| 1210 | */ |
||
| 1211 | 95 | protected function formatNsn($number, PhoneMetadata $metadata, $numberFormat, $carrierCode = null) |
|
| 1224 | |||
| 1225 | /** |
||
| 1226 | * @param NumberFormat[] $availableFormats |
||
| 1227 | * @param string $nationalNumber |
||
| 1228 | * @return NumberFormat|null |
||
| 1229 | */ |
||
| 1230 | 128 | public function chooseFormattingPatternForNumber(array $availableFormats, $nationalNumber) |
|
| 1251 | |||
| 1252 | /** |
||
| 1253 | * Note that carrierCode is optional - if null or an empty string, no carrier code replacement |
||
| 1254 | * will take place. |
||
| 1255 | * @param string $nationalNumber |
||
| 1256 | * @param NumberFormat $formattingPattern |
||
| 1257 | * @param int $numberFormat PhoneNumberFormat |
||
| 1258 | * @param null|string $carrierCode |
||
| 1259 | * @return string |
||
| 1260 | */ |
||
| 1261 | 95 | public function formatNsnUsingPattern( |
|
| 1308 | |||
| 1309 | /** |
||
| 1310 | * Appends the formatted extension of a phone number to formattedNumber, if the phone number had |
||
| 1311 | * an extension specified. |
||
| 1312 | * |
||
| 1313 | * @param PhoneNumber $number |
||
| 1314 | * @param PhoneMetadata|null $metadata |
||
| 1315 | * @param int $numberFormat PhoneNumberFormat |
||
| 1316 | * @param string $formattedNumber |
||
| 1317 | */ |
||
| 1318 | 96 | protected function maybeAppendFormattedExtension(PhoneNumber $number, $metadata, $numberFormat, &$formattedNumber) |
|
| 1332 | |||
| 1333 | /** |
||
| 1334 | * Returns the mobile token for the provided country calling code if it has one, otherwise |
||
| 1335 | * returns an empty string. A mobile token is a number inserted before the area code when dialing |
||
| 1336 | * a mobile number from that country from abroad. |
||
| 1337 | * |
||
| 1338 | * @param int $countryCallingCode the country calling code for which we want the mobile token |
||
| 1339 | * @return string the mobile token, as a string, for the given country calling code |
||
| 1340 | */ |
||
| 1341 | 16 | public static function getCountryMobileToken($countryCallingCode) |
|
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT. A valid vanity |
||
| 1355 | * number will start with at least 3 digits and will have three or more alpha characters. This |
||
| 1356 | * does not do region-specific checks - to work out if this number is actually valid for a region, |
||
| 1357 | * it should be parsed and methods such as {@link #isPossibleNumberWithReason} and |
||
| 1358 | * {@link #isValidNumber} should be used. |
||
| 1359 | * |
||
| 1360 | * @param string $number the number that needs to be checked |
||
| 1361 | * @return bool true if the number is a valid vanity number |
||
| 1362 | */ |
||
| 1363 | 1 | public function isAlphaNumber($number) |
|
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Checks to see if the string of characters could possibly be a phone number at all. At the |
||
| 1375 | * moment, checks to see that the string begins with at least 2 digits, ignoring any punctuation |
||
| 1376 | * commonly found in phone numbers. |
||
| 1377 | * This method does not require the number to be normalized in advance - but does assume that |
||
| 1378 | * leading non-number symbols have been removed, such as by the method extractPossibleNumber. |
||
| 1379 | * |
||
| 1380 | * @param string $number to be checked for viability as a phone number |
||
| 1381 | * @return boolean true if the number could be a phone number of some sort, otherwise false |
||
| 1382 | */ |
||
| 1383 | 2975 | public static function isViablePhoneNumber($number) |
|
| 1398 | |||
| 1399 | /** |
||
| 1400 | * We append optionally the extension pattern to the end here, as a valid phone number may |
||
| 1401 | * have an extension prefix appended, followed by 1 or more digits. |
||
| 1402 | * @return string |
||
| 1403 | */ |
||
| 1404 | 2974 | protected static function getValidPhoneNumberPattern() |
|
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Strips any extension (as in, the part of the number dialled after the call is connected, |
||
| 1411 | * usually indicated with extn, ext, x or similar) from the end of the number, and returns it. |
||
| 1412 | * |
||
| 1413 | * @param string $number the non-normalized telephone number that we wish to strip the extension from |
||
| 1414 | * @return string the phone extension |
||
| 1415 | */ |
||
| 1416 | 2969 | protected function maybeStripExtension(&$number) |
|
| 1437 | |||
| 1438 | /** |
||
| 1439 | * Parses a string and returns it in proto buffer format. This method differs from {@link #parse} |
||
| 1440 | * in that it always populates the raw_input field of the protocol buffer with numberToParse as |
||
| 1441 | * well as the country_code_source field. |
||
| 1442 | * |
||
| 1443 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 1444 | * such as +, ( and -, as well as a phone number extension. It can also |
||
| 1445 | * be provided in RFC3966 format. |
||
| 1446 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 1447 | * if the number being parsed is not written in international format. |
||
| 1448 | * The country calling code for the number in this case would be stored |
||
| 1449 | * as that of the default region supplied. |
||
| 1450 | * @param PhoneNumber $phoneNumber |
||
| 1451 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 1452 | */ |
||
| 1453 | 180 | public function parseAndKeepRawInput($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null) |
|
| 1461 | |||
| 1462 | /** |
||
| 1463 | * Returns an iterable over all PhoneNumberMatches in $text |
||
| 1464 | * |
||
| 1465 | * @param string $text |
||
| 1466 | * @param string $defaultRegion |
||
| 1467 | * @param AbstractLeniency $leniency Defaults to Leniency::VALID() |
||
| 1468 | * @param int $maxTries Defaults to PHP_INT_MAX |
||
| 1469 | * @return PhoneNumberMatcher |
||
| 1470 | */ |
||
| 1471 | 205 | public function findNumbers($text, $defaultRegion, AbstractLeniency $leniency = null, $maxTries = PHP_INT_MAX) |
|
| 1479 | |||
| 1480 | /** |
||
| 1481 | * Gets an AsYouTypeFormatter for the specific region. |
||
| 1482 | * |
||
| 1483 | * @param string $regionCode The region where the phone number is being entered. |
||
| 1484 | * @return AsYouTypeFormatter |
||
| 1485 | */ |
||
| 1486 | 33 | public function getAsYouTypeFormatter($regionCode) |
|
| 1490 | |||
| 1491 | /** |
||
| 1492 | * A helper function to set the values related to leading zeros in a PhoneNumber. |
||
| 1493 | * @param string $nationalNumber |
||
| 1494 | * @param PhoneNumber $phoneNumber |
||
| 1495 | */ |
||
| 1496 | 2966 | public static function setItalianLeadingZerosForPhoneNumber($nationalNumber, PhoneNumber $phoneNumber) |
|
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Parses a string and fills up the phoneNumber. This method is the same as the public |
||
| 1516 | * parse() method, with the exception that it allows the default region to be null, for use by |
||
| 1517 | * isNumberMatch(). checkRegion should be set to false if it is permitted for the default region |
||
| 1518 | * to be null or unknown ("ZZ"). |
||
| 1519 | * @param string $numberToParse |
||
| 1520 | * @param string $defaultRegion |
||
| 1521 | * @param bool $keepRawInput |
||
| 1522 | * @param bool $checkRegion |
||
| 1523 | * @param PhoneNumber $phoneNumber |
||
| 1524 | * @throws NumberParseException |
||
| 1525 | */ |
||
| 1526 | 2972 | protected function parseHelper($numberToParse, $defaultRegion, $keepRawInput, $checkRegion, PhoneNumber $phoneNumber) |
|
| 1675 | |||
| 1676 | /** |
||
| 1677 | * Returns a new phone number containing only the fields needed to uniquely identify a phone |
||
| 1678 | * number, rather than any fields that capture the context in which the phone number was created. |
||
| 1679 | * These fields correspond to those set in parse() rather than parseAndKeepRawInput() |
||
| 1680 | * |
||
| 1681 | * @param PhoneNumber $phoneNumberIn |
||
| 1682 | * @return PhoneNumber |
||
| 1683 | */ |
||
| 1684 | 8 | protected static function copyCoreFieldsOnly(PhoneNumber $phoneNumberIn) |
|
| 1699 | |||
| 1700 | /** |
||
| 1701 | * Converts numberToParse to a form that we can parse and write it to nationalNumber if it is |
||
| 1702 | * written in RFC3966; otherwise extract a possible number out of it and write to nationalNumber. |
||
| 1703 | * @param string $numberToParse |
||
| 1704 | * @param string $nationalNumber |
||
| 1705 | */ |
||
| 1706 | 2970 | protected function buildNationalNumberForParsing($numberToParse, &$nationalNumber) |
|
| 1750 | |||
| 1751 | /** |
||
| 1752 | * Attempts to extract a possible number from the string passed in. This currently strips all |
||
| 1753 | * leading characters that cannot be used to start a phone number. Characters that can be used to |
||
| 1754 | * start a phone number are defined in the VALID_START_CHAR_PATTERN. If none of these characters |
||
| 1755 | * are found in the number passed in, an empty string is returned. This function also attempts to |
||
| 1756 | * strip off any alternative extensions or endings if two or more are present, such as in the case |
||
| 1757 | * of: (530) 583-6985 x302/x2303. The second extension here makes this actually two phone numbers, |
||
| 1758 | * (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second extension so that the first |
||
| 1759 | * number is parsed correctly. |
||
| 1760 | * |
||
| 1761 | * @param int $number the string that might contain a phone number |
||
| 1762 | * @return string the number, stripped of any non-phone-number prefix (such as "Tel:") or an empty |
||
| 1763 | * string if no character used to start phone numbers (such as + or any digit) is |
||
| 1764 | * found in the number |
||
| 1765 | */ |
||
| 1766 | 2993 | public static function extractPossibleNumber($number) |
|
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Checks to see that the region code used is valid, or if it is not valid, that the number to |
||
| 1796 | * parse starts with a + symbol so that we can attempt to infer the region from the number. |
||
| 1797 | * Returns false if it cannot use the region provided and the region cannot be inferred. |
||
| 1798 | * @param string $numberToParse |
||
| 1799 | * @param string $defaultRegion |
||
| 1800 | * @return bool |
||
| 1801 | */ |
||
| 1802 | 2969 | protected function checkRegionForParsing($numberToParse, $defaultRegion) |
|
| 1813 | |||
| 1814 | /** |
||
| 1815 | * Tries to extract a country calling code from a number. This method will return zero if no |
||
| 1816 | * country calling code is considered to be present. Country calling codes are extracted in the |
||
| 1817 | * following ways: |
||
| 1818 | * <ul> |
||
| 1819 | * <li> by stripping the international dialing prefix of the region the person is dialing from, |
||
| 1820 | * if this is present in the number, and looking at the next digits |
||
| 1821 | * <li> by stripping the '+' sign if present and then looking at the next digits |
||
| 1822 | * <li> by comparing the start of the number and the country calling code of the default region. |
||
| 1823 | * If the number is not considered possible for the numbering plan of the default region |
||
| 1824 | * initially, but starts with the country calling code of this region, validation will be |
||
| 1825 | * reattempted after stripping this country calling code. If this number is considered a |
||
| 1826 | * possible number, then the first digits will be considered the country calling code and |
||
| 1827 | * removed as such. |
||
| 1828 | * </ul> |
||
| 1829 | * It will throw a NumberParseException if the number starts with a '+' but the country calling |
||
| 1830 | * code supplied after this does not match that of any known region. |
||
| 1831 | * |
||
| 1832 | * @param string $number non-normalized telephone number that we wish to extract a country calling |
||
| 1833 | * code from - may begin with '+' |
||
| 1834 | * @param PhoneMetadata $defaultRegionMetadata metadata about the region this number may be from |
||
| 1835 | * @param string $nationalNumber a string buffer to store the national significant number in, in the case |
||
| 1836 | * that a country calling code was extracted. The number is appended to any existing contents. |
||
| 1837 | * If no country calling code was extracted, this will be left unchanged. |
||
| 1838 | * @param bool $keepRawInput true if the country_code_source and preferred_carrier_code fields of |
||
| 1839 | * phoneNumber should be populated. |
||
| 1840 | * @param PhoneNumber $phoneNumber the PhoneNumber object where the country_code and country_code_source need |
||
| 1841 | * to be populated. Note the country_code is always populated, whereas country_code_source is |
||
| 1842 | * only populated when keepCountryCodeSource is true. |
||
| 1843 | * @return int the country calling code extracted or 0 if none could be extracted |
||
| 1844 | * @throws NumberParseException |
||
| 1845 | */ |
||
| 1846 | 2969 | public function maybeExtractCountryCode( |
|
| 1927 | |||
| 1928 | /** |
||
| 1929 | * Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes |
||
| 1930 | * the resulting number, and indicates if an international prefix was present. |
||
| 1931 | * |
||
| 1932 | * @param string $number the non-normalized telephone number that we wish to strip any international |
||
| 1933 | * dialing prefix from. |
||
| 1934 | * @param string $possibleIddPrefix string the international direct dialing prefix from the region we |
||
| 1935 | * think this number may be dialed in |
||
| 1936 | * @return int the corresponding CountryCodeSource if an international dialing prefix could be |
||
| 1937 | * removed from the number, otherwise CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did |
||
| 1938 | * not seem to be in international format. |
||
| 1939 | */ |
||
| 1940 | 2970 | public function maybeStripInternationalPrefixAndNormalize(&$number, $possibleIddPrefix) |
|
| 1961 | |||
| 1962 | /** |
||
| 1963 | * Normalizes a string of characters representing a phone number. This performs |
||
| 1964 | * the following conversions: |
||
| 1965 | * Punctuation is stripped. |
||
| 1966 | * For ALPHA/VANITY numbers: |
||
| 1967 | * Letters are converted to their numeric representation on a telephone |
||
| 1968 | * keypad. The keypad used here is the one defined in ITU Recommendation |
||
| 1969 | * E.161. This is only done if there are 3 or more letters in the number, |
||
| 1970 | * to lessen the risk that such letters are typos. |
||
| 1971 | * For other numbers: |
||
| 1972 | * Wide-ascii digits are converted to normal ASCII (European) digits. |
||
| 1973 | * Arabic-Indic numerals are converted to European numerals. |
||
| 1974 | * Spurious alpha characters are stripped. |
||
| 1975 | * |
||
| 1976 | * @param string $number a string of characters representing a phone number. |
||
| 1977 | * @return string the normalized string version of the phone number. |
||
| 1978 | */ |
||
| 1979 | 2974 | public static function normalize(&$number) |
|
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Normalizes a string of characters representing a phone number. This converts wide-ascii and |
||
| 1995 | * arabic-indic numerals to European numerals, and strips punctuation and alpha characters. |
||
| 1996 | * |
||
| 1997 | * @param $number string a string of characters representing a phone number |
||
| 1998 | * @return string the normalized string version of the phone number |
||
| 1999 | */ |
||
| 2000 | 2993 | public static function normalizeDigitsOnly($number) |
|
| 2004 | |||
| 2005 | /** |
||
| 2006 | * @param string $number |
||
| 2007 | * @param bool $keepNonDigits |
||
| 2008 | * @return string |
||
| 2009 | */ |
||
| 2010 | 3026 | public static function normalizeDigits($number, $keepNonDigits) |
|
| 2026 | |||
| 2027 | /** |
||
| 2028 | * Strips the IDD from the start of the number if present. Helper function used by |
||
| 2029 | * maybeStripInternationalPrefixAndNormalize. |
||
| 2030 | * @param string $iddPattern |
||
| 2031 | * @param string $number |
||
| 2032 | * @return bool |
||
| 2033 | */ |
||
| 2034 | 2913 | protected function parsePrefixAsIdd($iddPattern, &$number) |
|
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Extracts country calling code from fullNumber, returns it and places the remaining number in nationalNumber. |
||
| 2056 | * It assumes that the leading plus sign or IDD has already been removed. |
||
| 2057 | * Returns 0 if fullNumber doesn't start with a valid country calling code, and leaves nationalNumber unmodified. |
||
| 2058 | * @param string $fullNumber |
||
| 2059 | * @param string $nationalNumber |
||
| 2060 | * @return int |
||
| 2061 | * @internal |
||
| 2062 | */ |
||
| 2063 | 352 | public function extractCountryCode($fullNumber, &$nationalNumber) |
|
| 2079 | |||
| 2080 | /** |
||
| 2081 | * Strips any national prefix (such as 0, 1) present in the number provided. |
||
| 2082 | * |
||
| 2083 | * @param string $number the normalized telephone number that we wish to strip any national |
||
| 2084 | * dialing prefix from |
||
| 2085 | * @param PhoneMetadata $metadata the metadata for the region that we think this number is from |
||
| 2086 | * @param string $carrierCode a place to insert the carrier code if one is extracted |
||
| 2087 | * @return bool true if a national prefix or carrier code (or both) could be extracted. |
||
| 2088 | */ |
||
| 2089 | 2969 | public function maybeStripNationalPrefixAndCarrierCode(&$number, PhoneMetadata $metadata, &$carrierCode) |
|
| 2148 | |||
| 2149 | /** |
||
| 2150 | * Convenience wrapper around isPossibleNumberForTypeWithReason. Instead of returning the reason |
||
| 2151 | * reason for failure, this method returns true if the number is either a possible fully-qualified |
||
| 2152 | * number (containing the area code and country code), or if the number could be a possible local |
||
| 2153 | * number (with a country code, but missing an area code). Local numbers are considered possible |
||
| 2154 | * if they could be possibly dialled in this format: if the area code is needed for a call to |
||
| 2155 | * connect, the number is not considered possible without it. |
||
| 2156 | * |
||
| 2157 | * @param PhoneNumber $number The number that needs to be checked |
||
| 2158 | * @param int $type PhoneNumberType The type we are interested in |
||
| 2159 | * @return bool true if the number is possible for this particular type |
||
| 2160 | */ |
||
| 2161 | 4 | public function isPossibleNumberForType(PhoneNumber $number, $type) |
|
| 2167 | |||
| 2168 | /** |
||
| 2169 | * Helper method to check a number against possible lengths for this number type, and determine |
||
| 2170 | * whether it matches, or is too short or too long. Currently, if a number pattern suggests that |
||
| 2171 | * numbers of length 7 and 10 are possible, and a number in between these possible lengths is |
||
| 2172 | * entered, such as of length 8, this will return TOO_LONG. |
||
| 2173 | * |
||
| 2174 | * @param string $number |
||
| 2175 | * @param PhoneMetadata $metadata |
||
| 2176 | * @param int $type PhoneNumberType |
||
| 2177 | * @return int ValidationResult |
||
| 2178 | */ |
||
| 2179 | 2980 | protected function testNumberLength($number, PhoneMetadata $metadata, $type = PhoneNumberType::UNKNOWN) |
|
| 2252 | |||
| 2253 | /** |
||
| 2254 | * Returns a list with the region codes that match the specific country calling code. For |
||
| 2255 | * non-geographical country calling codes, the region code 001 is returned. Also, in the case |
||
| 2256 | * of no region code being found, an empty list is returned. |
||
| 2257 | * @param int $countryCallingCode |
||
| 2258 | * @return array |
||
| 2259 | */ |
||
| 2260 | 10 | public function getRegionCodesForCountryCode($countryCallingCode) |
|
| 2265 | |||
| 2266 | /** |
||
| 2267 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 2268 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 2269 | * |
||
| 2270 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 2271 | * @return int the country calling code for the region denoted by regionCode |
||
| 2272 | */ |
||
| 2273 | 37 | public function getCountryCodeForRegion($regionCode) |
|
| 2280 | |||
| 2281 | /** |
||
| 2282 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 2283 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 2284 | * |
||
| 2285 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 2286 | * @return int the country calling code for the region denoted by regionCode |
||
| 2287 | * @throws \InvalidArgumentException if the region is invalid |
||
| 2288 | */ |
||
| 2289 | 1955 | protected function getCountryCodeForValidRegion($regionCode) |
|
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Returns a number formatted in such a way that it can be dialed from a mobile phone in a |
||
| 2300 | * specific region. If the number cannot be reached from the region (e.g. some countries block |
||
| 2301 | * toll-free numbers from being called outside of the country), the method returns an empty |
||
| 2302 | * string. |
||
| 2303 | * |
||
| 2304 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2305 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2306 | * @param boolean $withFormatting whether the number should be returned with formatting symbols, such as |
||
| 2307 | * spaces and dashes. |
||
| 2308 | * @return string the formatted phone number |
||
| 2309 | */ |
||
| 2310 | 1 | public function formatNumberForMobileDialing(PhoneNumber $number, $regionCallingFrom, $withFormatting) |
|
| 2396 | |||
| 2397 | /** |
||
| 2398 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2399 | * {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the |
||
| 2400 | * phone number already has a preferred domestic carrier code stored. If {@code carrierCode} |
||
| 2401 | * contains an empty string, returns the number in national format without any carrier code. |
||
| 2402 | * |
||
| 2403 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2404 | * @param string $carrierCode the carrier selection code to be used |
||
| 2405 | * @return string the formatted phone number in national format for dialing using the carrier as |
||
| 2406 | * specified in the {@code carrierCode} |
||
| 2407 | */ |
||
| 2408 | 2 | public function formatNationalNumberWithCarrierCode(PhoneNumber $number, $carrierCode) |
|
| 2437 | |||
| 2438 | /** |
||
| 2439 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2440 | * preferredDomesticCarrierCode field of the PhoneNumber object passed in. If that is missing, |
||
| 2441 | * use the {@code fallbackCarrierCode} passed in instead. If there is no |
||
| 2442 | * {@code preferredDomesticCarrierCode}, and the {@code fallbackCarrierCode} contains an empty |
||
| 2443 | * string, return the number in national format without any carrier code. |
||
| 2444 | * |
||
| 2445 | * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in |
||
| 2446 | * should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting. |
||
| 2447 | * |
||
| 2448 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2449 | * @param string $fallbackCarrierCode the carrier selection code to be used, if none is found in the |
||
| 2450 | * phone number itself |
||
| 2451 | * @return string the formatted phone number in national format for dialing using the number's |
||
| 2452 | * {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if |
||
| 2453 | * none is found |
||
| 2454 | */ |
||
| 2455 | 1 | public function formatNationalNumberWithPreferredCarrierCode(PhoneNumber $number, $fallbackCarrierCode) |
|
| 2467 | |||
| 2468 | /** |
||
| 2469 | * Returns true if the number can be dialled from outside the region, or unknown. If the number |
||
| 2470 | * can only be dialled from within the region, returns false. Does not check the number is a valid |
||
| 2471 | * number. |
||
| 2472 | * TODO: Make this method public when we have enough metadata to make it worthwhile. |
||
| 2473 | * |
||
| 2474 | * @param PhoneNumber $number the phone-number for which we want to know whether it is diallable from outside the region |
||
| 2475 | * @return bool |
||
| 2476 | */ |
||
| 2477 | 35 | public function canBeInternationallyDialled(PhoneNumber $number) |
|
| 2488 | |||
| 2489 | /** |
||
| 2490 | * Normalizes a string of characters representing a phone number. This strips all characters which |
||
| 2491 | * are not diallable on a mobile phone keypad (including all non-ASCII digits). |
||
| 2492 | * |
||
| 2493 | * @param string $number a string of characters representing a phone number |
||
| 2494 | * @return string the normalized string version of the phone number |
||
| 2495 | */ |
||
| 2496 | 4 | public static function normalizeDiallableCharsOnly($number) |
|
| 2504 | |||
| 2505 | /** |
||
| 2506 | * Formats a phone number for out-of-country dialing purposes. |
||
| 2507 | * |
||
| 2508 | * Note that in this version, if the number was entered originally using alpha characters and |
||
| 2509 | * this version of the number is stored in raw_input, this representation of the number will be |
||
| 2510 | * used rather than the digit representation. Grouping information, as specified by characters |
||
| 2511 | * such as "-" and " ", will be retained. |
||
| 2512 | * |
||
| 2513 | * <p><b>Caveats:</b></p> |
||
| 2514 | * <ul> |
||
| 2515 | * <li> This will not produce good results if the country calling code is both present in the raw |
||
| 2516 | * input _and_ is the start of the national number. This is not a problem in the regions |
||
| 2517 | * which typically use alpha numbers. |
||
| 2518 | * <li> This will also not produce good results if the raw input has any grouping information |
||
| 2519 | * within the first three digits of the national number, and if the function needs to strip |
||
| 2520 | * preceding digits/words in the raw input before these digits. Normally people group the |
||
| 2521 | * first three digits together so this is not a huge problem - and will be fixed if it |
||
| 2522 | * proves to be so. |
||
| 2523 | * </ul> |
||
| 2524 | * |
||
| 2525 | * @param PhoneNumber $number the phone number that needs to be formatted |
||
| 2526 | * @param String $regionCallingFrom the region where the call is being placed |
||
| 2527 | * @return String the formatted phone number |
||
| 2528 | */ |
||
| 2529 | 1 | public function formatOutOfCountryKeepingAlphaChars(PhoneNumber $number, $regionCallingFrom) |
|
| 2622 | |||
| 2623 | /** |
||
| 2624 | * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is |
||
| 2625 | * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the |
||
| 2626 | * same as that of the region where the number is from, then NATIONAL formatting will be applied. |
||
| 2627 | * |
||
| 2628 | * <p>If the number itself has a country calling code of zero or an otherwise invalid country |
||
| 2629 | * calling code, then we return the number with no formatting applied. |
||
| 2630 | * |
||
| 2631 | * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and |
||
| 2632 | * Kazakhstan (who share the same country calling code). In those cases, no international prefix |
||
| 2633 | * is used. For regions which have multiple international prefixes, the number in its |
||
| 2634 | * INTERNATIONAL format will be returned instead. |
||
| 2635 | * |
||
| 2636 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2637 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2638 | * @return string the formatted phone number |
||
| 2639 | */ |
||
| 2640 | 8 | public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCallingFrom) |
|
| 2707 | |||
| 2708 | /** |
||
| 2709 | * Checks if this is a region under the North American Numbering Plan Administration (NANPA). |
||
| 2710 | * @param string $regionCode |
||
| 2711 | * @return boolean true if regionCode is one of the regions under NANPA |
||
| 2712 | */ |
||
| 2713 | 5 | public function isNANPACountry($regionCode) |
|
| 2717 | |||
| 2718 | /** |
||
| 2719 | * Formats a phone number using the original phone number format that the number is parsed from. |
||
| 2720 | * The original format is embedded in the country_code_source field of the PhoneNumber object |
||
| 2721 | * passed in. If such information is missing, the number will be formatted into the NATIONAL |
||
| 2722 | * format by default. When the number contains a leading zero and this is unexpected for this |
||
| 2723 | * country, or we don't have a formatting pattern for the number, the method returns the raw input |
||
| 2724 | * when it is available. |
||
| 2725 | * |
||
| 2726 | * Note this method guarantees no digit will be inserted, removed or modified as a result of |
||
| 2727 | * formatting. |
||
| 2728 | * |
||
| 2729 | * @param PhoneNumber $number the phone number that needs to be formatted in its original number format |
||
| 2730 | * @param string $regionCallingFrom the region whose IDD needs to be prefixed if the original number |
||
| 2731 | * has one |
||
| 2732 | * @return string the formatted phone number in its original number format |
||
| 2733 | */ |
||
| 2734 | 1 | public function formatInOriginalFormat(PhoneNumber $number, $regionCallingFrom) |
|
| 2832 | |||
| 2833 | /** |
||
| 2834 | * Returns true if a number is from a region whose national significant number couldn't contain a |
||
| 2835 | * leading zero, but has the italian_leading_zero field set to true. |
||
| 2836 | * @param PhoneNumber $number |
||
| 2837 | * @return bool |
||
| 2838 | */ |
||
| 2839 | 1 | protected function hasUnexpectedItalianLeadingZero(PhoneNumber $number) |
|
| 2843 | |||
| 2844 | /** |
||
| 2845 | * Checks whether the country calling code is from a region whose national significant number |
||
| 2846 | * could contain a leading zero. An example of such a region is Italy. Returns false if no |
||
| 2847 | * metadata for the country is found. |
||
| 2848 | * @param int $countryCallingCode |
||
| 2849 | * @return bool |
||
| 2850 | */ |
||
| 2851 | 2 | public function isLeadingZeroPossible($countryCallingCode) |
|
| 2862 | |||
| 2863 | /** |
||
| 2864 | * @param PhoneNumber $number |
||
| 2865 | * @return bool |
||
| 2866 | */ |
||
| 2867 | 1 | protected function hasFormattingPatternForNumber(PhoneNumber $number) |
|
| 2879 | |||
| 2880 | /** |
||
| 2881 | * Returns the national dialling prefix for a specific region. For example, this would be 1 for |
||
| 2882 | * the United States, and 0 for New Zealand. Set stripNonDigits to true to strip symbols like "~" |
||
| 2883 | * (which indicates a wait for a dialling tone) from the prefix returned. If no national prefix is |
||
| 2884 | * present, we return null. |
||
| 2885 | * |
||
| 2886 | * <p>Warning: Do not use this method for do-your-own formatting - for some regions, the |
||
| 2887 | * national dialling prefix is used only for certain types of numbers. Use the library's |
||
| 2888 | * formatting functions to prefix the national prefix when required. |
||
| 2889 | * |
||
| 2890 | * @param string $regionCode the region that we want to get the dialling prefix for |
||
| 2891 | * @param boolean $stripNonDigits true to strip non-digits from the national dialling prefix |
||
| 2892 | * @return string the dialling prefix for the region denoted by regionCode |
||
| 2893 | */ |
||
| 2894 | 28 | public function getNddPrefixForRegion($regionCode, $stripNonDigits) |
|
| 2912 | |||
| 2913 | /** |
||
| 2914 | * Check if rawInput, which is assumed to be in the national format, has a national prefix. The |
||
| 2915 | * national prefix is assumed to be in digits-only form. |
||
| 2916 | * @param string $rawInput |
||
| 2917 | * @param string $nationalPrefix |
||
| 2918 | * @param string $regionCode |
||
| 2919 | * @return bool |
||
| 2920 | */ |
||
| 2921 | 1 | protected function rawInputContainsNationalPrefix($rawInput, $nationalPrefix, $regionCode) |
|
| 2939 | |||
| 2940 | /** |
||
| 2941 | * Tests whether a phone number matches a valid pattern. Note this doesn't verify the number |
||
| 2942 | * is actually in use, which is impossible to tell by just looking at a number itself. It only |
||
| 2943 | * verifies whether the parsed, canonicalised number is valid: not whether a particular series of |
||
| 2944 | * digits entered by the user is diallable from the region provided when parsing. For example, the |
||
| 2945 | * number +41 (0) 78 927 2696 can be parsed into a number with country code "41" and national |
||
| 2946 | * significant number "789272696". This is valid, while the original string is not diallable. |
||
| 2947 | * |
||
| 2948 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2949 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2950 | */ |
||
| 2951 | 1977 | public function isValidNumber(PhoneNumber $number) |
|
| 2956 | |||
| 2957 | /** |
||
| 2958 | * Tests whether a phone number is valid for a certain region. Note this doesn't verify the number |
||
| 2959 | * is actually in use, which is impossible to tell by just looking at a number itself. If the |
||
| 2960 | * country calling code is not the same as the country calling code for the region, this |
||
| 2961 | * immediately exits with false. After this, the specific number pattern rules for the region are |
||
| 2962 | * examined. This is useful for determining for example whether a particular number is valid for |
||
| 2963 | * Canada, rather than just a valid NANPA number. |
||
| 2964 | * Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this |
||
| 2965 | * method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for |
||
| 2966 | * the region "GB" (United Kingdom), since it has its own region code, "IM", which may be |
||
| 2967 | * undesirable. |
||
| 2968 | * |
||
| 2969 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2970 | * @param string $regionCode the region that we want to validate the phone number for |
||
| 2971 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2972 | */ |
||
| 2973 | 1983 | public function isValidNumberForRegion(PhoneNumber $number, $regionCode) |
|
| 2989 | |||
| 2990 | /** |
||
| 2991 | * Parses a string and returns it as a phone number in proto buffer format. The method is quite |
||
| 2992 | * lenient and looks for a number in the input text (raw input) and does not check whether the |
||
| 2993 | * string is definitely only a phone number. To do this, it ignores punctuation and white-space, |
||
| 2994 | * as well as any text before the number (e.g. a leading “Tel: ”) and trims the non-number bits. |
||
| 2995 | * It will accept a number in any format (E164, national, international etc), assuming it can |
||
| 2996 | * interpreted with the defaultRegion supplied. It also attempts to convert any alpha characters |
||
| 2997 | * into digits if it thinks this is a vanity number of the type "1800 MICROSOFT". |
||
| 2998 | * |
||
| 2999 | * <p> This method will throw a {@link NumberParseException} if the number is not considered to |
||
| 3000 | * be a possible number. Note that validation of whether the number is actually a valid number |
||
| 3001 | * for a particular region is not performed. This can be done separately with {@link #isValidnumber}. |
||
| 3002 | * |
||
| 3003 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 3004 | * such as +, ( and -, as well as a phone number extension. |
||
| 3005 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 3006 | * if the number being parsed is not written in international format. |
||
| 3007 | * The country_code for the number in this case would be stored as that |
||
| 3008 | * of the default region supplied. If the number is guaranteed to |
||
| 3009 | * start with a '+' followed by the country calling code, then |
||
| 3010 | * "ZZ" or null can be supplied. |
||
| 3011 | * @param PhoneNumber|null $phoneNumber |
||
| 3012 | * @param bool $keepRawInput |
||
| 3013 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 3014 | * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. |
||
| 3015 | * too few or too many digits) or if no default region was supplied |
||
| 3016 | * and the number is not in international format (does not start |
||
| 3017 | * with +) |
||
| 3018 | */ |
||
| 3019 | 2808 | public function parse($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null, $keepRawInput = false) |
|
| 3027 | |||
| 3028 | /** |
||
| 3029 | * Formats a phone number in the specified format using client-defined formatting rules. Note that |
||
| 3030 | * if the phone number has a country calling code of zero or an otherwise invalid country calling |
||
| 3031 | * code, we cannot work out things like whether there should be a national prefix applied, or how |
||
| 3032 | * to format extensions, so we return the national significant number with no formatting applied. |
||
| 3033 | * |
||
| 3034 | * @param PhoneNumber $number the phone number to be formatted |
||
| 3035 | * @param int $numberFormat the format the phone number should be formatted into |
||
| 3036 | * @param array $userDefinedFormats formatting rules specified by clients |
||
| 3037 | * @return String the formatted phone number |
||
| 3038 | */ |
||
| 3039 | 2 | public function formatByPattern(PhoneNumber $number, $numberFormat, array $userDefinedFormats) |
|
| 3086 | |||
| 3087 | /** |
||
| 3088 | * Gets a valid number for the specified region. |
||
| 3089 | * |
||
| 3090 | * @param string regionCode the region for which an example number is needed |
||
| 3091 | * @return PhoneNumber a valid fixed-line number for the specified region. Returns null when the metadata |
||
| 3092 | * does not contain such information, or the region 001 is passed in. For 001 (representing |
||
| 3093 | * non-geographical numbers), call {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 3094 | */ |
||
| 3095 | 247 | public function getExampleNumber($regionCode) |
|
| 3099 | |||
| 3100 | /** |
||
| 3101 | * Gets an invalid number for the specified region. This is useful for unit-testing purposes, |
||
| 3102 | * where you want to test what will happen with an invalid number. Note that the number that is |
||
| 3103 | * returned will always be able to be parsed and will have the correct country code. It may also |
||
| 3104 | * be a valid *short* number/code for this region. Validity checking such numbers is handled with |
||
| 3105 | * {@link ShortNumberInfo}. |
||
| 3106 | * |
||
| 3107 | * @param string $regionCode The region for which an example number is needed |
||
| 3108 | * @return PhoneNumber|null An invalid number for the specified region. Returns null when an unsupported region |
||
| 3109 | * or the region 001 (Earth) is passed in. |
||
| 3110 | */ |
||
| 3111 | 244 | public function getInvalidExampleNumber($regionCode) |
|
| 3157 | |||
| 3158 | /** |
||
| 3159 | * Gets a valid number for the specified region and number type. |
||
| 3160 | * |
||
| 3161 | * @param string|int $regionCodeOrType the region for which an example number is needed |
||
| 3162 | * @param int $type the PhoneNumberType of number that is needed |
||
| 3163 | * @return PhoneNumber a valid number for the specified region and type. Returns null when the metadata |
||
| 3164 | * does not contain such information or if an invalid region or region 001 was entered. |
||
| 3165 | * For 001 (representing non-geographical numbers), call |
||
| 3166 | * {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 3167 | * |
||
| 3168 | * If $regionCodeOrType is the only parameter supplied, then a valid number for the specified number type |
||
| 3169 | * will be returned that may belong to any country. |
||
| 3170 | */ |
||
| 3171 | 3176 | public function getExampleNumberForType($regionCodeOrType, $type = null) |
|
| 3213 | |||
| 3214 | /** |
||
| 3215 | * @param PhoneMetadata $metadata |
||
| 3216 | * @param int $type PhoneNumberType |
||
| 3217 | * @return PhoneNumberDesc |
||
| 3218 | */ |
||
| 3219 | 4350 | protected function getNumberDescByType(PhoneMetadata $metadata, $type) |
|
| 3247 | |||
| 3248 | /** |
||
| 3249 | * Gets a valid number for the specified country calling code for a non-geographical entity. |
||
| 3250 | * |
||
| 3251 | * @param int $countryCallingCode the country calling code for a non-geographical entity |
||
| 3252 | * @return PhoneNumber a valid number for the non-geographical entity. Returns null when the metadata |
||
| 3253 | * does not contain such information, or the country calling code passed in does not belong |
||
| 3254 | * to a non-geographical entity. |
||
| 3255 | */ |
||
| 3256 | 10 | public function getExampleNumberForNonGeoEntity($countryCallingCode) |
|
| 3286 | |||
| 3287 | |||
| 3288 | /** |
||
| 3289 | * Takes two phone numbers and compares them for equality. |
||
| 3290 | * |
||
| 3291 | * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero |
||
| 3292 | * for Italian numbers and any extension present are the same. Returns NSN_MATCH |
||
| 3293 | * if either or both has no region specified, and the NSNs and extensions are |
||
| 3294 | * the same. Returns SHORT_NSN_MATCH if either or both has no region specified, |
||
| 3295 | * or the region specified is the same, and one NSN could be a shorter version |
||
| 3296 | * of the other number. This includes the case where one has an extension |
||
| 3297 | * specified, and the other does not. Returns NO_MATCH otherwise. For example, |
||
| 3298 | * the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH. The numbers |
||
| 3299 | * +1 345 657 1234 and 345 657 are a NO_MATCH. |
||
| 3300 | * |
||
| 3301 | * @param $firstNumberIn PhoneNumber|string First number to compare. If it is a |
||
| 3302 | * string it can contain formatting, and can have country calling code specified |
||
| 3303 | * with + at the start. |
||
| 3304 | * @param $secondNumberIn PhoneNumber|string Second number to compare. If it is a |
||
| 3305 | * string it can contain formatting, and can have country calling code specified |
||
| 3306 | * with + at the start. |
||
| 3307 | * @throws \InvalidArgumentException |
||
| 3308 | * @return int {MatchType} NOT_A_NUMBER, NO_MATCH, |
||
| 3309 | */ |
||
| 3310 | 8 | public function isNumberMatch($firstNumberIn, $secondNumberIn) |
|
| 3415 | |||
| 3416 | /** |
||
| 3417 | * Returns true when one national number is the suffix of the other or both are the same. |
||
| 3418 | * @param PhoneNumber $firstNumber |
||
| 3419 | * @param PhoneNumber $secondNumber |
||
| 3420 | * @return bool |
||
| 3421 | */ |
||
| 3422 | 4 | protected function isNationalNumberSuffixOfTheOther(PhoneNumber $firstNumber, PhoneNumber $secondNumber) |
|
| 3429 | |||
| 3430 | 4 | protected function stringEndsWithString($hayStack, $needle) |
|
| 3436 | |||
| 3437 | /** |
||
| 3438 | * Returns true if the supplied region supports mobile number portability. Returns false for |
||
| 3439 | * invalid, unknown or regions that don't support mobile number portability. |
||
| 3440 | * |
||
| 3441 | * @param string $regionCode the region for which we want to know whether it supports mobile number |
||
| 3442 | * portability or not. |
||
| 3443 | * @return bool |
||
| 3444 | */ |
||
| 3445 | 3 | public function isMobileNumberPortableRegion($regionCode) |
|
| 3454 | |||
| 3455 | /** |
||
| 3456 | * Check whether a phone number is a possible number given a number in the form of a string, and |
||
| 3457 | * the region where the number could be dialed from. It provides a more lenient check than |
||
| 3458 | * {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details. |
||
| 3459 | * |
||
| 3460 | * Convenience wrapper around {@link #isPossibleNumberWithReason}. Instead of returning the reason |
||
| 3461 | * for failure, this method returns a boolean value. |
||
| 3462 | * for failure, this method returns true if the number is either a possible fully-qualified number |
||
| 3463 | * (containing the area code and country code), or if the number could be a possible local number |
||
| 3464 | * (with a country code, but missing an area code). Local numbers are considered possible if they |
||
| 3465 | * could be possibly dialled in this format: if the area code is needed for a call to connect, the |
||
| 3466 | * number is not considered possible without it. |
||
| 3467 | * |
||
| 3468 | * Note: There are two ways to call this method. |
||
| 3469 | * |
||
| 3470 | * isPossibleNumber(PhoneNumber $numberObject) |
||
| 3471 | * isPossibleNumber(string '+441174960126', string 'GB') |
||
| 3472 | * |
||
| 3473 | * @param PhoneNumber|string $number the number that needs to be checked, in the form of a string |
||
| 3474 | * @param string|null $regionDialingFrom the region that we are expecting the number to be dialed from. |
||
| 3475 | * Note this is different from the region where the number belongs. For example, the number |
||
| 3476 | * +1 650 253 0000 is a number that belongs to US. When written in this form, it can be |
||
| 3477 | * dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any |
||
| 3478 | * region which uses an international dialling prefix of 00. When it is written as |
||
| 3479 | * 650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it |
||
| 3480 | * can only be dialed from within a smaller area in the US (Mountain View, CA, to be more |
||
| 3481 | * specific). |
||
| 3482 | * @return boolean true if the number is possible |
||
| 3483 | */ |
||
| 3484 | 57 | public function isPossibleNumber($number, $regionDialingFrom = null) |
|
| 3498 | |||
| 3499 | |||
| 3500 | /** |
||
| 3501 | * Check whether a phone number is a possible number. It provides a more lenient check than |
||
| 3502 | * {@link #isValidNumber} in the following sense: |
||
| 3503 | * <ol> |
||
| 3504 | * <li> It only checks the length of phone numbers. In particular, it doesn't check starting |
||
| 3505 | * digits of the number. |
||
| 3506 | * <li> It doesn't attempt to figure out the type of the number, but uses general rules which |
||
| 3507 | * applies to all types of phone numbers in a region. Therefore, it is much faster than |
||
| 3508 | * isValidNumber. |
||
| 3509 | * <li> For some numbers (particularly fixed-line), many regions have the concept of area code, |
||
| 3510 | * which together with subscriber number constitute the national significant number. It is |
||
| 3511 | * sometimes okay to dial only the subscriber number when dialing in the same area. This |
||
| 3512 | * function will return IS_POSSIBLE_LOCAL_ONLY if the subscriber-number-only version is |
||
| 3513 | * passed in. On the other hand, because isValidNumber validates using information on both |
||
| 3514 | * starting digits (for fixed line numbers, that would most likely be area codes) and |
||
| 3515 | * length (obviously includes the length of area codes for fixed line numbers), it will |
||
| 3516 | * return false for the subscriber-number-only version. |
||
| 3517 | * </ol> |
||
| 3518 | * @param PhoneNumber $number the number that needs to be checked |
||
| 3519 | * @return int a ValidationResult object which indicates whether the number is possible |
||
| 3520 | */ |
||
| 3521 | 59 | public function isPossibleNumberWithReason(PhoneNumber $number) |
|
| 3525 | |||
| 3526 | /** |
||
| 3527 | * Check whether a phone number is a possible number of a particular type. For types that don't |
||
| 3528 | * exist in a particular region, this will return a result that isn't so useful; it is recommended |
||
| 3529 | * that you use {@link #getSupportedTypesForRegion} or {@link #getSupportedTypesForNonGeoEntity} |
||
| 3530 | * respectively before calling this method to determine whether you should call it for this number |
||
| 3531 | * at all. |
||
| 3532 | * |
||
| 3533 | * This provides a more lenient check than {@link #isValidNumber} in the following sense: |
||
| 3534 | * |
||
| 3535 | * <ol> |
||
| 3536 | * <li> It only checks the length of phone numbers. In particular, it doesn't check starting |
||
| 3537 | * digits of the number. |
||
| 3538 | * <li> For some numbers (particularly fixed-line), many regions have the concept of area code, |
||
| 3539 | * which together with subscriber number constitute the national significant number. It is |
||
| 3540 | * sometimes okay to dial only the subscriber number when dialing in the same area. This |
||
| 3541 | * function will return IS_POSSIBLE_LOCAL_ONLY if the subscriber-number-only version is |
||
| 3542 | * passed in. On the other hand, because isValidNumber validates using information on both |
||
| 3543 | * starting digits (for fixed line numbers, that would most likely be area codes) and |
||
| 3544 | * length (obviously includes the length of area codes for fixed line numbers), it will |
||
| 3545 | * return false for the subscriber-number-only version. |
||
| 3546 | * </ol> |
||
| 3547 | * |
||
| 3548 | * @param PhoneNumber $number the number that needs to be checked |
||
| 3549 | * @param int $type the PhoneNumberType we are interested in |
||
| 3550 | * @return int a ValidationResult object which indicates whether the number is possible |
||
| 3551 | */ |
||
| 3552 | 68 | public function isPossibleNumberForTypeWithReason(PhoneNumber $number, $type) |
|
| 3572 | |||
| 3573 | /** |
||
| 3574 | * Attempts to extract a valid number from a phone number that is too long to be valid, and resets |
||
| 3575 | * the PhoneNumber object passed in to that valid version. If no valid number could be extracted, |
||
| 3576 | * the PhoneNumber object passed in will not be modified. |
||
| 3577 | * @param PhoneNumber $number a PhoneNumber object which contains a number that is too long to be valid. |
||
| 3578 | * @return boolean true if a valid phone number can be successfully extracted. |
||
| 3579 | */ |
||
| 3580 | 1 | public function truncateTooLongNumber(PhoneNumber $number) |
|
| 3598 | } |
||
| 3599 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: