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, used to find numbers in |
||
| 75 | // text and to decide what is a viable phone number. This excludes diallable characters. |
||
| 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 single international dialing |
||
| 84 | // prefix or not. If a region has a single international prefix (e.g. 011 in USA), it will be |
||
| 85 | // represented as a string that contains a sequence of ASCII digits, and possible a tilde, which |
||
| 86 | // signals waiting for the tone. If there are multiple available international prefixes in a |
||
| 87 | // region, they will be represented as a regex string that always contains one or more characters |
||
| 88 | // that are not ASCII digits or a tilde. |
||
| 89 | const SINGLE_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 | // Constants used in the formatting rules to represent the national prefix, first group and |
||
| 98 | // carrier code respectively. |
||
| 99 | const NP_STRING = '$NP'; |
||
| 100 | const FG_STRING = '$FG'; |
||
| 101 | const CC_STRING = '$CC'; |
||
| 102 | |||
| 103 | // A pattern that is used to determine if the national prefix formatting rule has the first group |
||
| 104 | // only, i.e., does not start with the national prefix. Note that the pattern explicitly allows |
||
| 105 | // for unbalanced parentheses. |
||
| 106 | const FIRST_GROUP_ONLY_PREFIX_PATTERN = '\\(?\\$1\\)?'; |
||
| 107 | public static $PLUS_CHARS_PATTERN; |
||
| 108 | protected static $SEPARATOR_PATTERN; |
||
| 109 | protected static $CAPTURING_DIGIT_PATTERN; |
||
| 110 | protected static $VALID_START_CHAR_PATTERN = null; |
||
| 111 | public static $SECOND_NUMBER_START_PATTERN = '[\\\\/] *x'; |
||
| 112 | public static $UNWANTED_END_CHAR_PATTERN = "[[\\P{N}&&\\P{L}]&&[^#]]+$"; |
||
| 113 | protected static $DIALLABLE_CHAR_MAPPINGS = array(); |
||
| 114 | protected static $CAPTURING_EXTN_DIGITS; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var PhoneNumberUtil |
||
| 118 | */ |
||
| 119 | protected static $instance = null; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Only upper-case variants of alpha characters are stored. |
||
| 123 | * @var array |
||
| 124 | */ |
||
| 125 | protected static $ALPHA_MAPPINGS = array( |
||
| 126 | 'A' => '2', |
||
| 127 | 'B' => '2', |
||
| 128 | 'C' => '2', |
||
| 129 | 'D' => '3', |
||
| 130 | 'E' => '3', |
||
| 131 | 'F' => '3', |
||
| 132 | 'G' => '4', |
||
| 133 | 'H' => '4', |
||
| 134 | 'I' => '4', |
||
| 135 | 'J' => '5', |
||
| 136 | 'K' => '5', |
||
| 137 | 'L' => '5', |
||
| 138 | 'M' => '6', |
||
| 139 | 'N' => '6', |
||
| 140 | 'O' => '6', |
||
| 141 | 'P' => '7', |
||
| 142 | 'Q' => '7', |
||
| 143 | 'R' => '7', |
||
| 144 | 'S' => '7', |
||
| 145 | 'T' => '8', |
||
| 146 | 'U' => '8', |
||
| 147 | 'V' => '8', |
||
| 148 | 'W' => '9', |
||
| 149 | 'X' => '9', |
||
| 150 | 'Y' => '9', |
||
| 151 | 'Z' => '9', |
||
| 152 | ); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Map of country calling codes that use a mobile token before the area code. One example of when |
||
| 156 | * this is relevant is when determining the length of the national destination code, which should |
||
| 157 | * be the length of the area code plus the length of the mobile token. |
||
| 158 | * @var array |
||
| 159 | */ |
||
| 160 | protected static $MOBILE_TOKEN_MAPPINGS = array(); |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set of country codes that have geographically assigned mobile numbers (see GEO_MOBILE_COUNTRIES |
||
| 164 | * below) which are not based on *area codes*. For example, in China mobile numbers start with a |
||
| 165 | * carrier indicator, and beyond that are geographically assigned: this carrier indicator is not |
||
| 166 | * considered to be an area code. |
||
| 167 | * |
||
| 168 | * @var array |
||
| 169 | */ |
||
| 170 | protected static $GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set of country calling codes that have geographically assigned mobile numbers. This may not be |
||
| 174 | * complete; we add calling codes case by case, as we find geographical mobile numbers or hear |
||
| 175 | * from user reports. Note that countries like the US, where we can't distinguish between |
||
| 176 | * fixed-line or mobile numbers, are not listed here, since we consider FIXED_LINE_OR_MOBILE to be |
||
| 177 | * a possibly geographically-related type anyway (like FIXED_LINE). |
||
| 178 | * |
||
| 179 | * @var array |
||
| 180 | */ |
||
| 181 | protected static $GEO_MOBILE_COUNTRIES; |
||
| 182 | |||
| 183 | /** |
||
| 184 | * For performance reasons, amalgamate both into one map. |
||
| 185 | * @var array |
||
| 186 | */ |
||
| 187 | protected static $ALPHA_PHONE_MAPPINGS = null; |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Separate map of all symbols that we wish to retain when formatting alpha numbers. This |
||
| 191 | * includes digits, ASCII letters and number grouping symbols such as "-" and " ". |
||
| 192 | * @var array |
||
| 193 | */ |
||
| 194 | protected static $ALL_PLUS_NUMBER_GROUPING_SYMBOLS; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and |
||
| 198 | * ALL_PLUS_NUMBER_GROUPING_SYMBOLS. |
||
| 199 | * @var array |
||
| 200 | */ |
||
| 201 | protected static $asciiDigitMappings = array( |
||
| 202 | '0' => '0', |
||
| 203 | '1' => '1', |
||
| 204 | '2' => '2', |
||
| 205 | '3' => '3', |
||
| 206 | '4' => '4', |
||
| 207 | '5' => '5', |
||
| 208 | '6' => '6', |
||
| 209 | '7' => '7', |
||
| 210 | '8' => '8', |
||
| 211 | '9' => '9', |
||
| 212 | ); |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Regexp of all possible ways to write extensions, for use when parsing. This will be run as a |
||
| 216 | * case-insensitive regexp match. Wide character versions are also provided after each ASCII |
||
| 217 | * version. |
||
| 218 | * @var String |
||
| 219 | */ |
||
| 220 | protected static $EXTN_PATTERNS_FOR_PARSING; |
||
| 221 | /** |
||
| 222 | * @var string |
||
| 223 | * @internal |
||
| 224 | */ |
||
| 225 | public static $EXTN_PATTERNS_FOR_MATCHING; |
||
| 226 | protected static $EXTN_PATTERN = null; |
||
| 227 | protected static $VALID_PHONE_NUMBER_PATTERN = null; |
||
| 228 | protected static $MIN_LENGTH_PHONE_NUMBER_PATTERN; |
||
| 229 | /** |
||
| 230 | * Regular expression of viable phone numbers. This is location independent. Checks we have at |
||
| 231 | * least three leading digits, and only valid punctuation, alpha characters and |
||
| 232 | * digits in the phone number. Does not include extension data. |
||
| 233 | * The symbol 'x' is allowed here as valid punctuation since it is often used as a placeholder for |
||
| 234 | * carrier codes, for example in Brazilian phone numbers. We also allow multiple "+" characters at |
||
| 235 | * the start. |
||
| 236 | * Corresponds to the following: |
||
| 237 | * [digits]{minLengthNsn}| |
||
| 238 | * plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])* |
||
| 239 | * |
||
| 240 | * The first reg-ex is to allow short numbers (two digits long) to be parsed if they are entered |
||
| 241 | * as "15" etc, but only if there is no punctuation in them. The second expression restricts the |
||
| 242 | * number of digits to three or more, but then allows them to be in international form, and to |
||
| 243 | * have alpha-characters and punctuation. |
||
| 244 | * |
||
| 245 | * Note VALID_PUNCTUATION starts with a -, so must be the first in the range. |
||
| 246 | * @var string |
||
| 247 | */ |
||
| 248 | protected static $VALID_PHONE_NUMBER; |
||
| 249 | protected static $numericCharacters = array( |
||
| 250 | "\xef\xbc\x90" => 0, |
||
| 251 | "\xef\xbc\x91" => 1, |
||
| 252 | "\xef\xbc\x92" => 2, |
||
| 253 | "\xef\xbc\x93" => 3, |
||
| 254 | "\xef\xbc\x94" => 4, |
||
| 255 | "\xef\xbc\x95" => 5, |
||
| 256 | "\xef\xbc\x96" => 6, |
||
| 257 | "\xef\xbc\x97" => 7, |
||
| 258 | "\xef\xbc\x98" => 8, |
||
| 259 | "\xef\xbc\x99" => 9, |
||
| 260 | |||
| 261 | "\xd9\xa0" => 0, |
||
| 262 | "\xd9\xa1" => 1, |
||
| 263 | "\xd9\xa2" => 2, |
||
| 264 | "\xd9\xa3" => 3, |
||
| 265 | "\xd9\xa4" => 4, |
||
| 266 | "\xd9\xa5" => 5, |
||
| 267 | "\xd9\xa6" => 6, |
||
| 268 | "\xd9\xa7" => 7, |
||
| 269 | "\xd9\xa8" => 8, |
||
| 270 | "\xd9\xa9" => 9, |
||
| 271 | |||
| 272 | "\xdb\xb0" => 0, |
||
| 273 | "\xdb\xb1" => 1, |
||
| 274 | "\xdb\xb2" => 2, |
||
| 275 | "\xdb\xb3" => 3, |
||
| 276 | "\xdb\xb4" => 4, |
||
| 277 | "\xdb\xb5" => 5, |
||
| 278 | "\xdb\xb6" => 6, |
||
| 279 | "\xdb\xb7" => 7, |
||
| 280 | "\xdb\xb8" => 8, |
||
| 281 | "\xdb\xb9" => 9, |
||
| 282 | |||
| 283 | "\xe1\xa0\x90" => 0, |
||
| 284 | "\xe1\xa0\x91" => 1, |
||
| 285 | "\xe1\xa0\x92" => 2, |
||
| 286 | "\xe1\xa0\x93" => 3, |
||
| 287 | "\xe1\xa0\x94" => 4, |
||
| 288 | "\xe1\xa0\x95" => 5, |
||
| 289 | "\xe1\xa0\x96" => 6, |
||
| 290 | "\xe1\xa0\x97" => 7, |
||
| 291 | "\xe1\xa0\x98" => 8, |
||
| 292 | "\xe1\xa0\x99" => 9, |
||
| 293 | ); |
||
| 294 | |||
| 295 | /** |
||
| 296 | * The set of county calling codes that map to the non-geo entity region ("001"). |
||
| 297 | * @var array |
||
| 298 | */ |
||
| 299 | protected $countryCodesForNonGeographicalRegion = array(); |
||
| 300 | /** |
||
| 301 | * The set of regions the library supports. |
||
| 302 | * @var array |
||
| 303 | */ |
||
| 304 | protected $supportedRegions = array(); |
||
| 305 | |||
| 306 | /** |
||
| 307 | * A mapping from a country calling code to the region codes which denote the region represented |
||
| 308 | * by that country calling code. In the case of multiple regions sharing a calling code, such as |
||
| 309 | * the NANPA regions, the one indicated with "isMainCountryForCode" in the metadata should be |
||
| 310 | * first. |
||
| 311 | * @var array |
||
| 312 | */ |
||
| 313 | protected $countryCallingCodeToRegionCodeMap = array(); |
||
| 314 | /** |
||
| 315 | * The set of regions that share country calling code 1. |
||
| 316 | * @var array |
||
| 317 | */ |
||
| 318 | protected $nanpaRegions = array(); |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @var MetadataSourceInterface |
||
| 322 | */ |
||
| 323 | protected $metadataSource; |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @var MatcherAPIInterface |
||
| 327 | */ |
||
| 328 | protected $matcherAPI; |
||
| 329 | |||
| 330 | /** |
||
| 331 | * This class implements a singleton, so the only constructor is protected. |
||
| 332 | * @param MetadataSourceInterface $metadataSource |
||
| 333 | * @param $countryCallingCodeToRegionCodeMap |
||
| 334 | */ |
||
| 335 | 667 | protected function __construct(MetadataSourceInterface $metadataSource, $countryCallingCodeToRegionCodeMap) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting, |
||
| 396 | * parsing, or validation. The instance is loaded with phone number metadata for a number of most |
||
| 397 | * commonly used regions. |
||
| 398 | * |
||
| 399 | * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance |
||
| 400 | * multiple times will only result in one instance being created. |
||
| 401 | * |
||
| 402 | * @param string $baseFileLocation |
||
| 403 | * @param array|null $countryCallingCodeToRegionCodeMap |
||
| 404 | * @param MetadataLoaderInterface|null $metadataLoader |
||
| 405 | * @param MetadataSourceInterface|null $metadataSource |
||
| 406 | * @return PhoneNumberUtil instance |
||
| 407 | */ |
||
| 408 | 6288 | public static function getInstance($baseFileLocation = self::META_DATA_FILE_PREFIX, array $countryCallingCodeToRegionCodeMap = null, MetadataLoaderInterface $metadataLoader = null, MetadataSourceInterface $metadataSource = null) |
|
| 427 | |||
| 428 | 667 | protected function init() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @internal |
||
| 453 | */ |
||
| 454 | 669 | public static function initCapturingExtnDigits() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * @internal |
||
| 461 | */ |
||
| 462 | 669 | public static function initExtnPatterns() |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Helper initialiser method to create the regular-expression pattern to match extensions, |
||
| 477 | * allowing the one-char extension symbols provided by {@code singleExtnSymbols}. |
||
| 478 | * @param string $singleExtnSymbols |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | 669 | protected static function createExtnPattern($singleExtnSymbols) |
|
| 499 | |||
| 500 | 667 | protected static function initExtnPattern() |
|
| 504 | |||
| 505 | 669 | protected static function initValidPhoneNumberPatterns() |
|
| 513 | |||
| 514 | 669 | protected static function initAlphaPhoneMappings() |
|
| 518 | |||
| 519 | 668 | protected static function initValidStartCharPattern() |
|
| 523 | |||
| 524 | 668 | protected static function initMobileTokenMappings() |
|
| 530 | |||
| 531 | 668 | protected static function initDiallableCharMappings() |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Used for testing purposes only to reset the PhoneNumberUtil singleton to null. |
||
| 541 | */ |
||
| 542 | 673 | public static function resetInstance() |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Converts all alpha characters in a number to their respective digits on a keypad, but retains |
||
| 549 | * existing formatting. |
||
| 550 | * @param string $number |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | 2 | public static function convertAlphaCharactersInNumber($number) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Normalizes a string of characters representing a phone number by replacing all characters found |
||
| 564 | * in the accompanying map with the values therein, and stripping all other characters if |
||
| 565 | * removeNonMatches is true. |
||
| 566 | * |
||
| 567 | * @param string $number a string of characters representing a phone number |
||
| 568 | * @param array $normalizationReplacements a mapping of characters to what they should be replaced by in |
||
| 569 | * the normalized version of the phone number |
||
| 570 | * @param bool $removeNonMatches indicates whether characters that are not able to be replaced |
||
| 571 | * should be stripped from the number. If this is false, they will be left unchanged in the number. |
||
| 572 | * @return string the normalized string version of the phone number |
||
| 573 | */ |
||
| 574 | 15 | protected static function normalizeHelper($number, array $normalizationReplacements, $removeNonMatches) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Helper function to check if the national prefix formatting rule has the first group only, i.e., |
||
| 594 | * does not start with the national prefix. |
||
| 595 | * @param string $nationalPrefixFormattingRule |
||
| 596 | * @return bool |
||
| 597 | */ |
||
| 598 | 41 | public static function formattingRuleHasFirstGroupOnly($nationalPrefixFormattingRule) |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Returns all regions the library has metadata for. |
||
| 609 | * |
||
| 610 | * @return array An unordered array of the two-letter region codes for every geographical region the |
||
| 611 | * library supports |
||
| 612 | */ |
||
| 613 | 246 | public function getSupportedRegions() |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Returns all global network calling codes the library has metadata for. |
||
| 620 | * |
||
| 621 | * @return array An unordered array of the country calling codes for every non-geographical entity |
||
| 622 | * the library supports |
||
| 623 | */ |
||
| 624 | 2 | public function getSupportedGlobalNetworkCallingCodes() |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Returns all country calling codes the library has metadata for, covering both non-geographical |
||
| 631 | * entities (global network calling codes) and those used for geographical entities. The could be |
||
| 632 | * used to populate a drop-down box of country calling codes for a phone-number widget, for |
||
| 633 | * instance. |
||
| 634 | * |
||
| 635 | * @return array An unordered array of the country calling codes for every geographical and |
||
| 636 | * non-geographical entity the library supports |
||
| 637 | */ |
||
| 638 | 1 | public function getSupportedCallingCodes() |
|
| 642 | |||
| 643 | /** |
||
| 644 | * Returns true if there is any possible number data set for a particular PhoneNumberDesc. |
||
| 645 | * |
||
| 646 | * @param PhoneNumberDesc $desc |
||
| 647 | * @return bool |
||
| 648 | */ |
||
| 649 | 5 | protected static function descHasPossibleNumberData(PhoneNumberDesc $desc) |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Returns true if there is any data set for a particular PhoneNumberDesc. |
||
| 659 | * |
||
| 660 | * @param PhoneNumberDesc $desc |
||
| 661 | * @return bool |
||
| 662 | */ |
||
| 663 | 2 | protected static function descHasData(PhoneNumberDesc $desc) |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Returns the types we have metadata for based on the PhoneMetadata object passed in |
||
| 676 | * |
||
| 677 | * @param PhoneMetadata $metadata |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | 2 | private function getSupportedTypesForMetadata(PhoneMetadata $metadata) |
|
| 697 | |||
| 698 | /** |
||
| 699 | * Returns the types for a given region which the library has metadata for. Will not include |
||
| 700 | * FIXED_LINE_OR_MOBILE (if the numbers in this region could be classified as FIXED_LINE_OR_MOBILE, |
||
| 701 | * both FIXED_LINE and MOBILE would be present) and UNKNOWN. |
||
| 702 | * |
||
| 703 | * No types will be returned for invalid or unknown region codes. |
||
| 704 | * |
||
| 705 | * @param string $regionCode |
||
| 706 | * @return array |
||
| 707 | */ |
||
| 708 | 1 | public function getSupportedTypesForRegion($regionCode) |
|
| 716 | |||
| 717 | /** |
||
| 718 | * Returns the types for a country-code belonging to a non-geographical entity which the library |
||
| 719 | * has metadata for. Will not include FIXED_LINE_OR_MOBILE (if numbers for this non-geographical |
||
| 720 | * entity could be classified as FIXED_LINE_OR_MOBILE, both FIXED_LINE and MOBILE would be |
||
| 721 | * present) and UNKNOWN. |
||
| 722 | * |
||
| 723 | * @param int $countryCallingCode |
||
| 724 | * @return array |
||
| 725 | */ |
||
| 726 | 1 | public function getSupportedTypesForNonGeoEntity($countryCallingCode) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Gets the length of the geographical area code from the {@code nationalNumber} field of the |
||
| 738 | * PhoneNumber object passed in, so that clients could use it to split a national significant |
||
| 739 | * number into geographical area code and subscriber number. It works in such a way that the |
||
| 740 | * resultant subscriber number should be diallable, at least on some devices. An example of how |
||
| 741 | * this could be used: |
||
| 742 | * |
||
| 743 | * <code> |
||
| 744 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 745 | * $number = $phoneUtil->parse("16502530000", "US"); |
||
| 746 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 747 | * |
||
| 748 | * $areaCodeLength = $phoneUtil->getLengthOfGeographicalAreaCode($number); |
||
| 749 | * if ($areaCodeLength > 0) |
||
| 750 | * { |
||
| 751 | * $areaCode = substr($nationalSignificantNumber, 0,$areaCodeLength); |
||
| 752 | * $subscriberNumber = substr($nationalSignificantNumber, $areaCodeLength); |
||
| 753 | * } else { |
||
| 754 | * $areaCode = ""; |
||
| 755 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 756 | * } |
||
| 757 | * </code> |
||
| 758 | * |
||
| 759 | * N.B.: area code is a very ambiguous concept, so the I18N team generally recommends against |
||
| 760 | * using it for most purposes, but recommends using the more general {@code nationalNumber} |
||
| 761 | * instead. Read the following carefully before deciding to use this method: |
||
| 762 | * <ul> |
||
| 763 | * <li> geographical area codes change over time, and this method honors those changes; |
||
| 764 | * therefore, it doesn't guarantee the stability of the result it produces. |
||
| 765 | * <li> subscriber numbers may not be diallable from all devices (notably mobile devices, which |
||
| 766 | * typically requires the full national_number to be dialled in most regions). |
||
| 767 | * <li> most non-geographical numbers have no area codes, including numbers from non-geographical |
||
| 768 | * entities |
||
| 769 | * <li> some geographical numbers have no area codes. |
||
| 770 | * </ul> |
||
| 771 | * @param PhoneNumber $number PhoneNumber object for which clients want to know the length of the area code. |
||
| 772 | * @return int the length of area code of the PhoneNumber object passed in. |
||
| 773 | */ |
||
| 774 | 1 | public function getLengthOfGeographicalAreaCode(PhoneNumber $number) |
|
| 804 | |||
| 805 | /** |
||
| 806 | * Returns the metadata for the given region code or {@code null} if the region code is invalid |
||
| 807 | * or unknown. |
||
| 808 | * @param string $regionCode |
||
| 809 | * @return PhoneMetadata |
||
| 810 | */ |
||
| 811 | 4995 | public function getMetadataForRegion($regionCode) |
|
| 819 | |||
| 820 | /** |
||
| 821 | * Helper function to check region code is not unknown or null. |
||
| 822 | * @param string $regionCode |
||
| 823 | * @return bool |
||
| 824 | */ |
||
| 825 | 4996 | protected function isValidRegionCode($regionCode) |
|
| 829 | |||
| 830 | /** |
||
| 831 | * Returns the region where a phone number is from. This could be used for geocoding at the region |
||
| 832 | * level. Only guarantees correct results for valid, full numbers (not short-codes, or invalid |
||
| 833 | * numbers). |
||
| 834 | * |
||
| 835 | * @param PhoneNumber $number the phone number whose origin we want to know |
||
| 836 | * @return null|string the region where the phone number is from, or null if no region matches this calling |
||
| 837 | * code |
||
| 838 | */ |
||
| 839 | 2283 | public function getRegionCodeForNumber(PhoneNumber $number) |
|
| 852 | |||
| 853 | /** |
||
| 854 | * @param PhoneNumber $number |
||
| 855 | * @param array $regionCodes |
||
| 856 | * @return null|string |
||
| 857 | */ |
||
| 858 | 596 | protected function getRegionCodeForNumberFromRegionList(PhoneNumber $number, array $regionCodes) |
|
| 881 | |||
| 882 | /** |
||
| 883 | * Gets the national significant number of the a phone number. Note a national significant number |
||
| 884 | * doesn't contain a national prefix or any formatting. |
||
| 885 | * |
||
| 886 | * @param PhoneNumber $number the phone number for which the national significant number is needed |
||
| 887 | * @return string the national significant number of the PhoneNumber object passed in |
||
| 888 | */ |
||
| 889 | 2182 | public function getNationalSignificantNumber(PhoneNumber $number) |
|
| 900 | |||
| 901 | /** |
||
| 902 | * @param string $nationalNumber |
||
| 903 | * @param PhoneMetadata $metadata |
||
| 904 | * @return int PhoneNumberType constant |
||
| 905 | */ |
||
| 906 | 2063 | protected function getNumberTypeHelper($nationalNumber, PhoneMetadata $metadata) |
|
| 955 | |||
| 956 | /** |
||
| 957 | * @param string $nationalNumber |
||
| 958 | * @param PhoneNumberDesc $numberDesc |
||
| 959 | * @return bool |
||
| 960 | */ |
||
| 961 | 2091 | public function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) |
|
| 974 | |||
| 975 | /** |
||
| 976 | * isNumberGeographical(PhoneNumber) |
||
| 977 | * |
||
| 978 | * Tests whether a phone number has a geographical association. It checks if the number is |
||
| 979 | * associated with a certain region in the country to which it belongs. Note that this doesn't |
||
| 980 | * verify if the number is actually in use. |
||
| 981 | * |
||
| 982 | * isNumberGeographical(PhoneNumberType, $countryCallingCode) |
||
| 983 | * |
||
| 984 | * Tests whether a phone number has a geographical association, as represented by its type and the |
||
| 985 | * country it belongs to. |
||
| 986 | * |
||
| 987 | * This version exists since calculating the phone number type is expensive; if we have already |
||
| 988 | * done this, we don't want to do it again. |
||
| 989 | * |
||
| 990 | * @param PhoneNumber|int $phoneNumberObjOrType A PhoneNumber object, or a PhoneNumberType integer |
||
| 991 | * @param int|null $countryCallingCode Used when passing a PhoneNumberType |
||
| 992 | * @return bool |
||
| 993 | */ |
||
| 994 | 21 | public function isNumberGeographical($phoneNumberObjOrType, $countryCallingCode = null) |
|
| 1005 | |||
| 1006 | /** |
||
| 1007 | * Gets the type of a valid phone number. |
||
| 1008 | * @param PhoneNumber $number the number the phone number that we want to know the type |
||
| 1009 | * @return int PhoneNumberType the type of the phone number, or UNKNOWN if it is invalid |
||
| 1010 | */ |
||
| 1011 | 1363 | public function getNumberType(PhoneNumber $number) |
|
| 1021 | |||
| 1022 | /** |
||
| 1023 | * @param int $countryCallingCode |
||
| 1024 | * @param string $regionCode |
||
| 1025 | * @return PhoneMetadata |
||
| 1026 | */ |
||
| 1027 | 2101 | protected function getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode) |
|
| 1032 | |||
| 1033 | /** |
||
| 1034 | * @param int $countryCallingCode |
||
| 1035 | * @return PhoneMetadata |
||
| 1036 | */ |
||
| 1037 | 34 | public function getMetadataForNonGeographicalRegion($countryCallingCode) |
|
| 1044 | |||
| 1045 | /** |
||
| 1046 | * Gets the length of the national destination code (NDC) from the PhoneNumber object passed in, |
||
| 1047 | * so that clients could use it to split a national significant number into NDC and subscriber |
||
| 1048 | * number. The NDC of a phone number is normally the first group of digit(s) right after the |
||
| 1049 | * country calling code when the number is formatted in the international format, if there is a |
||
| 1050 | * subscriber number part that follows. An example of how this could be used: |
||
| 1051 | * |
||
| 1052 | * <code> |
||
| 1053 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 1054 | * $number = $phoneUtil->parse("18002530000", "US"); |
||
| 1055 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 1056 | * |
||
| 1057 | * $nationalDestinationCodeLength = $phoneUtil->getLengthOfNationalDestinationCode($number); |
||
| 1058 | * if ($nationalDestinationCodeLength > 0) { |
||
| 1059 | * $nationalDestinationCode = substr($nationalSignificantNumber, 0, $nationalDestinationCodeLength); |
||
| 1060 | * $subscriberNumber = substr($nationalSignificantNumber, $nationalDestinationCodeLength); |
||
| 1061 | * } else { |
||
| 1062 | * $nationalDestinationCode = ""; |
||
| 1063 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 1064 | * } |
||
| 1065 | * </code> |
||
| 1066 | * |
||
| 1067 | * Refer to the unit tests to see the difference between this function and |
||
| 1068 | * {@link #getLengthOfGeographicalAreaCode}. |
||
| 1069 | * |
||
| 1070 | * @param PhoneNumber $number the PhoneNumber object for which clients want to know the length of the NDC. |
||
| 1071 | * @return int the length of NDC of the PhoneNumber object passed in. |
||
| 1072 | */ |
||
| 1073 | 2 | public function getLengthOfNationalDestinationCode(PhoneNumber $number) |
|
| 1110 | |||
| 1111 | /** |
||
| 1112 | * Formats a phone number in the specified format using default rules. Note that this does not |
||
| 1113 | * promise to produce a phone number that the user can dial from where they are - although we do |
||
| 1114 | * format in either 'national' or 'international' format depending on what the client asks for, we |
||
| 1115 | * do not currently support a more abbreviated format, such as for users in the same "area" who |
||
| 1116 | * could potentially dial the number without area code. Note that if the phone number has a |
||
| 1117 | * country calling code of 0 or an otherwise invalid country calling code, we cannot work out |
||
| 1118 | * which formatting rules to apply so we return the national significant number with no formatting |
||
| 1119 | * applied. |
||
| 1120 | * |
||
| 1121 | * @param PhoneNumber $number the phone number to be formatted |
||
| 1122 | * @param int $numberFormat the PhoneNumberFormat the phone number should be formatted into |
||
| 1123 | * @return string the formatted phone number |
||
| 1124 | */ |
||
| 1125 | 342 | public function format(PhoneNumber $number, $numberFormat) |
|
| 1168 | |||
| 1169 | /** |
||
| 1170 | * A helper function that is used by format and formatByPattern. |
||
| 1171 | * @param int $countryCallingCode |
||
| 1172 | * @param int $numberFormat PhoneNumberFormat |
||
| 1173 | * @param string $formattedNumber |
||
| 1174 | */ |
||
| 1175 | 343 | protected function prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, &$formattedNumber) |
|
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Helper function to check the country calling code is valid. |
||
| 1195 | * @param int $countryCallingCode |
||
| 1196 | * @return bool |
||
| 1197 | */ |
||
| 1198 | 164 | protected function hasValidCountryCallingCode($countryCallingCode) |
|
| 1202 | |||
| 1203 | /** |
||
| 1204 | * Returns the region code that matches the specific country calling code. In the case of no |
||
| 1205 | * region code being found, ZZ will be returned. In the case of multiple regions, the one |
||
| 1206 | * designated in the metadata as the "main" region for this calling code will be returned. If the |
||
| 1207 | * countryCallingCode entered is valid but doesn't match a specific region (such as in the case of |
||
| 1208 | * non-geographical calling codes like 800) the value "001" will be returned (corresponding to |
||
| 1209 | * the value for World in the UN M.49 schema). |
||
| 1210 | * |
||
| 1211 | * @param int $countryCallingCode |
||
| 1212 | * @return string |
||
| 1213 | */ |
||
| 1214 | 551 | public function getRegionCodeForCountryCode($countryCallingCode) |
|
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Note in some regions, the national number can be written in two completely different ways |
||
| 1222 | * depending on whether it forms part of the NATIONAL format or INTERNATIONAL format. The |
||
| 1223 | * numberFormat parameter here is used to specify which format to use for those cases. If a |
||
| 1224 | * carrierCode is specified, this will be inserted into the formatted string to replace $CC. |
||
| 1225 | * @param string $number |
||
| 1226 | * @param PhoneMetadata $metadata |
||
| 1227 | * @param int $numberFormat PhoneNumberFormat |
||
| 1228 | * @param null|string $carrierCode |
||
| 1229 | * @return string |
||
| 1230 | */ |
||
| 1231 | 95 | protected function formatNsn($number, PhoneMetadata $metadata, $numberFormat, $carrierCode = null) |
|
| 1244 | |||
| 1245 | /** |
||
| 1246 | * @param NumberFormat[] $availableFormats |
||
| 1247 | * @param string $nationalNumber |
||
| 1248 | * @return NumberFormat|null |
||
| 1249 | */ |
||
| 1250 | 128 | public function chooseFormattingPatternForNumber(array $availableFormats, $nationalNumber) |
|
| 1271 | |||
| 1272 | /** |
||
| 1273 | * Note that carrierCode is optional - if null or an empty string, no carrier code replacement |
||
| 1274 | * will take place. |
||
| 1275 | * @param string $nationalNumber |
||
| 1276 | * @param NumberFormat $formattingPattern |
||
| 1277 | * @param int $numberFormat PhoneNumberFormat |
||
| 1278 | * @param null|string $carrierCode |
||
| 1279 | * @return string |
||
| 1280 | */ |
||
| 1281 | 95 | public function formatNsnUsingPattern( |
|
| 1327 | |||
| 1328 | /** |
||
| 1329 | * Appends the formatted extension of a phone number to formattedNumber, if the phone number had |
||
| 1330 | * an extension specified. |
||
| 1331 | * |
||
| 1332 | * @param PhoneNumber $number |
||
| 1333 | * @param PhoneMetadata|null $metadata |
||
| 1334 | * @param int $numberFormat PhoneNumberFormat |
||
| 1335 | * @param string $formattedNumber |
||
| 1336 | */ |
||
| 1337 | 96 | protected function maybeAppendFormattedExtension(PhoneNumber $number, $metadata, $numberFormat, &$formattedNumber) |
|
| 1351 | |||
| 1352 | /** |
||
| 1353 | * Returns the mobile token for the provided country calling code if it has one, otherwise |
||
| 1354 | * returns an empty string. A mobile token is a number inserted before the area code when dialing |
||
| 1355 | * a mobile number from that country from abroad. |
||
| 1356 | * |
||
| 1357 | * @param int $countryCallingCode the country calling code for which we want the mobile token |
||
| 1358 | * @return string the mobile token, as a string, for the given country calling code |
||
| 1359 | */ |
||
| 1360 | 16 | public static function getCountryMobileToken($countryCallingCode) |
|
| 1371 | |||
| 1372 | /** |
||
| 1373 | * Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT. A valid vanity |
||
| 1374 | * number will start with at least 3 digits and will have three or more alpha characters. This |
||
| 1375 | * does not do region-specific checks - to work out if this number is actually valid for a region, |
||
| 1376 | * it should be parsed and methods such as {@link #isPossibleNumberWithReason} and |
||
| 1377 | * {@link #isValidNumber} should be used. |
||
| 1378 | * |
||
| 1379 | * @param string $number the number that needs to be checked |
||
| 1380 | * @return bool true if the number is a valid vanity number |
||
| 1381 | */ |
||
| 1382 | 1 | public function isAlphaNumber($number) |
|
| 1391 | |||
| 1392 | /** |
||
| 1393 | * Checks to see if the string of characters could possibly be a phone number at all. At the |
||
| 1394 | * moment, checks to see that the string begins with at least 2 digits, ignoring any punctuation |
||
| 1395 | * commonly found in phone numbers. |
||
| 1396 | * This method does not require the number to be normalized in advance - but does assume that |
||
| 1397 | * leading non-number symbols have been removed, such as by the method extractPossibleNumber. |
||
| 1398 | * |
||
| 1399 | * @param string $number to be checked for viability as a phone number |
||
| 1400 | * @return boolean true if the number could be a phone number of some sort, otherwise false |
||
| 1401 | */ |
||
| 1402 | 3063 | public static function isViablePhoneNumber($number) |
|
| 1417 | |||
| 1418 | /** |
||
| 1419 | * We append optionally the extension pattern to the end here, as a valid phone number may |
||
| 1420 | * have an extension prefix appended, followed by 1 or more digits. |
||
| 1421 | * @return string |
||
| 1422 | */ |
||
| 1423 | 3062 | protected static function getValidPhoneNumberPattern() |
|
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Strips any extension (as in, the part of the number dialled after the call is connected, |
||
| 1430 | * usually indicated with extn, ext, x or similar) from the end of the number, and returns it. |
||
| 1431 | * |
||
| 1432 | * @param string $number the non-normalized telephone number that we wish to strip the extension from |
||
| 1433 | * @return string the phone extension |
||
| 1434 | */ |
||
| 1435 | 3057 | protected function maybeStripExtension(&$number) |
|
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Parses a string and returns it in proto buffer format. This method differs from {@link #parse} |
||
| 1459 | * in that it always populates the raw_input field of the protocol buffer with numberToParse as |
||
| 1460 | * well as the country_code_source field. |
||
| 1461 | * |
||
| 1462 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 1463 | * such as +, ( and -, as well as a phone number extension. It can also |
||
| 1464 | * be provided in RFC3966 format. |
||
| 1465 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 1466 | * if the number being parsed is not written in international format. |
||
| 1467 | * The country calling code for the number in this case would be stored |
||
| 1468 | * as that of the default region supplied. |
||
| 1469 | * @param PhoneNumber $phoneNumber |
||
| 1470 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 1471 | */ |
||
| 1472 | 180 | public function parseAndKeepRawInput($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null) |
|
| 1480 | |||
| 1481 | /** |
||
| 1482 | * Returns an iterable over all PhoneNumberMatches in $text |
||
| 1483 | * |
||
| 1484 | * @param string $text |
||
| 1485 | * @param string $defaultRegion |
||
| 1486 | * @param AbstractLeniency $leniency Defaults to Leniency::VALID() |
||
| 1487 | * @param int $maxTries Defaults to PHP_INT_MAX |
||
| 1488 | * @return PhoneNumberMatcher |
||
| 1489 | */ |
||
| 1490 | 205 | public function findNumbers($text, $defaultRegion, AbstractLeniency $leniency = null, $maxTries = PHP_INT_MAX) |
|
| 1498 | |||
| 1499 | /** |
||
| 1500 | * Gets an AsYouTypeFormatter for the specific region. |
||
| 1501 | * |
||
| 1502 | * @param string $regionCode The region where the phone number is being entered. |
||
| 1503 | * @return AsYouTypeFormatter |
||
| 1504 | */ |
||
| 1505 | 33 | public function getAsYouTypeFormatter($regionCode) |
|
| 1509 | |||
| 1510 | /** |
||
| 1511 | * A helper function to set the values related to leading zeros in a PhoneNumber. |
||
| 1512 | * @param string $nationalNumber |
||
| 1513 | * @param PhoneNumber $phoneNumber |
||
| 1514 | */ |
||
| 1515 | 3054 | public static function setItalianLeadingZerosForPhoneNumber($nationalNumber, PhoneNumber $phoneNumber) |
|
| 1532 | |||
| 1533 | /** |
||
| 1534 | * Parses a string and fills up the phoneNumber. This method is the same as the public |
||
| 1535 | * parse() method, with the exception that it allows the default region to be null, for use by |
||
| 1536 | * isNumberMatch(). checkRegion should be set to false if it is permitted for the default region |
||
| 1537 | * to be null or unknown ("ZZ"). |
||
| 1538 | * @param string $numberToParse |
||
| 1539 | * @param string $defaultRegion |
||
| 1540 | * @param bool $keepRawInput |
||
| 1541 | * @param bool $checkRegion |
||
| 1542 | * @param PhoneNumber $phoneNumber |
||
| 1543 | * @throws NumberParseException |
||
| 1544 | */ |
||
| 1545 | 3060 | protected function parseHelper($numberToParse, $defaultRegion, $keepRawInput, $checkRegion, PhoneNumber $phoneNumber) |
|
| 1694 | |||
| 1695 | /** |
||
| 1696 | * Returns a new phone number containing only the fields needed to uniquely identify a phone |
||
| 1697 | * number, rather than any fields that capture the context in which the phone number was created. |
||
| 1698 | * These fields correspond to those set in parse() rather than parseAndKeepRawInput() |
||
| 1699 | * |
||
| 1700 | * @param PhoneNumber $phoneNumberIn |
||
| 1701 | * @return PhoneNumber |
||
| 1702 | */ |
||
| 1703 | 8 | protected static function copyCoreFieldsOnly(PhoneNumber $phoneNumberIn) |
|
| 1718 | |||
| 1719 | /** |
||
| 1720 | * Converts numberToParse to a form that we can parse and write it to nationalNumber if it is |
||
| 1721 | * written in RFC3966; otherwise extract a possible number out of it and write to nationalNumber. |
||
| 1722 | * @param string $numberToParse |
||
| 1723 | * @param string $nationalNumber |
||
| 1724 | */ |
||
| 1725 | 3058 | protected function buildNationalNumberForParsing($numberToParse, &$nationalNumber) |
|
| 1770 | |||
| 1771 | /** |
||
| 1772 | * Attempts to extract a possible number from the string passed in. This currently strips all |
||
| 1773 | * leading characters that cannot be used to start a phone number. Characters that can be used to |
||
| 1774 | * start a phone number are defined in the VALID_START_CHAR_PATTERN. If none of these characters |
||
| 1775 | * are found in the number passed in, an empty string is returned. This function also attempts to |
||
| 1776 | * strip off any alternative extensions or endings if two or more are present, such as in the case |
||
| 1777 | * of: (530) 583-6985 x302/x2303. The second extension here makes this actually two phone numbers, |
||
| 1778 | * (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second extension so that the first |
||
| 1779 | * number is parsed correctly. |
||
| 1780 | * |
||
| 1781 | * @param int $number the string that might contain a phone number |
||
| 1782 | * @return string the number, stripped of any non-phone-number prefix (such as "Tel:") or an empty |
||
| 1783 | * string if no character used to start phone numbers (such as + or any digit) is |
||
| 1784 | * found in the number |
||
| 1785 | */ |
||
| 1786 | 3081 | public static function extractPossibleNumber($number) |
|
| 1813 | |||
| 1814 | /** |
||
| 1815 | * Checks to see that the region code used is valid, or if it is not valid, that the number to |
||
| 1816 | * parse starts with a + symbol so that we can attempt to infer the region from the number. |
||
| 1817 | * Returns false if it cannot use the region provided and the region cannot be inferred. |
||
| 1818 | * @param string $numberToParse |
||
| 1819 | * @param string $defaultRegion |
||
| 1820 | * @return bool |
||
| 1821 | */ |
||
| 1822 | 3057 | protected function checkRegionForParsing($numberToParse, $defaultRegion) |
|
| 1833 | |||
| 1834 | /** |
||
| 1835 | * Tries to extract a country calling code from a number. This method will return zero if no |
||
| 1836 | * country calling code is considered to be present. Country calling codes are extracted in the |
||
| 1837 | * following ways: |
||
| 1838 | * <ul> |
||
| 1839 | * <li> by stripping the international dialing prefix of the region the person is dialing from, |
||
| 1840 | * if this is present in the number, and looking at the next digits |
||
| 1841 | * <li> by stripping the '+' sign if present and then looking at the next digits |
||
| 1842 | * <li> by comparing the start of the number and the country calling code of the default region. |
||
| 1843 | * If the number is not considered possible for the numbering plan of the default region |
||
| 1844 | * initially, but starts with the country calling code of this region, validation will be |
||
| 1845 | * reattempted after stripping this country calling code. If this number is considered a |
||
| 1846 | * possible number, then the first digits will be considered the country calling code and |
||
| 1847 | * removed as such. |
||
| 1848 | * </ul> |
||
| 1849 | * It will throw a NumberParseException if the number starts with a '+' but the country calling |
||
| 1850 | * code supplied after this does not match that of any known region. |
||
| 1851 | * |
||
| 1852 | * @param string $number non-normalized telephone number that we wish to extract a country calling |
||
| 1853 | * code from - may begin with '+' |
||
| 1854 | * @param PhoneMetadata $defaultRegionMetadata metadata about the region this number may be from |
||
| 1855 | * @param string $nationalNumber a string buffer to store the national significant number in, in the case |
||
| 1856 | * that a country calling code was extracted. The number is appended to any existing contents. |
||
| 1857 | * If no country calling code was extracted, this will be left unchanged. |
||
| 1858 | * @param bool $keepRawInput true if the country_code_source and preferred_carrier_code fields of |
||
| 1859 | * phoneNumber should be populated. |
||
| 1860 | * @param PhoneNumber $phoneNumber the PhoneNumber object where the country_code and country_code_source need |
||
| 1861 | * to be populated. Note the country_code is always populated, whereas country_code_source is |
||
| 1862 | * only populated when keepCountryCodeSource is true. |
||
| 1863 | * @return int the country calling code extracted or 0 if none could be extracted |
||
| 1864 | * @throws NumberParseException |
||
| 1865 | */ |
||
| 1866 | 3057 | public function maybeExtractCountryCode( |
|
| 1944 | |||
| 1945 | /** |
||
| 1946 | * Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes |
||
| 1947 | * the resulting number, and indicates if an international prefix was present. |
||
| 1948 | * |
||
| 1949 | * @param string $number the non-normalized telephone number that we wish to strip any international |
||
| 1950 | * dialing prefix from. |
||
| 1951 | * @param string $possibleIddPrefix string the international direct dialing prefix from the region we |
||
| 1952 | * think this number may be dialed in |
||
| 1953 | * @return int the corresponding CountryCodeSource if an international dialing prefix could be |
||
| 1954 | * removed from the number, otherwise CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did |
||
| 1955 | * not seem to be in international format. |
||
| 1956 | */ |
||
| 1957 | 3058 | public function maybeStripInternationalPrefixAndNormalize(&$number, $possibleIddPrefix) |
|
| 1978 | |||
| 1979 | /** |
||
| 1980 | * Normalizes a string of characters representing a phone number. This performs |
||
| 1981 | * the following conversions: |
||
| 1982 | * Punctuation is stripped. |
||
| 1983 | * For ALPHA/VANITY numbers: |
||
| 1984 | * Letters are converted to their numeric representation on a telephone |
||
| 1985 | * keypad. The keypad used here is the one defined in ITU Recommendation |
||
| 1986 | * E.161. This is only done if there are 3 or more letters in the number, |
||
| 1987 | * to lessen the risk that such letters are typos. |
||
| 1988 | * For other numbers: |
||
| 1989 | * - Wide-ascii digits are converted to normal ASCII (European) digits. |
||
| 1990 | * - Arabic-Indic numerals are converted to European numerals. |
||
| 1991 | * - Spurious alpha characters are stripped. |
||
| 1992 | * |
||
| 1993 | * @param string $number a string of characters representing a phone number. |
||
| 1994 | * @return string the normalized string version of the phone number. |
||
| 1995 | */ |
||
| 1996 | 3062 | public static function normalize(&$number) |
|
| 2009 | |||
| 2010 | /** |
||
| 2011 | * Normalizes a string of characters representing a phone number. This converts wide-ascii and |
||
| 2012 | * arabic-indic numerals to European numerals, and strips punctuation and alpha characters. |
||
| 2013 | * |
||
| 2014 | * @param $number string a string of characters representing a phone number |
||
| 2015 | * @return string the normalized string version of the phone number |
||
| 2016 | */ |
||
| 2017 | 3081 | public static function normalizeDigitsOnly($number) |
|
| 2021 | |||
| 2022 | /** |
||
| 2023 | * @param string $number |
||
| 2024 | * @param bool $keepNonDigits |
||
| 2025 | * @return string |
||
| 2026 | */ |
||
| 2027 | 3114 | public static function normalizeDigits($number, $keepNonDigits) |
|
| 2043 | |||
| 2044 | /** |
||
| 2045 | * Strips the IDD from the start of the number if present. Helper function used by |
||
| 2046 | * maybeStripInternationalPrefixAndNormalize. |
||
| 2047 | * @param string $iddPattern |
||
| 2048 | * @param string $number |
||
| 2049 | * @return bool |
||
| 2050 | */ |
||
| 2051 | 3001 | protected function parsePrefixAsIdd($iddPattern, &$number) |
|
| 2070 | |||
| 2071 | /** |
||
| 2072 | * Extracts country calling code from fullNumber, returns it and places the remaining number in nationalNumber. |
||
| 2073 | * It assumes that the leading plus sign or IDD has already been removed. |
||
| 2074 | * Returns 0 if fullNumber doesn't start with a valid country calling code, and leaves nationalNumber unmodified. |
||
| 2075 | * @param string $fullNumber |
||
| 2076 | * @param string $nationalNumber |
||
| 2077 | * @return int |
||
| 2078 | * @internal |
||
| 2079 | */ |
||
| 2080 | 352 | public function extractCountryCode($fullNumber, &$nationalNumber) |
|
| 2096 | |||
| 2097 | /** |
||
| 2098 | * Strips any national prefix (such as 0, 1) present in the number provided. |
||
| 2099 | * |
||
| 2100 | * @param string $number the normalized telephone number that we wish to strip any national |
||
| 2101 | * dialing prefix from |
||
| 2102 | * @param PhoneMetadata $metadata the metadata for the region that we think this number is from |
||
| 2103 | * @param string $carrierCode a place to insert the carrier code if one is extracted |
||
| 2104 | * @return bool true if a national prefix or carrier code (or both) could be extracted. |
||
| 2105 | */ |
||
| 2106 | 3057 | public function maybeStripNationalPrefixAndCarrierCode(&$number, PhoneMetadata $metadata, &$carrierCode) |
|
| 2165 | |||
| 2166 | /** |
||
| 2167 | * Convenience wrapper around isPossibleNumberForTypeWithReason. Instead of returning the reason |
||
| 2168 | * reason for failure, this method returns true if the number is either a possible fully-qualified |
||
| 2169 | * number (containing the area code and country code), or if the number could be a possible local |
||
| 2170 | * number (with a country code, but missing an area code). Local numbers are considered possible |
||
| 2171 | * if they could be possibly dialled in this format: if the area code is needed for a call to |
||
| 2172 | * connect, the number is not considered possible without it. |
||
| 2173 | * |
||
| 2174 | * @param PhoneNumber $number The number that needs to be checked |
||
| 2175 | * @param int $type PhoneNumberType The type we are interested in |
||
| 2176 | * @return bool true if the number is possible for this particular type |
||
| 2177 | */ |
||
| 2178 | 4 | public function isPossibleNumberForType(PhoneNumber $number, $type) |
|
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Helper method to check a number against possible lengths for this number type, and determine |
||
| 2187 | * whether it matches, or is too short or too long. Currently, if a number pattern suggests that |
||
| 2188 | * numbers of length 7 and 10 are possible, and a number in between these possible lengths is |
||
| 2189 | * entered, such as of length 8, this will return TOO_LONG. |
||
| 2190 | * |
||
| 2191 | * @param string $number |
||
| 2192 | * @param PhoneMetadata $metadata |
||
| 2193 | * @param int $type PhoneNumberType |
||
| 2194 | * @return int ValidationResult |
||
| 2195 | */ |
||
| 2196 | 3068 | protected function testNumberLength($number, PhoneMetadata $metadata, $type = PhoneNumberType::UNKNOWN) |
|
| 2269 | |||
| 2270 | /** |
||
| 2271 | * Returns a list with the region codes that match the specific country calling code. For |
||
| 2272 | * non-geographical country calling codes, the region code 001 is returned. Also, in the case |
||
| 2273 | * of no region code being found, an empty list is returned. |
||
| 2274 | * @param int $countryCallingCode |
||
| 2275 | * @return array |
||
| 2276 | */ |
||
| 2277 | 10 | public function getRegionCodesForCountryCode($countryCallingCode) |
|
| 2282 | |||
| 2283 | /** |
||
| 2284 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 2285 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 2286 | * |
||
| 2287 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 2288 | * @return int the country calling code for the region denoted by regionCode |
||
| 2289 | */ |
||
| 2290 | 37 | public function getCountryCodeForRegion($regionCode) |
|
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 2300 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 2301 | * |
||
| 2302 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 2303 | * @return int the country calling code for the region denoted by regionCode |
||
| 2304 | * @throws \InvalidArgumentException if the region is invalid |
||
| 2305 | */ |
||
| 2306 | 1950 | protected function getCountryCodeForValidRegion($regionCode) |
|
| 2314 | |||
| 2315 | /** |
||
| 2316 | * Returns a number formatted in such a way that it can be dialed from a mobile phone in a |
||
| 2317 | * specific region. If the number cannot be reached from the region (e.g. some countries block |
||
| 2318 | * toll-free numbers from being called outside of the country), the method returns an empty |
||
| 2319 | * string. |
||
| 2320 | * |
||
| 2321 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2322 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2323 | * @param boolean $withFormatting whether the number should be returned with formatting symbols, such as |
||
| 2324 | * spaces and dashes. |
||
| 2325 | * @return string the formatted phone number |
||
| 2326 | */ |
||
| 2327 | 1 | public function formatNumberForMobileDialing(PhoneNumber $number, $regionCallingFrom, $withFormatting) |
|
| 2413 | |||
| 2414 | /** |
||
| 2415 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2416 | * {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the |
||
| 2417 | * phone number already has a preferred domestic carrier code stored. If {@code carrierCode} |
||
| 2418 | * contains an empty string, returns the number in national format without any carrier code. |
||
| 2419 | * |
||
| 2420 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2421 | * @param string $carrierCode the carrier selection code to be used |
||
| 2422 | * @return string the formatted phone number in national format for dialing using the carrier as |
||
| 2423 | * specified in the {@code carrierCode} |
||
| 2424 | */ |
||
| 2425 | 2 | public function formatNationalNumberWithCarrierCode(PhoneNumber $number, $carrierCode) |
|
| 2454 | |||
| 2455 | /** |
||
| 2456 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2457 | * preferredDomesticCarrierCode field of the PhoneNumber object passed in. If that is missing, |
||
| 2458 | * use the {@code fallbackCarrierCode} passed in instead. If there is no |
||
| 2459 | * {@code preferredDomesticCarrierCode}, and the {@code fallbackCarrierCode} contains an empty |
||
| 2460 | * string, return the number in national format without any carrier code. |
||
| 2461 | * |
||
| 2462 | * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in |
||
| 2463 | * should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting. |
||
| 2464 | * |
||
| 2465 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2466 | * @param string $fallbackCarrierCode the carrier selection code to be used, if none is found in the |
||
| 2467 | * phone number itself |
||
| 2468 | * @return string the formatted phone number in national format for dialing using the number's |
||
| 2469 | * {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if |
||
| 2470 | * none is found |
||
| 2471 | */ |
||
| 2472 | 1 | public function formatNationalNumberWithPreferredCarrierCode(PhoneNumber $number, $fallbackCarrierCode) |
|
| 2484 | |||
| 2485 | /** |
||
| 2486 | * Returns true if the number can be dialled from outside the region, or unknown. If the number |
||
| 2487 | * can only be dialled from within the region, returns false. Does not check the number is a valid |
||
| 2488 | * number. Note that, at the moment, this method does not handle short numbers (which are |
||
| 2489 | * currently all presumed to not be diallable from outside their country). |
||
| 2490 | * |
||
| 2491 | * @param PhoneNumber $number the phone-number for which we want to know whether it is diallable from outside the region |
||
| 2492 | * @return bool |
||
| 2493 | */ |
||
| 2494 | 36 | public function canBeInternationallyDialled(PhoneNumber $number) |
|
| 2505 | |||
| 2506 | /** |
||
| 2507 | * Normalizes a string of characters representing a phone number. This strips all characters which |
||
| 2508 | * are not diallable on a mobile phone keypad (including all non-ASCII digits). |
||
| 2509 | * |
||
| 2510 | * @param string $number a string of characters representing a phone number |
||
| 2511 | * @return string the normalized string version of the phone number |
||
| 2512 | */ |
||
| 2513 | 4 | public static function normalizeDiallableCharsOnly($number) |
|
| 2521 | |||
| 2522 | /** |
||
| 2523 | * Formats a phone number for out-of-country dialing purposes. |
||
| 2524 | * |
||
| 2525 | * Note that in this version, if the number was entered originally using alpha characters and |
||
| 2526 | * this version of the number is stored in raw_input, this representation of the number will be |
||
| 2527 | * used rather than the digit representation. Grouping information, as specified by characters |
||
| 2528 | * such as "-" and " ", will be retained. |
||
| 2529 | * |
||
| 2530 | * <p><b>Caveats:</b></p> |
||
| 2531 | * <ul> |
||
| 2532 | * <li> This will not produce good results if the country calling code is both present in the raw |
||
| 2533 | * input _and_ is the start of the national number. This is not a problem in the regions |
||
| 2534 | * which typically use alpha numbers. |
||
| 2535 | * <li> This will also not produce good results if the raw input has any grouping information |
||
| 2536 | * within the first three digits of the national number, and if the function needs to strip |
||
| 2537 | * preceding digits/words in the raw input before these digits. Normally people group the |
||
| 2538 | * first three digits together so this is not a huge problem - and will be fixed if it |
||
| 2539 | * proves to be so. |
||
| 2540 | * </ul> |
||
| 2541 | * |
||
| 2542 | * @param PhoneNumber $number the phone number that needs to be formatted |
||
| 2543 | * @param String $regionCallingFrom the region where the call is being placed |
||
| 2544 | * @return String the formatted phone number |
||
| 2545 | */ |
||
| 2546 | 1 | public function formatOutOfCountryKeepingAlphaChars(PhoneNumber $number, $regionCallingFrom) |
|
| 2639 | |||
| 2640 | /** |
||
| 2641 | * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is |
||
| 2642 | * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the |
||
| 2643 | * same as that of the region where the number is from, then NATIONAL formatting will be applied. |
||
| 2644 | * |
||
| 2645 | * <p>If the number itself has a country calling code of zero or an otherwise invalid country |
||
| 2646 | * calling code, then we return the number with no formatting applied. |
||
| 2647 | * |
||
| 2648 | * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and |
||
| 2649 | * Kazakhstan (who share the same country calling code). In those cases, no international prefix |
||
| 2650 | * is used. For regions which have multiple international prefixes, the number in its |
||
| 2651 | * INTERNATIONAL format will be returned instead. |
||
| 2652 | * |
||
| 2653 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2654 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2655 | * @return string the formatted phone number |
||
| 2656 | */ |
||
| 2657 | 8 | public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCallingFrom) |
|
| 2724 | |||
| 2725 | /** |
||
| 2726 | * Checks if this is a region under the North American Numbering Plan Administration (NANPA). |
||
| 2727 | * @param string $regionCode |
||
| 2728 | * @return boolean true if regionCode is one of the regions under NANPA |
||
| 2729 | */ |
||
| 2730 | 5 | public function isNANPACountry($regionCode) |
|
| 2734 | |||
| 2735 | /** |
||
| 2736 | * Formats a phone number using the original phone number format that the number is parsed from. |
||
| 2737 | * The original format is embedded in the country_code_source field of the PhoneNumber object |
||
| 2738 | * passed in. If such information is missing, the number will be formatted into the NATIONAL |
||
| 2739 | * format by default. When we don't have a formatting pattern for the number, the method returns |
||
| 2740 | * the raw inptu when it is available. |
||
| 2741 | * |
||
| 2742 | * Note this method guarantees no digit will be inserted, removed or modified as a result of |
||
| 2743 | * formatting. |
||
| 2744 | * |
||
| 2745 | * @param PhoneNumber $number the phone number that needs to be formatted in its original number format |
||
| 2746 | * @param string $regionCallingFrom the region whose IDD needs to be prefixed if the original number |
||
| 2747 | * has one |
||
| 2748 | * @return string the formatted phone number in its original number format |
||
| 2749 | */ |
||
| 2750 | 1 | public function formatInOriginalFormat(PhoneNumber $number, $regionCallingFrom) |
|
| 2846 | |||
| 2847 | /** |
||
| 2848 | * @param PhoneNumber $number |
||
| 2849 | * @return bool |
||
| 2850 | */ |
||
| 2851 | 1 | protected function hasFormattingPatternForNumber(PhoneNumber $number) |
|
| 2863 | |||
| 2864 | /** |
||
| 2865 | * Returns the national dialling prefix for a specific region. For example, this would be 1 for |
||
| 2866 | * the United States, and 0 for New Zealand. Set stripNonDigits to true to strip symbols like "~" |
||
| 2867 | * (which indicates a wait for a dialling tone) from the prefix returned. If no national prefix is |
||
| 2868 | * present, we return null. |
||
| 2869 | * |
||
| 2870 | * <p>Warning: Do not use this method for do-your-own formatting - for some regions, the |
||
| 2871 | * national dialling prefix is used only for certain types of numbers. Use the library's |
||
| 2872 | * formatting functions to prefix the national prefix when required. |
||
| 2873 | * |
||
| 2874 | * @param string $regionCode the region that we want to get the dialling prefix for |
||
| 2875 | * @param boolean $stripNonDigits true to strip non-digits from the national dialling prefix |
||
| 2876 | * @return string the dialling prefix for the region denoted by regionCode |
||
| 2877 | */ |
||
| 2878 | 28 | public function getNddPrefixForRegion($regionCode, $stripNonDigits) |
|
| 2896 | |||
| 2897 | /** |
||
| 2898 | * Check if rawInput, which is assumed to be in the national format, has a national prefix. The |
||
| 2899 | * national prefix is assumed to be in digits-only form. |
||
| 2900 | * @param string $rawInput |
||
| 2901 | * @param string $nationalPrefix |
||
| 2902 | * @param string $regionCode |
||
| 2903 | * @return bool |
||
| 2904 | */ |
||
| 2905 | 1 | protected function rawInputContainsNationalPrefix($rawInput, $nationalPrefix, $regionCode) |
|
| 2923 | |||
| 2924 | /** |
||
| 2925 | * Tests whether a phone number matches a valid pattern. Note this doesn't verify the number |
||
| 2926 | * is actually in use, which is impossible to tell by just looking at a number itself. It only |
||
| 2927 | * verifies whether the parsed, canonicalised number is valid: not whether a particular series of |
||
| 2928 | * digits entered by the user is diallable from the region provided when parsing. For example, the |
||
| 2929 | * number +41 (0) 78 927 2696 can be parsed into a number with country code "41" and national |
||
| 2930 | * significant number "789272696". This is valid, while the original string is not diallable. |
||
| 2931 | * |
||
| 2932 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2933 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2934 | */ |
||
| 2935 | 1972 | public function isValidNumber(PhoneNumber $number) |
|
| 2940 | |||
| 2941 | /** |
||
| 2942 | * Tests whether a phone number is valid for a certain region. Note this doesn't verify the number |
||
| 2943 | * is actually in use, which is impossible to tell by just looking at a number itself. If the |
||
| 2944 | * country calling code is not the same as the country calling code for the region, this |
||
| 2945 | * immediately exits with false. After this, the specific number pattern rules for the region are |
||
| 2946 | * examined. This is useful for determining for example whether a particular number is valid for |
||
| 2947 | * Canada, rather than just a valid NANPA number. |
||
| 2948 | * Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this |
||
| 2949 | * method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for |
||
| 2950 | * the region "GB" (United Kingdom), since it has its own region code, "IM", which may be |
||
| 2951 | * undesirable. |
||
| 2952 | * |
||
| 2953 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2954 | * @param string $regionCode the region that we want to validate the phone number for |
||
| 2955 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2956 | */ |
||
| 2957 | 1978 | public function isValidNumberForRegion(PhoneNumber $number, $regionCode) |
|
| 2973 | |||
| 2974 | /** |
||
| 2975 | * Parses a string and returns it as a phone number in proto buffer format. The method is quite |
||
| 2976 | * lenient and looks for a number in the input text (raw input) and does not check whether the |
||
| 2977 | * string is definitely only a phone number. To do this, it ignores punctuation and white-space, |
||
| 2978 | * as well as any text before the number (e.g. a leading “Tel: ”) and trims the non-number bits. |
||
| 2979 | * It will accept a number in any format (E164, national, international etc), assuming it can |
||
| 2980 | * interpreted with the defaultRegion supplied. It also attempts to convert any alpha characters |
||
| 2981 | * into digits if it thinks this is a vanity number of the type "1800 MICROSOFT". |
||
| 2982 | * |
||
| 2983 | * <p> This method will throw a {@link NumberParseException} if the number is not considered to |
||
| 2984 | * be a possible number. Note that validation of whether the number is actually a valid number |
||
| 2985 | * for a particular region is not performed. This can be done separately with {@link #isValidNumber}. |
||
| 2986 | * |
||
| 2987 | * <p> Note this method canonicalizes the phone number such that different representations can be |
||
| 2988 | * easily compared, no matter what form it was originally entered in (e.g. national, |
||
| 2989 | * international). If you want to record context about the number being parsed, such as the raw |
||
| 2990 | * input that was entered, how the country code was derived etc. then call {@link |
||
| 2991 | * #parseAndKeepRawInput} instead. |
||
| 2992 | * |
||
| 2993 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 2994 | * such as +, ( and -, as well as a phone number extension. |
||
| 2995 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 2996 | * if the number being parsed is not written in international format. |
||
| 2997 | * The country_code for the number in this case would be stored as that |
||
| 2998 | * of the default region supplied. If the number is guaranteed to |
||
| 2999 | * start with a '+' followed by the country calling code, then |
||
| 3000 | * "ZZ" or null can be supplied. |
||
| 3001 | * @param PhoneNumber|null $phoneNumber |
||
| 3002 | * @param bool $keepRawInput |
||
| 3003 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 3004 | * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. |
||
| 3005 | * too few or too many digits) or if no default region was supplied |
||
| 3006 | * and the number is not in international format (does not start |
||
| 3007 | * with +) |
||
| 3008 | */ |
||
| 3009 | 2896 | public function parse($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null, $keepRawInput = false) |
|
| 3017 | |||
| 3018 | /** |
||
| 3019 | * Formats a phone number in the specified format using client-defined formatting rules. Note that |
||
| 3020 | * if the phone number has a country calling code of zero or an otherwise invalid country calling |
||
| 3021 | * code, we cannot work out things like whether there should be a national prefix applied, or how |
||
| 3022 | * to format extensions, so we return the national significant number with no formatting applied. |
||
| 3023 | * |
||
| 3024 | * @param PhoneNumber $number the phone number to be formatted |
||
| 3025 | * @param int $numberFormat the format the phone number should be formatted into |
||
| 3026 | * @param array $userDefinedFormats formatting rules specified by clients |
||
| 3027 | * @return String the formatted phone number |
||
| 3028 | */ |
||
| 3029 | 2 | public function formatByPattern(PhoneNumber $number, $numberFormat, array $userDefinedFormats) |
|
| 3074 | |||
| 3075 | /** |
||
| 3076 | * Gets a valid number for the specified region. |
||
| 3077 | * |
||
| 3078 | * @param string regionCode the region for which an example number is needed |
||
| 3079 | * @return PhoneNumber a valid fixed-line number for the specified region. Returns null when the metadata |
||
| 3080 | * does not contain such information, or the region 001 is passed in. For 001 (representing |
||
| 3081 | * non-geographical numbers), call {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 3082 | */ |
||
| 3083 | 247 | public function getExampleNumber($regionCode) |
|
| 3087 | |||
| 3088 | /** |
||
| 3089 | * Gets an invalid number for the specified region. This is useful for unit-testing purposes, |
||
| 3090 | * where you want to test what will happen with an invalid number. Note that the number that is |
||
| 3091 | * returned will always be able to be parsed and will have the correct country code. It may also |
||
| 3092 | * be a valid *short* number/code for this region. Validity checking such numbers is handled with |
||
| 3093 | * {@link ShortNumberInfo}. |
||
| 3094 | * |
||
| 3095 | * @param string $regionCode The region for which an example number is needed |
||
| 3096 | * @return PhoneNumber|null An invalid number for the specified region. Returns null when an unsupported region |
||
| 3097 | * or the region 001 (Earth) is passed in. |
||
| 3098 | */ |
||
| 3099 | 244 | public function getInvalidExampleNumber($regionCode) |
|
| 3145 | |||
| 3146 | /** |
||
| 3147 | * Gets a valid number for the specified region and number type. |
||
| 3148 | * |
||
| 3149 | * @param string|int $regionCodeOrType the region for which an example number is needed |
||
| 3150 | * @param int $type the PhoneNumberType of number that is needed |
||
| 3151 | * @return PhoneNumber a valid number for the specified region and type. Returns null when the metadata |
||
| 3152 | * does not contain such information or if an invalid region or region 001 was entered. |
||
| 3153 | * For 001 (representing non-geographical numbers), call |
||
| 3154 | * {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 3155 | * |
||
| 3156 | * If $regionCodeOrType is the only parameter supplied, then a valid number for the specified number type |
||
| 3157 | * will be returned that may belong to any country. |
||
| 3158 | */ |
||
| 3159 | 3176 | public function getExampleNumberForType($regionCodeOrType, $type = null) |
|
| 3201 | |||
| 3202 | /** |
||
| 3203 | * @param PhoneMetadata $metadata |
||
| 3204 | * @param int $type PhoneNumberType |
||
| 3205 | * @return PhoneNumberDesc |
||
| 3206 | */ |
||
| 3207 | 4443 | protected function getNumberDescByType(PhoneMetadata $metadata, $type) |
|
| 3235 | |||
| 3236 | /** |
||
| 3237 | * Gets a valid number for the specified country calling code for a non-geographical entity. |
||
| 3238 | * |
||
| 3239 | * @param int $countryCallingCode the country calling code for a non-geographical entity |
||
| 3240 | * @return PhoneNumber a valid number for the non-geographical entity. Returns null when the metadata |
||
| 3241 | * does not contain such information, or the country calling code passed in does not belong |
||
| 3242 | * to a non-geographical entity. |
||
| 3243 | */ |
||
| 3244 | 10 | public function getExampleNumberForNonGeoEntity($countryCallingCode) |
|
| 3274 | |||
| 3275 | |||
| 3276 | /** |
||
| 3277 | * Takes two phone numbers and compares them for equality. |
||
| 3278 | * |
||
| 3279 | * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero |
||
| 3280 | * for Italian numbers and any extension present are the same. Returns NSN_MATCH |
||
| 3281 | * if either or both has no region specified, and the NSNs and extensions are |
||
| 3282 | * the same. Returns SHORT_NSN_MATCH if either or both has no region specified, |
||
| 3283 | * or the region specified is the same, and one NSN could be a shorter version |
||
| 3284 | * of the other number. This includes the case where one has an extension |
||
| 3285 | * specified, and the other does not. Returns NO_MATCH otherwise. For example, |
||
| 3286 | * the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH. The numbers |
||
| 3287 | * +1 345 657 1234 and 345 657 are a NO_MATCH. |
||
| 3288 | * |
||
| 3289 | * @param $firstNumberIn PhoneNumber|string First number to compare. If it is a |
||
| 3290 | * string it can contain formatting, and can have country calling code specified |
||
| 3291 | * with + at the start. |
||
| 3292 | * @param $secondNumberIn PhoneNumber|string Second number to compare. If it is a |
||
| 3293 | * string it can contain formatting, and can have country calling code specified |
||
| 3294 | * with + at the start. |
||
| 3295 | * @throws \InvalidArgumentException |
||
| 3296 | * @return int {MatchType} NOT_A_NUMBER, NO_MATCH, |
||
| 3297 | */ |
||
| 3298 | 8 | public function isNumberMatch($firstNumberIn, $secondNumberIn) |
|
| 3403 | |||
| 3404 | /** |
||
| 3405 | * Returns true when one national number is the suffix of the other or both are the same. |
||
| 3406 | * @param PhoneNumber $firstNumber |
||
| 3407 | * @param PhoneNumber $secondNumber |
||
| 3408 | * @return bool |
||
| 3409 | */ |
||
| 3410 | 4 | protected function isNationalNumberSuffixOfTheOther(PhoneNumber $firstNumber, PhoneNumber $secondNumber) |
|
| 3417 | |||
| 3418 | 4 | protected function stringEndsWithString($hayStack, $needle) |
|
| 3424 | |||
| 3425 | /** |
||
| 3426 | * Returns true if the supplied region supports mobile number portability. Returns false for |
||
| 3427 | * invalid, unknown or regions that don't support mobile number portability. |
||
| 3428 | * |
||
| 3429 | * @param string $regionCode the region for which we want to know whether it supports mobile number |
||
| 3430 | * portability or not. |
||
| 3431 | * @return bool |
||
| 3432 | */ |
||
| 3433 | 3 | public function isMobileNumberPortableRegion($regionCode) |
|
| 3442 | |||
| 3443 | /** |
||
| 3444 | * Check whether a phone number is a possible number given a number in the form of a string, and |
||
| 3445 | * the region where the number could be dialed from. It provides a more lenient check than |
||
| 3446 | * {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details. |
||
| 3447 | * |
||
| 3448 | * Convenience wrapper around {@link #isPossibleNumberWithReason}. Instead of returning the reason |
||
| 3449 | * for failure, this method returns a boolean value. |
||
| 3450 | * for failure, this method returns true if the number is either a possible fully-qualified number |
||
| 3451 | * (containing the area code and country code), or if the number could be a possible local number |
||
| 3452 | * (with a country code, but missing an area code). Local numbers are considered possible if they |
||
| 3453 | * could be possibly dialled in this format: if the area code is needed for a call to connect, the |
||
| 3454 | * number is not considered possible without it. |
||
| 3455 | * |
||
| 3456 | * Note: There are two ways to call this method. |
||
| 3457 | * |
||
| 3458 | * isPossibleNumber(PhoneNumber $numberObject) |
||
| 3459 | * isPossibleNumber(string '+441174960126', string 'GB') |
||
| 3460 | * |
||
| 3461 | * @param PhoneNumber|string $number the number that needs to be checked, in the form of a string |
||
| 3462 | * @param string|null $regionDialingFrom the region that we are expecting the number to be dialed from. |
||
| 3463 | * Note this is different from the region where the number belongs. For example, the number |
||
| 3464 | * +1 650 253 0000 is a number that belongs to US. When written in this form, it can be |
||
| 3465 | * dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any |
||
| 3466 | * region which uses an international dialling prefix of 00. When it is written as |
||
| 3467 | * 650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it |
||
| 3468 | * can only be dialed from within a smaller area in the US (Mountain View, CA, to be more |
||
| 3469 | * specific). |
||
| 3470 | * @return boolean true if the number is possible |
||
| 3471 | */ |
||
| 3472 | 57 | public function isPossibleNumber($number, $regionDialingFrom = null) |
|
| 3486 | |||
| 3487 | |||
| 3488 | /** |
||
| 3489 | * Check whether a phone number is a possible number. It provides a more lenient check than |
||
| 3490 | * {@link #isValidNumber} in the following sense: |
||
| 3491 | * <ol> |
||
| 3492 | * <li> It only checks the length of phone numbers. In particular, it doesn't check starting |
||
| 3493 | * digits of the number. |
||
| 3494 | * <li> It doesn't attempt to figure out the type of the number, but uses general rules which |
||
| 3495 | * applies to all types of phone numbers in a region. Therefore, it is much faster than |
||
| 3496 | * isValidNumber. |
||
| 3497 | * <li> For some numbers (particularly fixed-line), many regions have the concept of area code, |
||
| 3498 | * which together with subscriber number constitute the national significant number. It is |
||
| 3499 | * sometimes okay to dial only the subscriber number when dialing in the same area. This |
||
| 3500 | * function will return IS_POSSIBLE_LOCAL_ONLY if the subscriber-number-only version is |
||
| 3501 | * passed in. On the other hand, because isValidNumber validates using information on both |
||
| 3502 | * starting digits (for fixed line numbers, that would most likely be area codes) and |
||
| 3503 | * length (obviously includes the length of area codes for fixed line numbers), it will |
||
| 3504 | * return false for the subscriber-number-only version. |
||
| 3505 | * </ol> |
||
| 3506 | * @param PhoneNumber $number the number that needs to be checked |
||
| 3507 | * @return int a ValidationResult object which indicates whether the number is possible |
||
| 3508 | */ |
||
| 3509 | 59 | public function isPossibleNumberWithReason(PhoneNumber $number) |
|
| 3513 | |||
| 3514 | /** |
||
| 3515 | * Check whether a phone number is a possible number of a particular type. For types that don't |
||
| 3516 | * exist in a particular region, this will return a result that isn't so useful; it is recommended |
||
| 3517 | * that you use {@link #getSupportedTypesForRegion} or {@link #getSupportedTypesForNonGeoEntity} |
||
| 3518 | * respectively before calling this method to determine whether you should call it for this number |
||
| 3519 | * at all. |
||
| 3520 | * |
||
| 3521 | * This provides a more lenient check than {@link #isValidNumber} in the following sense: |
||
| 3522 | * |
||
| 3523 | * <ol> |
||
| 3524 | * <li> It only checks the length of phone numbers. In particular, it doesn't check starting |
||
| 3525 | * digits of the number. |
||
| 3526 | * <li> For some numbers (particularly fixed-line), many regions have the concept of area code, |
||
| 3527 | * which together with subscriber number constitute the national significant number. It is |
||
| 3528 | * sometimes okay to dial only the subscriber number when dialing in the same area. This |
||
| 3529 | * function will return IS_POSSIBLE_LOCAL_ONLY if the subscriber-number-only version is |
||
| 3530 | * passed in. On the other hand, because isValidNumber validates using information on both |
||
| 3531 | * starting digits (for fixed line numbers, that would most likely be area codes) and |
||
| 3532 | * length (obviously includes the length of area codes for fixed line numbers), it will |
||
| 3533 | * return false for the subscriber-number-only version. |
||
| 3534 | * </ol> |
||
| 3535 | * |
||
| 3536 | * @param PhoneNumber $number the number that needs to be checked |
||
| 3537 | * @param int $type the PhoneNumberType we are interested in |
||
| 3538 | * @return int a ValidationResult object which indicates whether the number is possible |
||
| 3539 | */ |
||
| 3540 | 68 | public function isPossibleNumberForTypeWithReason(PhoneNumber $number, $type) |
|
| 3560 | |||
| 3561 | /** |
||
| 3562 | * Attempts to extract a valid number from a phone number that is too long to be valid, and resets |
||
| 3563 | * the PhoneNumber object passed in to that valid version. If no valid number could be extracted, |
||
| 3564 | * the PhoneNumber object passed in will not be modified. |
||
| 3565 | * @param PhoneNumber $number a PhoneNumber object which contains a number that is too long to be valid. |
||
| 3566 | * @return boolean true if a valid phone number can be successfully extracted. |
||
| 3567 | */ |
||
| 3568 | 1 | public function truncateTooLongNumber(PhoneNumber $number) |
|
| 3586 | } |
||
| 3587 |
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: