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 |
||
| 21 | class PhoneNumberUtil |
||
| 22 | { |
||
| 23 | /** Flags to use when compiling regular expressions for phone numbers */ |
||
| 24 | const REGEX_FLAGS = 'ui'; //Unicode and case insensitive |
||
| 25 | // The minimum and maximum length of the national significant number. |
||
| 26 | const MIN_LENGTH_FOR_NSN = 2; |
||
| 27 | // The ITU says the maximum length should be 15, but we have found longer numbers in Germany. |
||
| 28 | const MAX_LENGTH_FOR_NSN = 17; |
||
| 29 | |||
| 30 | // We don't allow input strings for parsing to be longer than 250 chars. This prevents malicious |
||
| 31 | // input from overflowing the regular-expression engine. |
||
| 32 | const MAX_INPUT_STRING_LENGTH = 250; |
||
| 33 | |||
| 34 | // The maximum length of the country calling code. |
||
| 35 | const MAX_LENGTH_COUNTRY_CODE = 3; |
||
| 36 | |||
| 37 | const REGION_CODE_FOR_NON_GEO_ENTITY = "001"; |
||
| 38 | const META_DATA_FILE_PREFIX = 'PhoneNumberMetadata'; |
||
| 39 | const TEST_META_DATA_FILE_PREFIX = 'PhoneNumberMetadataForTesting'; |
||
| 40 | |||
| 41 | // Region-code for the unknown region. |
||
| 42 | const UNKNOWN_REGION = "ZZ"; |
||
| 43 | |||
| 44 | const NANPA_COUNTRY_CODE = 1; |
||
| 45 | /* |
||
| 46 | * The prefix that needs to be inserted in front of a Colombian landline number when dialed from |
||
| 47 | * a mobile number in Colombia. |
||
| 48 | */ |
||
| 49 | const COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX = "3"; |
||
| 50 | // The PLUS_SIGN signifies the international prefix. |
||
| 51 | const PLUS_SIGN = '+'; |
||
| 52 | const PLUS_CHARS = '++'; |
||
| 53 | const STAR_SIGN = '*'; |
||
| 54 | |||
| 55 | const RFC3966_EXTN_PREFIX = ";ext="; |
||
| 56 | const RFC3966_PREFIX = "tel:"; |
||
| 57 | const RFC3966_PHONE_CONTEXT = ";phone-context="; |
||
| 58 | const RFC3966_ISDN_SUBADDRESS = ";isub="; |
||
| 59 | |||
| 60 | // We use this pattern to check if the phone number has at least three letters in it - if so, then |
||
| 61 | // we treat it as a number where some phone-number digits are represented by letters. |
||
| 62 | const VALID_ALPHA_PHONE_PATTERN = "(?:.*?[A-Za-z]){3}.*"; |
||
| 63 | // We accept alpha characters in phone numbers, ASCII only, upper and lower case. |
||
| 64 | const VALID_ALPHA = "A-Za-z"; |
||
| 65 | |||
| 66 | |||
| 67 | // Default extension prefix to use when formatting. This will be put in front of any extension |
||
| 68 | // component of the number, after the main national number is formatted. For example, if you wish |
||
| 69 | // the default extension formatting to be " extn: 3456", then you should specify " extn: " here |
||
| 70 | // as the default extension prefix. This can be overridden by region-specific preferences. |
||
| 71 | const DEFAULT_EXTN_PREFIX = " ext. "; |
||
| 72 | |||
| 73 | // Regular expression of acceptable punctuation found in phone numbers. This excludes punctuation |
||
| 74 | // found as a leading character only. |
||
| 75 | // This consists of dash characters, white space characters, full stops, slashes, |
||
| 76 | // square brackets, parentheses and tildes. It also includes the letter 'x' as that is found as a |
||
| 77 | // placeholder for carrier information in some phone numbers. Full-width variants are also |
||
| 78 | // present. |
||
| 79 | 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"; |
||
| 80 | const DIGITS = "\\p{Nd}"; |
||
| 81 | |||
| 82 | // Pattern that makes it easy to distinguish whether a region has a unique international dialing |
||
| 83 | // prefix or not. If a region has a unique international prefix (e.g. 011 in USA), it will be |
||
| 84 | // represented as a string that contains a sequence of ASCII digits. If there are multiple |
||
| 85 | // available international prefixes in a region, they will be represented as a regex string that |
||
| 86 | // always contains character(s) other than ASCII digits. |
||
| 87 | // Note this regex also includes tilde, which signals waiting for the tone. |
||
| 88 | const UNIQUE_INTERNATIONAL_PREFIX = "[\\d]+(?:[~\xE2\x81\x93\xE2\x88\xBC\xEF\xBD\x9E][\\d]+)?"; |
||
| 89 | const NON_DIGITS_PATTERN = "(\\D+)"; |
||
| 90 | |||
| 91 | // The FIRST_GROUP_PATTERN was originally set to $1 but there are some countries for which the |
||
| 92 | // first group is not used in the national pattern (e.g. Argentina) so the $1 group does not match |
||
| 93 | // correctly. Therefore, we use \d, so that the first group actually used in the pattern will be |
||
| 94 | // matched. |
||
| 95 | const FIRST_GROUP_PATTERN = "(\\$\\d)"; |
||
| 96 | const NP_PATTERN = '\\$NP'; |
||
| 97 | const FG_PATTERN = '\\$FG'; |
||
| 98 | const CC_PATTERN = '\\$CC'; |
||
| 99 | |||
| 100 | // A pattern that is used to determine if the national prefix formatting rule has the first group |
||
| 101 | // only, i.e., does not start with the national prefix. Note that the pattern explicitly allows |
||
| 102 | // for unbalanced parentheses. |
||
| 103 | const FIRST_GROUP_ONLY_PREFIX_PATTERN = '\\(?\\$1\\)?'; |
||
| 104 | public static $PLUS_CHARS_PATTERN; |
||
| 105 | protected static $SEPARATOR_PATTERN; |
||
| 106 | protected static $CAPTURING_DIGIT_PATTERN; |
||
| 107 | protected static $VALID_START_CHAR_PATTERN = null; |
||
| 108 | protected static $SECOND_NUMBER_START_PATTERN = "[\\\\/] *x"; |
||
| 109 | protected static $UNWANTED_END_CHAR_PATTERN = "[[\\P{N}&&\\P{L}]&&[^#]]+$"; |
||
| 110 | protected static $DIALLABLE_CHAR_MAPPINGS = array(); |
||
| 111 | protected static $CAPTURING_EXTN_DIGITS; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var PhoneNumberUtil |
||
| 115 | */ |
||
| 116 | protected static $instance = null; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Only upper-case variants of alpha characters are stored. |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | protected static $ALPHA_MAPPINGS = array( |
||
| 123 | 'A' => '2', |
||
| 124 | 'B' => '2', |
||
| 125 | 'C' => '2', |
||
| 126 | 'D' => '3', |
||
| 127 | 'E' => '3', |
||
| 128 | 'F' => '3', |
||
| 129 | 'G' => '4', |
||
| 130 | 'H' => '4', |
||
| 131 | 'I' => '4', |
||
| 132 | 'J' => '5', |
||
| 133 | 'K' => '5', |
||
| 134 | 'L' => '5', |
||
| 135 | 'M' => '6', |
||
| 136 | 'N' => '6', |
||
| 137 | 'O' => '6', |
||
| 138 | 'P' => '7', |
||
| 139 | 'Q' => '7', |
||
| 140 | 'R' => '7', |
||
| 141 | 'S' => '7', |
||
| 142 | 'T' => '8', |
||
| 143 | 'U' => '8', |
||
| 144 | 'V' => '8', |
||
| 145 | 'W' => '9', |
||
| 146 | 'X' => '9', |
||
| 147 | 'Y' => '9', |
||
| 148 | 'Z' => '9', |
||
| 149 | ); |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Map of country calling codes that use a mobile token before the area code. One example of when |
||
| 153 | * this is relevant is when determining the length of the national destination code, which should |
||
| 154 | * be the length of the area code plus the length of the mobile token. |
||
| 155 | * @var array |
||
| 156 | */ |
||
| 157 | protected static $MOBILE_TOKEN_MAPPINGS; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set of country codes that have geographically assigned mobile numbers (see GEO_MOBILE_COUNTRIES |
||
| 161 | * below) which are not based on *area codes*. For example, in China mobile numbers start with a |
||
| 162 | * carrier indicator, and beyond that are geographically assigned: this carrier indicator is not |
||
| 163 | * considered to be an area code. |
||
| 164 | * |
||
| 165 | * @var array |
||
| 166 | */ |
||
| 167 | protected static $GEO_MOBILE_COUNTRIES_WITHOUT_MOBILE_AREA_CODES; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set of country calling codes that have geographically assigned mobile numbers. This may not be |
||
| 171 | * complete; we add calling codes case by case, as we find geographical mobile numbers or hear |
||
| 172 | * from user reports. Note that countries like the US, where we can't distinguish between |
||
| 173 | * fixed-line or mobile numbers, are not listed here, since we consider FIXED_LINE_OR_MOBILE to be |
||
| 174 | * a possibly geographically-related type anyway (like FIXED_LINE). |
||
| 175 | * |
||
| 176 | * @var array |
||
| 177 | */ |
||
| 178 | protected static $GEO_MOBILE_COUNTRIES; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * For performance reasons, amalgamate both into one map. |
||
| 182 | * @var array |
||
| 183 | */ |
||
| 184 | protected static $ALPHA_PHONE_MAPPINGS; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Separate map of all symbols that we wish to retain when formatting alpha numbers. This |
||
| 188 | * includes digits, ASCII letters and number grouping symbols such as "-" and " ". |
||
| 189 | * @var array |
||
| 190 | */ |
||
| 191 | protected static $ALL_PLUS_NUMBER_GROUPING_SYMBOLS; |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and |
||
| 195 | * ALL_PLUS_NUMBER_GROUPING_SYMBOLS. |
||
| 196 | * @var array |
||
| 197 | */ |
||
| 198 | protected static $asciiDigitMappings = array( |
||
| 199 | '0' => '0', |
||
| 200 | '1' => '1', |
||
| 201 | '2' => '2', |
||
| 202 | '3' => '3', |
||
| 203 | '4' => '4', |
||
| 204 | '5' => '5', |
||
| 205 | '6' => '6', |
||
| 206 | '7' => '7', |
||
| 207 | '8' => '8', |
||
| 208 | '9' => '9', |
||
| 209 | ); |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Regexp of all possible ways to write extensions, for use when parsing. This will be run as a |
||
| 213 | * case-insensitive regexp match. Wide character versions are also provided after each ASCII |
||
| 214 | * version. |
||
| 215 | * @var String |
||
| 216 | */ |
||
| 217 | protected static $EXTN_PATTERNS_FOR_PARSING; |
||
| 218 | protected static $EXTN_PATTERN = null; |
||
| 219 | protected static $VALID_PHONE_NUMBER_PATTERN; |
||
| 220 | protected static $MIN_LENGTH_PHONE_NUMBER_PATTERN; |
||
| 221 | /** |
||
| 222 | * Regular expression of viable phone numbers. This is location independent. Checks we have at |
||
| 223 | * least three leading digits, and only valid punctuation, alpha characters and |
||
| 224 | * digits in the phone number. Does not include extension data. |
||
| 225 | * The symbol 'x' is allowed here as valid punctuation since it is often used as a placeholder for |
||
| 226 | * carrier codes, for example in Brazilian phone numbers. We also allow multiple "+" characters at |
||
| 227 | * the start. |
||
| 228 | * Corresponds to the following: |
||
| 229 | * [digits]{minLengthNsn}| |
||
| 230 | * plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])* |
||
| 231 | * |
||
| 232 | * The first reg-ex is to allow short numbers (two digits long) to be parsed if they are entered |
||
| 233 | * as "15" etc, but only if there is no punctuation in them. The second expression restricts the |
||
| 234 | * number of digits to three or more, but then allows them to be in international form, and to |
||
| 235 | * have alpha-characters and punctuation. |
||
| 236 | * |
||
| 237 | * Note VALID_PUNCTUATION starts with a -, so must be the first in the range. |
||
| 238 | * @var string |
||
| 239 | */ |
||
| 240 | protected static $VALID_PHONE_NUMBER; |
||
| 241 | protected static $numericCharacters = array( |
||
| 242 | "\xef\xbc\x90" => 0, |
||
| 243 | "\xef\xbc\x91" => 1, |
||
| 244 | "\xef\xbc\x92" => 2, |
||
| 245 | "\xef\xbc\x93" => 3, |
||
| 246 | "\xef\xbc\x94" => 4, |
||
| 247 | "\xef\xbc\x95" => 5, |
||
| 248 | "\xef\xbc\x96" => 6, |
||
| 249 | "\xef\xbc\x97" => 7, |
||
| 250 | "\xef\xbc\x98" => 8, |
||
| 251 | "\xef\xbc\x99" => 9, |
||
| 252 | |||
| 253 | "\xd9\xa0" => 0, |
||
| 254 | "\xd9\xa1" => 1, |
||
| 255 | "\xd9\xa2" => 2, |
||
| 256 | "\xd9\xa3" => 3, |
||
| 257 | "\xd9\xa4" => 4, |
||
| 258 | "\xd9\xa5" => 5, |
||
| 259 | "\xd9\xa6" => 6, |
||
| 260 | "\xd9\xa7" => 7, |
||
| 261 | "\xd9\xa8" => 8, |
||
| 262 | "\xd9\xa9" => 9, |
||
| 263 | |||
| 264 | "\xdb\xb0" => 0, |
||
| 265 | "\xdb\xb1" => 1, |
||
| 266 | "\xdb\xb2" => 2, |
||
| 267 | "\xdb\xb3" => 3, |
||
| 268 | "\xdb\xb4" => 4, |
||
| 269 | "\xdb\xb5" => 5, |
||
| 270 | "\xdb\xb6" => 6, |
||
| 271 | "\xdb\xb7" => 7, |
||
| 272 | "\xdb\xb8" => 8, |
||
| 273 | "\xdb\xb9" => 9, |
||
| 274 | |||
| 275 | "\xe1\xa0\x90" => 0, |
||
| 276 | "\xe1\xa0\x91" => 1, |
||
| 277 | "\xe1\xa0\x92" => 2, |
||
| 278 | "\xe1\xa0\x93" => 3, |
||
| 279 | "\xe1\xa0\x94" => 4, |
||
| 280 | "\xe1\xa0\x95" => 5, |
||
| 281 | "\xe1\xa0\x96" => 6, |
||
| 282 | "\xe1\xa0\x97" => 7, |
||
| 283 | "\xe1\xa0\x98" => 8, |
||
| 284 | "\xe1\xa0\x99" => 9, |
||
| 285 | ); |
||
| 286 | |||
| 287 | /** |
||
| 288 | * The set of county calling codes that map to the non-geo entity region ("001"). |
||
| 289 | * @var array |
||
| 290 | */ |
||
| 291 | protected $countryCodesForNonGeographicalRegion = array(); |
||
| 292 | /** |
||
| 293 | * The set of regions the library supports. |
||
| 294 | * @var array |
||
| 295 | */ |
||
| 296 | protected $supportedRegions = array(); |
||
| 297 | |||
| 298 | /** |
||
| 299 | * A mapping from a country calling code to the region codes which denote the region represented |
||
| 300 | * by that country calling code. In the case of multiple regions sharing a calling code, such as |
||
| 301 | * the NANPA regions, the one indicated with "isMainCountryForCode" in the metadata should be |
||
| 302 | * first. |
||
| 303 | * @var array |
||
| 304 | */ |
||
| 305 | protected $countryCallingCodeToRegionCodeMap = array(); |
||
| 306 | /** |
||
| 307 | * The set of regions that share country calling code 1. |
||
| 308 | * @var array |
||
| 309 | */ |
||
| 310 | protected $nanpaRegions = array(); |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @var MetadataSourceInterface |
||
| 314 | */ |
||
| 315 | protected $metadataSource; |
||
| 316 | |||
| 317 | /** |
||
| 318 | * This class implements a singleton, so the only constructor is protected. |
||
| 319 | * @param MetadataSourceInterface $metadataSource |
||
| 320 | * @param $countryCallingCodeToRegionCodeMap |
||
| 321 | */ |
||
| 322 | 403 | protected function __construct(MetadataSourceInterface $metadataSource, $countryCallingCodeToRegionCodeMap) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting, |
||
| 390 | * parsing, or validation. The instance is loaded with phone number metadata for a number of most |
||
| 391 | * commonly used regions. |
||
| 392 | * |
||
| 393 | * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance |
||
| 394 | * multiple times will only result in one instance being created. |
||
| 395 | * |
||
| 396 | * @param string $baseFileLocation |
||
| 397 | * @param array|null $countryCallingCodeToRegionCodeMap |
||
| 398 | * @param MetadataLoaderInterface|null $metadataLoader |
||
| 399 | * @param MetadataSourceInterface|null $metadataSource |
||
| 400 | * @return PhoneNumberUtil instance |
||
| 401 | */ |
||
| 402 | 5253 | public static function getInstance($baseFileLocation = self::META_DATA_FILE_PREFIX, array $countryCallingCodeToRegionCodeMap = null, MetadataLoaderInterface $metadataLoader = null, MetadataSourceInterface $metadataSource = null) |
|
| 421 | |||
| 422 | 403 | protected function init() |
|
| 444 | |||
| 445 | 403 | protected static function initCapturingExtnDigits() |
|
| 449 | |||
| 450 | 403 | protected static function initExtnPatterns() |
|
| 461 | |||
| 462 | // The FIRST_GROUP_PATTERN was originally set to $1 but there are some countries for which the |
||
| 463 | // first group is not used in the national pattern (e.g. Argentina) so the $1 group does not match |
||
| 464 | // correctly. Therefore, we use \d, so that the first group actually used in the pattern will be |
||
| 465 | // matched. |
||
| 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 | 403 | protected static function createExtnPattern($singleExtnSymbols) |
|
| 491 | |||
| 492 | 403 | protected static function initExtnPattern() |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Used for testing purposes only to reset the PhoneNumberUtil singleton to null. |
||
| 499 | */ |
||
| 500 | 402 | public static function resetInstance() |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Converts all alpha characters in a number to their respective digits on a keypad, but retains |
||
| 507 | * existing formatting. |
||
| 508 | * @param string $number |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public static function convertAlphaCharactersInNumber($number) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Normalizes a string of characters representing a phone number by replacing all characters found |
||
| 518 | * in the accompanying map with the values therein, and stripping all other characters if |
||
| 519 | * removeNonMatches is true. |
||
| 520 | * |
||
| 521 | * @param string $number a string of characters representing a phone number |
||
| 522 | * @param array $normalizationReplacements a mapping of characters to what they should be replaced by in |
||
| 523 | * the normalized version of the phone number |
||
| 524 | * @param bool $removeNonMatches indicates whether characters that are not able to be replaced |
||
| 525 | * should be stripped from the number. If this is false, they will be left unchanged in the number. |
||
| 526 | * @return string the normalized string version of the phone number |
||
| 527 | */ |
||
| 528 | 5 | protected static function normalizeHelper($number, array $normalizationReplacements, $removeNonMatches) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Helper function to check if the national prefix formatting rule has the first group only, i.e., |
||
| 548 | * does not start with the national prefix. |
||
| 549 | * @param string $nationalPrefixFormattingRule |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | public static function formattingRuleHasFirstGroupOnly($nationalPrefixFormattingRule) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Convenience method to get a list of what regions the library has metadata for. |
||
| 560 | * @return array |
||
| 561 | */ |
||
| 562 | 249 | public function getSupportedRegions() |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Convenience method to get a list of what global network calling codes the library has metadata |
||
| 569 | * for. |
||
| 570 | * @return array |
||
| 571 | */ |
||
| 572 | 5 | public function getSupportedGlobalNetworkCallingCodes() |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Gets the length of the geographical area code from the {@code nationalNumber} field of the |
||
| 579 | * PhoneNumber object passed in, so that clients could use it to split a national significant |
||
| 580 | * number into geographical area code and subscriber number. It works in such a way that the |
||
| 581 | * resultant subscriber number should be diallable, at least on some devices. An example of how |
||
| 582 | * this could be used: |
||
| 583 | * |
||
| 584 | * <code> |
||
| 585 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 586 | * $number = $phoneUtil->parse("16502530000", "US"); |
||
| 587 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 588 | * |
||
| 589 | * $areaCodeLength = $phoneUtil->getLengthOfGeographicalAreaCode($number); |
||
| 590 | * if ($areaCodeLength > 0) |
||
| 591 | * { |
||
| 592 | * $areaCode = substr($nationalSignificantNumber, 0,$areaCodeLength); |
||
| 593 | * $subscriberNumber = substr($nationalSignificantNumber, $areaCodeLength); |
||
| 594 | * } else { |
||
| 595 | * $areaCode = ""; |
||
| 596 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 597 | * } |
||
| 598 | * </code> |
||
| 599 | * |
||
| 600 | * N.B.: area code is a very ambiguous concept, so the I18N team generally recommends against |
||
| 601 | * using it for most purposes, but recommends using the more general {@code nationalNumber} |
||
| 602 | * instead. Read the following carefully before deciding to use this method: |
||
| 603 | * <ul> |
||
| 604 | * <li> geographical area codes change over time, and this method honors those changes; |
||
| 605 | * therefore, it doesn't guarantee the stability of the result it produces. |
||
| 606 | * <li> subscriber numbers may not be diallable from all devices (notably mobile devices, which |
||
| 607 | * typically requires the full national_number to be dialled in most regions). |
||
| 608 | * <li> most non-geographical numbers have no area codes, including numbers from non-geographical |
||
| 609 | * entities |
||
| 610 | * <li> some geographical numbers have no area codes. |
||
| 611 | * </ul> |
||
| 612 | * @param PhoneNumber $number PhoneNumber object for which clients want to know the length of the area code. |
||
| 613 | * @return int the length of area code of the PhoneNumber object passed in. |
||
| 614 | */ |
||
| 615 | 1 | public function getLengthOfGeographicalAreaCode(PhoneNumber $number) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Returns the metadata for the given region code or {@code null} if the region code is invalid |
||
| 648 | * or unknown. |
||
| 649 | * @param string $regionCode |
||
| 650 | * @return PhoneMetadata |
||
| 651 | */ |
||
| 652 | 296 | public function getMetadataForRegion($regionCode) |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Helper function to check region code is not unknown or null. |
||
| 663 | * @param string $regionCode |
||
| 664 | * @return bool |
||
| 665 | */ |
||
| 666 | 2 | protected function isValidRegionCode($regionCode) |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Returns the region where a phone number is from. This could be used for geocoding at the region |
||
| 673 | * level. |
||
| 674 | * |
||
| 675 | * @param PhoneNumber $number the phone number whose origin we want to know |
||
| 676 | * @return null|string the region where the phone number is from, or null if no region matches this calling |
||
| 677 | * code |
||
| 678 | */ |
||
| 679 | 545 | public function getRegionCodeForNumber(PhoneNumber $number) |
|
| 692 | |||
| 693 | /** |
||
| 694 | * @param PhoneNumber $number |
||
| 695 | * @param array $regionCodes |
||
| 696 | * @return null|string |
||
| 697 | */ |
||
| 698 | 128 | protected function getRegionCodeForNumberFromRegionList(PhoneNumber $number, array $regionCodes) |
|
| 721 | |||
| 722 | /** |
||
| 723 | * Gets the national significant number of the a phone number. Note a national significant number |
||
| 724 | * doesn't contain a national prefix or any formatting. |
||
| 725 | * |
||
| 726 | * @param PhoneNumber $number the phone number for which the national significant number is needed |
||
| 727 | * @return string the national significant number of the PhoneNumber object passed in |
||
| 728 | */ |
||
| 729 | 248 | public function getNationalSignificantNumber(PhoneNumber $number) |
|
| 740 | |||
| 741 | /** |
||
| 742 | * @param string $nationalNumber |
||
| 743 | * @param PhoneMetadata $metadata |
||
| 744 | * @return int PhoneNumberType constant |
||
| 745 | */ |
||
| 746 | 220 | protected function getNumberTypeHelper($nationalNumber, PhoneMetadata $metadata) |
|
| 795 | |||
| 796 | /** |
||
| 797 | * @param string $nationalNumber |
||
| 798 | * @param PhoneNumberDesc $numberDesc |
||
| 799 | * @return bool |
||
| 800 | */ |
||
| 801 | 185 | public function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) |
|
| 816 | |||
| 817 | /** |
||
| 818 | * isNumberGeographical(PhoneNumber) |
||
| 819 | * |
||
| 820 | * Tests whether a phone number has a geographical association. It checks if the number is |
||
| 821 | * associated to a certain region in the country where it belongs to. Note that this doesn't |
||
| 822 | * verify if the number is actually in use. |
||
| 823 | * |
||
| 824 | * isNumberGeographical(PhoneNumberType, $countryCallingCode) |
||
| 825 | * |
||
| 826 | * Tests whether a phone number has a geographical association, as represented by its type and the |
||
| 827 | * country it belongs to. |
||
| 828 | * |
||
| 829 | * This version exists since calculating the phone number type is expensive; if we have already |
||
| 830 | * done this, we don't want to do it again. |
||
| 831 | * |
||
| 832 | * @param PhoneNumber|int $phoneNumberObjOrType A PhoneNumber object, or a PhoneNumberType integer |
||
| 833 | * @param int|null $countryCallingCode Used when passing a PhoneNumberType |
||
| 834 | * @return bool |
||
| 835 | */ |
||
| 836 | 12 | public function isNumberGeographical($phoneNumberObjOrType, $countryCallingCode = null) |
|
| 847 | |||
| 848 | /** |
||
| 849 | * Gets the type of a phone number. |
||
| 850 | * @param PhoneNumber $number the number the phone number that we want to know the type |
||
| 851 | * @return int PhoneNumberType the type of the phone number |
||
| 852 | */ |
||
| 853 | 1334 | public function getNumberType(PhoneNumber $number) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * @param int $countryCallingCode |
||
| 866 | * @param string $regionCode |
||
| 867 | * @return PhoneMetadata |
||
| 868 | */ |
||
| 869 | 259 | protected function getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode) |
|
| 874 | |||
| 875 | /** |
||
| 876 | * @param int $countryCallingCode |
||
| 877 | * @return PhoneMetadata |
||
| 878 | */ |
||
| 879 | 10 | public function getMetadataForNonGeographicalRegion($countryCallingCode) |
|
| 886 | |||
| 887 | /** |
||
| 888 | * Gets the length of the national destination code (NDC) from the PhoneNumber object passed in, |
||
| 889 | * so that clients could use it to split a national significant number into NDC and subscriber |
||
| 890 | * number. The NDC of a phone number is normally the first group of digit(s) right after the |
||
| 891 | * country calling code when the number is formatted in the international format, if there is a |
||
| 892 | * subscriber number part that follows. An example of how this could be used: |
||
| 893 | * |
||
| 894 | * <code> |
||
| 895 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 896 | * $number = $phoneUtil->parse("18002530000", "US"); |
||
| 897 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 898 | * |
||
| 899 | * $nationalDestinationCodeLength = $phoneUtil->getLengthOfNationalDestinationCode($number); |
||
| 900 | * if ($nationalDestinationCodeLength > 0) { |
||
| 901 | * $nationalDestinationCode = substr($nationalSignificantNumber, 0, $nationalDestinationCodeLength); |
||
| 902 | * $subscriberNumber = substr($nationalSignificantNumber, $nationalDestinationCodeLength); |
||
| 903 | * } else { |
||
| 904 | * $nationalDestinationCode = ""; |
||
| 905 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 906 | * } |
||
| 907 | * </code> |
||
| 908 | * |
||
| 909 | * Refer to the unit tests to see the difference between this function and |
||
| 910 | * {@link #getLengthOfGeographicalAreaCode}. |
||
| 911 | * |
||
| 912 | * @param PhoneNumber $number the PhoneNumber object for which clients want to know the length of the NDC. |
||
| 913 | * @return int the length of NDC of the PhoneNumber object passed in. |
||
| 914 | */ |
||
| 915 | 1 | public function getLengthOfNationalDestinationCode(PhoneNumber $number) |
|
| 952 | |||
| 953 | /** |
||
| 954 | * Formats a phone number in the specified format using default rules. Note that this does not |
||
| 955 | * promise to produce a phone number that the user can dial from where they are - although we do |
||
| 956 | * format in either 'national' or 'international' format depending on what the client asks for, we |
||
| 957 | * do not currently support a more abbreviated format, such as for users in the same "area" who |
||
| 958 | * could potentially dial the number without area code. Note that if the phone number has a |
||
| 959 | * country calling code of 0 or an otherwise invalid country calling code, we cannot work out |
||
| 960 | * which formatting rules to apply so we return the national significant number with no formatting |
||
| 961 | * applied. |
||
| 962 | * |
||
| 963 | * @param PhoneNumber $number the phone number to be formatted |
||
| 964 | * @param int $numberFormat the PhoneNumberFormat the phone number should be formatted into |
||
| 965 | * @return string the formatted phone number |
||
| 966 | */ |
||
| 967 | 260 | public function format(PhoneNumber $number, $numberFormat) |
|
| 1005 | |||
| 1006 | /** |
||
| 1007 | * A helper function that is used by format and formatByPattern. |
||
| 1008 | * @param int $countryCallingCode |
||
| 1009 | * @param int $numberFormat PhoneNumberFormat |
||
| 1010 | * @param string $formattedNumber |
||
| 1011 | */ |
||
| 1012 | 259 | protected function prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, &$formattedNumber) |
|
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Helper function to check the country calling code is valid. |
||
| 1032 | * @param int $countryCallingCode |
||
| 1033 | * @return bool |
||
| 1034 | */ |
||
| 1035 | 21 | protected function hasValidCountryCallingCode($countryCallingCode) |
|
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Returns the region code that matches the specific country calling code. In the case of no |
||
| 1042 | * region code being found, ZZ will be returned. In the case of multiple regions, the one |
||
| 1043 | * designated in the metadata as the "main" region for this calling code will be returned. If the |
||
| 1044 | * countryCallingCode entered is valid but doesn't match a specific region (such as in the case of |
||
| 1045 | * non-geographical calling codes like 800) the value "001" will be returned (corresponding to |
||
| 1046 | * the value for World in the UN M.49 schema). |
||
| 1047 | * |
||
| 1048 | * @param int $countryCallingCode |
||
| 1049 | * @return string |
||
| 1050 | */ |
||
| 1051 | 293 | public function getRegionCodeForCountryCode($countryCallingCode) |
|
| 1056 | |||
| 1057 | /** |
||
| 1058 | * Note in some regions, the national number can be written in two completely different ways |
||
| 1059 | * depending on whether it forms part of the NATIONAL format or INTERNATIONAL format. The |
||
| 1060 | * numberFormat parameter here is used to specify which format to use for those cases. If a |
||
| 1061 | * carrierCode is specified, this will be inserted into the formatted string to replace $CC. |
||
| 1062 | * @param string $number |
||
| 1063 | * @param PhoneMetadata $metadata |
||
| 1064 | * @param int $numberFormat PhoneNumberFormat |
||
| 1065 | * @param null|string $carrierCode |
||
| 1066 | * @return string |
||
| 1067 | */ |
||
| 1068 | 23 | protected function formatNsn($number, PhoneMetadata $metadata, $numberFormat, $carrierCode = null) |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * @param NumberFormat[] $availableFormats |
||
| 1084 | * @param string $nationalNumber |
||
| 1085 | * @return NumberFormat|null |
||
| 1086 | */ |
||
| 1087 | 22 | public function chooseFormattingPatternForNumber(array $availableFormats, $nationalNumber) |
|
| 1108 | |||
| 1109 | /** |
||
| 1110 | * Note that carrierCode is optional - if null or an empty string, no carrier code replacement |
||
| 1111 | * will take place. |
||
| 1112 | * @param string $nationalNumber |
||
| 1113 | * @param NumberFormat $formattingPattern |
||
| 1114 | * @param int $numberFormat PhoneNumberFormat |
||
| 1115 | * @param null|string $carrierCode |
||
| 1116 | * @return string |
||
| 1117 | */ |
||
| 1118 | 22 | protected function formatNsnUsingPattern( |
|
| 1165 | |||
| 1166 | /** |
||
| 1167 | * Appends the formatted extension of a phone number to formattedNumber, if the phone number had |
||
| 1168 | * an extension specified. |
||
| 1169 | * |
||
| 1170 | * @param PhoneNumber $number |
||
| 1171 | * @param PhoneMetadata|null $metadata |
||
| 1172 | * @param int $numberFormat PhoneNumberFormat |
||
| 1173 | * @param string $formattedNumber |
||
| 1174 | */ |
||
| 1175 | 1 | protected function maybeAppendFormattedExtension(PhoneNumber $number, $metadata, $numberFormat, &$formattedNumber) |
|
| 1189 | |||
| 1190 | /** |
||
| 1191 | * Returns the mobile token for the provided country calling code if it has one, otherwise |
||
| 1192 | * returns an empty string. A mobile token is a number inserted before the area code when dialing |
||
| 1193 | * a mobile number from that country from abroad. |
||
| 1194 | * |
||
| 1195 | * @param int $countryCallingCode the country calling code for which we want the mobile token |
||
| 1196 | * @return string the mobile token, as a string, for the given country calling code |
||
| 1197 | */ |
||
| 1198 | 6 | public static function getCountryMobileToken($countryCallingCode) |
|
| 1205 | |||
| 1206 | /** |
||
| 1207 | * Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT. A valid vanity |
||
| 1208 | * number will start with at least 3 digits and will have three or more alpha characters. This |
||
| 1209 | * does not do region-specific checks - to work out if this number is actually valid for a region, |
||
| 1210 | * it should be parsed and methods such as {@link #isPossibleNumberWithReason} and |
||
| 1211 | * {@link #isValidNumber} should be used. |
||
| 1212 | * |
||
| 1213 | * @param string $number the number that needs to be checked |
||
| 1214 | * @return bool true if the number is a valid vanity number |
||
| 1215 | */ |
||
| 1216 | public function isAlphaNumber($number) |
||
| 1225 | |||
| 1226 | /** |
||
| 1227 | * Checks to see if the string of characters could possibly be a phone number at all. At the |
||
| 1228 | * moment, checks to see that the string begins with at least 2 digits, ignoring any punctuation |
||
| 1229 | * commonly found in phone numbers. |
||
| 1230 | * This method does not require the number to be normalized in advance - but does assume that |
||
| 1231 | * leading non-number symbols have been removed, such as by the method extractPossibleNumber. |
||
| 1232 | * |
||
| 1233 | * @param string $number to be checked for viability as a phone number |
||
| 1234 | * @return boolean true if the number could be a phone number of some sort, otherwise false |
||
| 1235 | */ |
||
| 1236 | 2210 | public static function isViablePhoneNumber($number) |
|
| 1247 | |||
| 1248 | /** |
||
| 1249 | * We append optionally the extension pattern to the end here, as a valid phone number may |
||
| 1250 | * have an extension prefix appended, followed by 1 or more digits. |
||
| 1251 | * @return string |
||
| 1252 | */ |
||
| 1253 | 2210 | protected static function getValidPhoneNumberPattern() |
|
| 1257 | |||
| 1258 | /** |
||
| 1259 | * Strips any extension (as in, the part of the number dialled after the call is connected, |
||
| 1260 | * usually indicated with extn, ext, x or similar) from the end of the number, and returns it. |
||
| 1261 | * |
||
| 1262 | * @param string $number the non-normalized telephone number that we wish to strip the extension from |
||
| 1263 | * @return string the phone extension |
||
| 1264 | */ |
||
| 1265 | 2209 | protected function maybeStripExtension(&$number) |
|
| 1286 | |||
| 1287 | /** |
||
| 1288 | * Parses a string and returns it in proto buffer format. This method differs from {@link #parse} |
||
| 1289 | * in that it always populates the raw_input field of the protocol buffer with numberToParse as |
||
| 1290 | * well as the country_code_source field. |
||
| 1291 | * |
||
| 1292 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 1293 | * such as +, ( and -, as well as a phone number extension. It can also |
||
| 1294 | * be provided in RFC3966 format. |
||
| 1295 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 1296 | * if the number being parsed is not written in international format. |
||
| 1297 | * The country calling code for the number in this case would be stored |
||
| 1298 | * as that of the default region supplied. |
||
| 1299 | * @param PhoneNumber $phoneNumber |
||
| 1300 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 1301 | */ |
||
| 1302 | public function parseAndKeepRawInput($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null) |
||
| 1310 | |||
| 1311 | /** |
||
| 1312 | * A helper function to set the values related to leading zeros in a PhoneNumber. |
||
| 1313 | * @param string $nationalNumber |
||
| 1314 | * @param PhoneNumber $phoneNumber |
||
| 1315 | */ |
||
| 1316 | 32 | public static function setItalianLeadingZerosForPhoneNumber($nationalNumber, PhoneNumber $phoneNumber) |
|
| 1333 | |||
| 1334 | /** |
||
| 1335 | * Parses a string and fills up the phoneNumber. This method is the same as the public |
||
| 1336 | * parse() method, with the exception that it allows the default region to be null, for use by |
||
| 1337 | * isNumberMatch(). checkRegion should be set to false if it is permitted for the default region |
||
| 1338 | * to be null or unknown ("ZZ"). |
||
| 1339 | * @param string $numberToParse |
||
| 1340 | * @param string $defaultRegion |
||
| 1341 | * @param bool $keepRawInput |
||
| 1342 | * @param bool $checkRegion |
||
| 1343 | * @param PhoneNumber $phoneNumber |
||
| 1344 | * @throws NumberParseException |
||
| 1345 | */ |
||
| 1346 | 2430 | protected function parseHelper($numberToParse, $defaultRegion, $keepRawInput, $checkRegion, PhoneNumber $phoneNumber) |
|
| 1496 | |||
| 1497 | /** |
||
| 1498 | * Converts numberToParse to a form that we can parse and write it to nationalNumber if it is |
||
| 1499 | * written in RFC3966; otherwise extract a possible number out of it and write to nationalNumber. |
||
| 1500 | * @param string $numberToParse |
||
| 1501 | * @param string $nationalNumber |
||
| 1502 | */ |
||
| 1503 | 2210 | protected function buildNationalNumberForParsing($numberToParse, &$nationalNumber) |
|
| 1547 | |||
| 1548 | /** |
||
| 1549 | * Attempts to extract a possible number from the string passed in. This currently strips all |
||
| 1550 | * leading characters that cannot be used to start a phone number. Characters that can be used to |
||
| 1551 | * start a phone number are defined in the VALID_START_CHAR_PATTERN. If none of these characters |
||
| 1552 | * are found in the number passed in, an empty string is returned. This function also attempts to |
||
| 1553 | * strip off any alternative extensions or endings if two or more are present, such as in the case |
||
| 1554 | * of: (530) 583-6985 x302/x2303. The second extension here makes this actually two phone numbers, |
||
| 1555 | * (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second extension so that the first |
||
| 1556 | * number is parsed correctly. |
||
| 1557 | * |
||
| 1558 | * @param int $number the string that might contain a phone number |
||
| 1559 | * @return string the number, stripped of any non-phone-number prefix (such as "Tel:") or an empty |
||
| 1560 | * string if no character used to start phone numbers (such as + or any digit) is |
||
| 1561 | * found in the number |
||
| 1562 | */ |
||
| 1563 | 1976 | public static function extractPossibleNumber($number) |
|
| 1586 | |||
| 1587 | /** |
||
| 1588 | * Checks to see that the region code used is valid, or if it is not valid, that the number to |
||
| 1589 | * parse starts with a + symbol so that we can attempt to infer the region from the number. |
||
| 1590 | * Returns false if it cannot use the region provided and the region cannot be inferred. |
||
| 1591 | * @param string $numberToParse |
||
| 1592 | * @param string $defaultRegion |
||
| 1593 | * @return bool |
||
| 1594 | */ |
||
| 1595 | 2209 | protected function checkRegionForParsing($numberToParse, $defaultRegion) |
|
| 1606 | |||
| 1607 | /** |
||
| 1608 | * Tries to extract a country calling code from a number. This method will return zero if no |
||
| 1609 | * country calling code is considered to be present. Country calling codes are extracted in the |
||
| 1610 | * following ways: |
||
| 1611 | * <ul> |
||
| 1612 | * <li> by stripping the international dialing prefix of the region the person is dialing from, |
||
| 1613 | * if this is present in the number, and looking at the next digits |
||
| 1614 | * <li> by stripping the '+' sign if present and then looking at the next digits |
||
| 1615 | * <li> by comparing the start of the number and the country calling code of the default region. |
||
| 1616 | * If the number is not considered possible for the numbering plan of the default region |
||
| 1617 | * initially, but starts with the country calling code of this region, validation will be |
||
| 1618 | * reattempted after stripping this country calling code. If this number is considered a |
||
| 1619 | * possible number, then the first digits will be considered the country calling code and |
||
| 1620 | * removed as such. |
||
| 1621 | * </ul> |
||
| 1622 | * It will throw a NumberParseException if the number starts with a '+' but the country calling |
||
| 1623 | * code supplied after this does not match that of any known region. |
||
| 1624 | * |
||
| 1625 | * @param string $number non-normalized telephone number that we wish to extract a country calling |
||
| 1626 | * code from - may begin with '+' |
||
| 1627 | * @param PhoneMetadata $defaultRegionMetadata metadata about the region this number may be from |
||
| 1628 | * @param string $nationalNumber a string buffer to store the national significant number in, in the case |
||
| 1629 | * that a country calling code was extracted. The number is appended to any existing contents. |
||
| 1630 | * If no country calling code was extracted, this will be left unchanged. |
||
| 1631 | * @param bool $keepRawInput true if the country_code_source and preferred_carrier_code fields of |
||
| 1632 | * phoneNumber should be populated. |
||
| 1633 | * @param PhoneNumber $phoneNumber the PhoneNumber object where the country_code and country_code_source need |
||
| 1634 | * to be populated. Note the country_code is always populated, whereas country_code_source is |
||
| 1635 | * only populated when keepCountryCodeSource is true. |
||
| 1636 | * @return int the country calling code extracted or 0 if none could be extracted |
||
| 1637 | * @throws NumberParseException |
||
| 1638 | */ |
||
| 1639 | 2434 | public function maybeExtractCountryCode( |
|
| 1718 | |||
| 1719 | /** |
||
| 1720 | * Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes |
||
| 1721 | * the resulting number, and indicates if an international prefix was present. |
||
| 1722 | * |
||
| 1723 | * @param string $number the non-normalized telephone number that we wish to strip any international |
||
| 1724 | * dialing prefix from. |
||
| 1725 | * @param string $possibleIddPrefix string the international direct dialing prefix from the region we |
||
| 1726 | * think this number may be dialed in |
||
| 1727 | * @return int the corresponding CountryCodeSource if an international dialing prefix could be |
||
| 1728 | * removed from the number, otherwise CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did |
||
| 1729 | * not seem to be in international format. |
||
| 1730 | */ |
||
| 1731 | 2434 | public function maybeStripInternationalPrefixAndNormalize(&$number, $possibleIddPrefix) |
|
| 1752 | |||
| 1753 | /** |
||
| 1754 | * Normalizes a string of characters representing a phone number. This performs |
||
| 1755 | * the following conversions: |
||
| 1756 | * Punctuation is stripped. |
||
| 1757 | * For ALPHA/VANITY numbers: |
||
| 1758 | * Letters are converted to their numeric representation on a telephone |
||
| 1759 | * keypad. The keypad used here is the one defined in ITU Recommendation |
||
| 1760 | * E.161. This is only done if there are 3 or more letters in the number, |
||
| 1761 | * to lessen the risk that such letters are typos. |
||
| 1762 | * For other numbers: |
||
| 1763 | * Wide-ascii digits are converted to normal ASCII (European) digits. |
||
| 1764 | * Arabic-Indic numerals are converted to European numerals. |
||
| 1765 | * Spurious alpha characters are stripped. |
||
| 1766 | * |
||
| 1767 | * @param string $number a string of characters representing a phone number. |
||
| 1768 | * @return string the normalized string version of the phone number. |
||
| 1769 | */ |
||
| 1770 | public static function normalize(&$number) |
||
| 1779 | |||
| 1780 | /** |
||
| 1781 | * Normalizes a string of characters representing a phone number. This converts wide-ascii and |
||
| 1782 | * arabic-indic numerals to European numerals, and strips punctuation and alpha characters. |
||
| 1783 | * |
||
| 1784 | * @param $number string a string of characters representing a phone number |
||
| 1785 | * @return string the normalized string version of the phone number |
||
| 1786 | */ |
||
| 1787 | public static function normalizeDigitsOnly($number) |
||
| 1791 | |||
| 1792 | /** |
||
| 1793 | * @param string $number |
||
| 1794 | * @param bool $keepNonDigits |
||
| 1795 | * @return string |
||
| 1796 | */ |
||
| 1797 | 28 | public static function normalizeDigits($number, $keepNonDigits) |
|
| 1816 | |||
| 1817 | /** |
||
| 1818 | * Strips the IDD from the start of the number if present. Helper function used by |
||
| 1819 | * maybeStripInternationalPrefixAndNormalize. |
||
| 1820 | * @param string $iddPattern |
||
| 1821 | * @param string $number |
||
| 1822 | * @return bool |
||
| 1823 | */ |
||
| 1824 | 2433 | protected function parsePrefixAsIdd($iddPattern, &$number) |
|
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Extracts country calling code from fullNumber, returns it and places the remaining number in nationalNumber. |
||
| 1846 | * It assumes that the leading plus sign or IDD has already been removed. |
||
| 1847 | * Returns 0 if fullNumber doesn't start with a valid country calling code, and leaves nationalNumber unmodified. |
||
| 1848 | * @param string $fullNumber |
||
| 1849 | * @param string $nationalNumber |
||
| 1850 | * @return int |
||
| 1851 | */ |
||
| 1852 | 267 | protected function extractCountryCode(&$fullNumber, &$nationalNumber) |
|
| 1868 | |||
| 1869 | /** |
||
| 1870 | * Strips any national prefix (such as 0, 1) present in the number provided. |
||
| 1871 | * |
||
| 1872 | * @param string $number the normalized telephone number that we wish to strip any national |
||
| 1873 | * dialing prefix from |
||
| 1874 | * @param PhoneMetadata $metadata the metadata for the region that we think this number is from |
||
| 1875 | * @param string $carrierCode a place to insert the carrier code if one is extracted |
||
| 1876 | * @return bool true if a national prefix or carrier code (or both) could be extracted. |
||
| 1877 | */ |
||
| 1878 | 2168 | public function maybeStripNationalPrefixAndCarrierCode(&$number, PhoneMetadata $metadata, &$carrierCode) |
|
| 1937 | |||
| 1938 | /** |
||
| 1939 | * Helper method to check a number against possible lengths for this number, and determine whether |
||
| 1940 | * it matches, or is too short or too long. Currently, if a number pattern suggests that numbers |
||
| 1941 | * of length 7 and 10 are possible, and a number in between these possible lengths is entered, |
||
| 1942 | * such as of length 8, this will return TOO_LONG. |
||
| 1943 | * @param string $number |
||
| 1944 | * @param PhoneNumberDesc $phoneNumberDesc |
||
| 1945 | * @return int ValidationResult |
||
| 1946 | */ |
||
| 1947 | 2172 | protected function testNumberLength($number, PhoneNumberDesc $phoneNumberDesc) |
|
| 1977 | |||
| 1978 | /** |
||
| 1979 | * Returns a list with the region codes that match the specific country calling code. For |
||
| 1980 | * non-geographical country calling codes, the region code 001 is returned. Also, in the case |
||
| 1981 | * of no region code being found, an empty list is returned. |
||
| 1982 | * @param int $countryCallingCode |
||
| 1983 | * @return array |
||
| 1984 | */ |
||
| 1985 | 8 | public function getRegionCodesForCountryCode($countryCallingCode) |
|
| 1990 | |||
| 1991 | /** |
||
| 1992 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 1993 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 1994 | * |
||
| 1995 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 1996 | * @return int the country calling code for the region denoted by regionCode |
||
| 1997 | */ |
||
| 1998 | public function getCountryCodeForRegion($regionCode) |
||
| 2005 | |||
| 2006 | /** |
||
| 2007 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 2008 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 2009 | * |
||
| 2010 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 2011 | * @return int the country calling code for the region denoted by regionCode |
||
| 2012 | * @throws \InvalidArgumentException if the region is invalid |
||
| 2013 | */ |
||
| 2014 | 1545 | protected function getCountryCodeForValidRegion($regionCode) |
|
| 2022 | |||
| 2023 | /** |
||
| 2024 | * Returns a number formatted in such a way that it can be dialed from a mobile phone in a |
||
| 2025 | * specific region. If the number cannot be reached from the region (e.g. some countries block |
||
| 2026 | * toll-free numbers from being called outside of the country), the method returns an empty |
||
| 2027 | * string. |
||
| 2028 | * |
||
| 2029 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2030 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2031 | * @param boolean $withFormatting whether the number should be returned with formatting symbols, such as |
||
| 2032 | * spaces and dashes. |
||
| 2033 | * @return string the formatted phone number |
||
| 2034 | */ |
||
| 2035 | 1 | public function formatNumberForMobileDialing(PhoneNumber $number, $regionCallingFrom, $withFormatting) |
|
| 2117 | |||
| 2118 | /** |
||
| 2119 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2120 | * {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the |
||
| 2121 | * phone number already has a preferred domestic carrier code stored. If {@code carrierCode} |
||
| 2122 | * contains an empty string, returns the number in national format without any carrier code. |
||
| 2123 | * |
||
| 2124 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2125 | * @param string $carrierCode the carrier selection code to be used |
||
| 2126 | * @return string the formatted phone number in national format for dialing using the carrier as |
||
| 2127 | * specified in the {@code carrierCode} |
||
| 2128 | */ |
||
| 2129 | 1 | public function formatNationalNumberWithCarrierCode(PhoneNumber $number, $carrierCode) |
|
| 2158 | |||
| 2159 | /** |
||
| 2160 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2161 | * preferredDomesticCarrierCode field of the PhoneNumber object passed in. If that is missing, |
||
| 2162 | * use the {@code fallbackCarrierCode} passed in instead. If there is no |
||
| 2163 | * {@code preferredDomesticCarrierCode}, and the {@code fallbackCarrierCode} contains an empty |
||
| 2164 | * string, return the number in national format without any carrier code. |
||
| 2165 | * |
||
| 2166 | * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in |
||
| 2167 | * should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting. |
||
| 2168 | * |
||
| 2169 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2170 | * @param string $fallbackCarrierCode the carrier selection code to be used, if none is found in the |
||
| 2171 | * phone number itself |
||
| 2172 | * @return string the formatted phone number in national format for dialing using the number's |
||
| 2173 | * {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if |
||
| 2174 | * none is found |
||
| 2175 | */ |
||
| 2176 | public function formatNationalNumberWithPreferredCarrierCode(PhoneNumber $number, $fallbackCarrierCode) |
||
| 2185 | |||
| 2186 | /** |
||
| 2187 | * Returns true if the number can be dialled from outside the region, or unknown. If the number |
||
| 2188 | * can only be dialled from within the region, returns false. Does not check the number is a valid |
||
| 2189 | * number. |
||
| 2190 | * TODO: Make this method public when we have enough metadata to make it worthwhile. |
||
| 2191 | * |
||
| 2192 | * @param PhoneNumber $number the phone-number for which we want to know whether it is diallable from outside the region |
||
| 2193 | * @return bool |
||
| 2194 | */ |
||
| 2195 | 29 | public function canBeInternationallyDialled(PhoneNumber $number) |
|
| 2206 | |||
| 2207 | /** |
||
| 2208 | * Normalizes a string of characters representing a phone number. This strips all characters which |
||
| 2209 | * are not diallable on a mobile phone keypad (including all non-ASCII digits). |
||
| 2210 | * |
||
| 2211 | * @param string $number a string of characters representing a phone number |
||
| 2212 | * @return string the normalized string version of the phone number |
||
| 2213 | */ |
||
| 2214 | public static function normalizeDiallableCharsOnly($number) |
||
| 2218 | |||
| 2219 | /** |
||
| 2220 | * Formats a phone number for out-of-country dialing purposes. |
||
| 2221 | * |
||
| 2222 | * Note that in this version, if the number was entered originally using alpha characters and |
||
| 2223 | * this version of the number is stored in raw_input, this representation of the number will be |
||
| 2224 | * used rather than the digit representation. Grouping information, as specified by characters |
||
| 2225 | * such as "-" and " ", will be retained. |
||
| 2226 | * |
||
| 2227 | * <p><b>Caveats:</b></p> |
||
| 2228 | * <ul> |
||
| 2229 | * <li> This will not produce good results if the country calling code is both present in the raw |
||
| 2230 | * input _and_ is the start of the national number. This is not a problem in the regions |
||
| 2231 | * which typically use alpha numbers. |
||
| 2232 | * <li> This will also not produce good results if the raw input has any grouping information |
||
| 2233 | * within the first three digits of the national number, and if the function needs to strip |
||
| 2234 | * preceding digits/words in the raw input before these digits. Normally people group the |
||
| 2235 | * first three digits together so this is not a huge problem - and will be fixed if it |
||
| 2236 | * proves to be so. |
||
| 2237 | * </ul> |
||
| 2238 | * |
||
| 2239 | * @param PhoneNumber $number the phone number that needs to be formatted |
||
| 2240 | * @param String $regionCallingFrom the region where the call is being placed |
||
| 2241 | * @return String the formatted phone number |
||
| 2242 | */ |
||
| 2243 | 1 | public function formatOutOfCountryKeepingAlphaChars(PhoneNumber $number, $regionCallingFrom) |
|
| 2336 | |||
| 2337 | /** |
||
| 2338 | * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is |
||
| 2339 | * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the |
||
| 2340 | * same as that of the region where the number is from, then NATIONAL formatting will be applied. |
||
| 2341 | * |
||
| 2342 | * <p>If the number itself has a country calling code of zero or an otherwise invalid country |
||
| 2343 | * calling code, then we return the number with no formatting applied. |
||
| 2344 | * |
||
| 2345 | * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and |
||
| 2346 | * Kazakhstan (who share the same country calling code). In those cases, no international prefix |
||
| 2347 | * is used. For regions which have multiple international prefixes, the number in its |
||
| 2348 | * INTERNATIONAL format will be returned instead. |
||
| 2349 | * |
||
| 2350 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2351 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2352 | * @return string the formatted phone number |
||
| 2353 | */ |
||
| 2354 | 3 | public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCallingFrom) |
|
| 2421 | |||
| 2422 | /** |
||
| 2423 | * Checks if this is a region under the North American Numbering Plan Administration (NANPA). |
||
| 2424 | * @param string $regionCode |
||
| 2425 | * @return boolean true if regionCode is one of the regions under NANPA |
||
| 2426 | */ |
||
| 2427 | public function isNANPACountry($regionCode) |
||
| 2431 | |||
| 2432 | /** |
||
| 2433 | * Formats a phone number using the original phone number format that the number is parsed from. |
||
| 2434 | * The original format is embedded in the country_code_source field of the PhoneNumber object |
||
| 2435 | * passed in. If such information is missing, the number will be formatted into the NATIONAL |
||
| 2436 | * format by default. When the number contains a leading zero and this is unexpected for this |
||
| 2437 | * country, or we don't have a formatting pattern for the number, the method returns the raw input |
||
| 2438 | * when it is available. |
||
| 2439 | * |
||
| 2440 | * Note this method guarantees no digit will be inserted, removed or modified as a result of |
||
| 2441 | * formatting. |
||
| 2442 | * |
||
| 2443 | * @param PhoneNumber $number the phone number that needs to be formatted in its original number format |
||
| 2444 | * @param string $regionCallingFrom the region whose IDD needs to be prefixed if the original number |
||
| 2445 | * has one |
||
| 2446 | * @return string the formatted phone number in its original number format |
||
| 2447 | */ |
||
| 2448 | 1 | public function formatInOriginalFormat(PhoneNumber $number, $regionCallingFrom) |
|
| 2546 | |||
| 2547 | /** |
||
| 2548 | * Returns true if a number is from a region whose national significant number couldn't contain a |
||
| 2549 | * leading zero, but has the italian_leading_zero field set to true. |
||
| 2550 | * @param PhoneNumber $number |
||
| 2551 | * @return bool |
||
| 2552 | */ |
||
| 2553 | protected function hasUnexpectedItalianLeadingZero(PhoneNumber $number) |
||
| 2557 | |||
| 2558 | /** |
||
| 2559 | * Checks whether the country calling code is from a region whose national significant number |
||
| 2560 | * could contain a leading zero. An example of such a region is Italy. Returns false if no |
||
| 2561 | * metadata for the country is found. |
||
| 2562 | * @param int $countryCallingCode |
||
| 2563 | * @return bool |
||
| 2564 | */ |
||
| 2565 | 1 | public function isLeadingZeroPossible($countryCallingCode) |
|
| 2576 | |||
| 2577 | /** |
||
| 2578 | * @param PhoneNumber $number |
||
| 2579 | * @return bool |
||
| 2580 | */ |
||
| 2581 | protected function hasFormattingPatternForNumber(PhoneNumber $number) |
||
| 2593 | |||
| 2594 | /** |
||
| 2595 | * Returns the national dialling prefix for a specific region. For example, this would be 1 for |
||
| 2596 | * the United States, and 0 for New Zealand. Set stripNonDigits to true to strip symbols like "~" |
||
| 2597 | * (which indicates a wait for a dialling tone) from the prefix returned. If no national prefix is |
||
| 2598 | * present, we return null. |
||
| 2599 | * |
||
| 2600 | * <p>Warning: Do not use this method for do-your-own formatting - for some regions, the |
||
| 2601 | * national dialling prefix is used only for certain types of numbers. Use the library's |
||
| 2602 | * formatting functions to prefix the national prefix when required. |
||
| 2603 | * |
||
| 2604 | * @param string $regionCode the region that we want to get the dialling prefix for |
||
| 2605 | * @param boolean $stripNonDigits true to strip non-digits from the national dialling prefix |
||
| 2606 | * @return string the dialling prefix for the region denoted by regionCode |
||
| 2607 | */ |
||
| 2608 | 1 | public function getNddPrefixForRegion($regionCode, $stripNonDigits) |
|
| 2626 | |||
| 2627 | /** |
||
| 2628 | * Check if rawInput, which is assumed to be in the national format, has a national prefix. The |
||
| 2629 | * national prefix is assumed to be in digits-only form. |
||
| 2630 | * @param string $rawInput |
||
| 2631 | * @param string $nationalPrefix |
||
| 2632 | * @param string $regionCode |
||
| 2633 | * @return bool |
||
| 2634 | */ |
||
| 2635 | protected function rawInputContainsNationalPrefix($rawInput, $nationalPrefix, $regionCode) |
||
| 2653 | |||
| 2654 | /** |
||
| 2655 | * Tests whether a phone number matches a valid pattern. Note this doesn't verify the number |
||
| 2656 | * is actually in use, which is impossible to tell by just looking at a number itself. |
||
| 2657 | * |
||
| 2658 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2659 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2660 | */ |
||
| 2661 | 1818 | public function isValidNumber(PhoneNumber $number) |
|
| 2666 | |||
| 2667 | /** |
||
| 2668 | * Tests whether a phone number is valid for a certain region. Note this doesn't verify the number |
||
| 2669 | * is actually in use, which is impossible to tell by just looking at a number itself. If the |
||
| 2670 | * country calling code is not the same as the country calling code for the region, this |
||
| 2671 | * immediately exits with false. After this, the specific number pattern rules for the region are |
||
| 2672 | * examined. This is useful for determining for example whether a particular number is valid for |
||
| 2673 | * Canada, rather than just a valid NANPA number. |
||
| 2674 | * Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this |
||
| 2675 | * method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for |
||
| 2676 | * the region "GB" (United Kingdom), since it has its own region code, "IM", which may be |
||
| 2677 | * undesirable. |
||
| 2678 | * |
||
| 2679 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2680 | * @param string $regionCode the region that we want to validate the phone number for |
||
| 2681 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2682 | */ |
||
| 2683 | 1577 | public function isValidNumberForRegion(PhoneNumber $number, $regionCode) |
|
| 2699 | |||
| 2700 | /** |
||
| 2701 | * Parses a string and returns it as a phone number in proto buffer format. The method is quite |
||
| 2702 | * lenient and looks for a number in the input text (raw input) and does not check whether the |
||
| 2703 | * string is definitely only a phone number. To do this, it ignores punctuation and white-space, |
||
| 2704 | * as well as any text before the number (e.g. a leading “Tel: ”) and trims the non-number bits. |
||
| 2705 | * It will accept a number in any format (E164, national, international etc), assuming it can |
||
| 2706 | * interpreted with the defaultRegion supplied. It also attempts to convert any alpha characters |
||
| 2707 | * into digits if it thinks this is a vanity number of the type "1800 MICROSOFT". |
||
| 2708 | * |
||
| 2709 | * <p> This method will throw a {@link NumberParseException} if the number is not considered to |
||
| 2710 | * be a possible number. Note that validation of whether the number is actually a valid number |
||
| 2711 | * for a particular region is not performed. This can be done separately with {@link #isValidnumber}. |
||
| 2712 | * |
||
| 2713 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 2714 | * such as +, ( and -, as well as a phone number extension. |
||
| 2715 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 2716 | * if the number being parsed is not written in international format. |
||
| 2717 | * The country_code for the number in this case would be stored as that |
||
| 2718 | * of the default region supplied. If the number is guaranteed to |
||
| 2719 | * start with a '+' followed by the country calling code, then |
||
| 2720 | * "ZZ" or null can be supplied. |
||
| 2721 | * @param PhoneNumber|null $phoneNumber |
||
| 2722 | * @param bool $keepRawInput |
||
| 2723 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 2724 | * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. |
||
| 2725 | * too few or too many digits) or if no default region was supplied |
||
| 2726 | * and the number is not in international format (does not start |
||
| 2727 | * with +) |
||
| 2728 | */ |
||
| 2729 | 2211 | public function parse($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null, $keepRawInput = false) |
|
| 2737 | |||
| 2738 | /** |
||
| 2739 | * Formats a phone number in the specified format using client-defined formatting rules. Note that |
||
| 2740 | * if the phone number has a country calling code of zero or an otherwise invalid country calling |
||
| 2741 | * code, we cannot work out things like whether there should be a national prefix applied, or how |
||
| 2742 | * to format extensions, so we return the national significant number with no formatting applied. |
||
| 2743 | * |
||
| 2744 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2745 | * @param int $numberFormat the format the phone number should be formatted into |
||
| 2746 | * @param array $userDefinedFormats formatting rules specified by clients |
||
| 2747 | * @return String the formatted phone number |
||
| 2748 | */ |
||
| 2749 | public function formatByPattern(PhoneNumber $number, $numberFormat, array $userDefinedFormats) |
||
| 2796 | |||
| 2797 | /** |
||
| 2798 | * Gets a valid number for the specified region. |
||
| 2799 | * |
||
| 2800 | * @param string regionCode the region for which an example number is needed |
||
| 2801 | * @return PhoneNumber a valid fixed-line number for the specified region. Returns null when the metadata |
||
| 2802 | * does not contain such information, or the region 001 is passed in. For 001 (representing |
||
| 2803 | * non-geographical numbers), call {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 2804 | */ |
||
| 2805 | public function getExampleNumber($regionCode) |
||
| 2809 | |||
| 2810 | /** |
||
| 2811 | * Gets an invalid number for the specified region. This is useful for unit-testing purposes, |
||
| 2812 | * where you want to test what will happen with an invalid number. Note that the number that is |
||
| 2813 | * returned will always be able to be parsed and will have the correct country code. It may also |
||
| 2814 | * be a valid *short* number/code for this region. Validity checking such numbers is handled with |
||
| 2815 | * {@link ShortNumberInfo}. |
||
| 2816 | * |
||
| 2817 | * @param string $regionCode The region for which an example number is needed |
||
| 2818 | * @return PhoneNumber|null An invalid number for the specified region. Returns null when an unsupported region |
||
| 2819 | * or the region 001 (Earth) is passed in. |
||
| 2820 | */ |
||
| 2821 | 244 | public function getInvalidExampleNumber($regionCode) |
|
| 2867 | |||
| 2868 | /** |
||
| 2869 | * Gets a valid number for the specified region and number type. |
||
| 2870 | * |
||
| 2871 | * @param string $regionCodeOrType the region for which an example number is needed |
||
| 2872 | * @param int $type the PhoneNumberType of number that is needed |
||
| 2873 | * @return PhoneNumber a valid number for the specified region and type. Returns null when the metadata |
||
| 2874 | * does not contain such information or if an invalid region or region 001 was entered. |
||
| 2875 | * For 001 (representing non-geographical numbers), call |
||
| 2876 | * {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 2877 | * |
||
| 2878 | * If $regionCodeOrType is the only parameter supplied, then a valid number for the specified number type |
||
| 2879 | * will be returned that may belong to any country. |
||
| 2880 | */ |
||
| 2881 | 3163 | public function getExampleNumberForType($regionCodeOrType, $type = null) |
|
| 2921 | |||
| 2922 | /** |
||
| 2923 | * @param PhoneMetadata $metadata |
||
| 2924 | * @param int $type PhoneNumberType |
||
| 2925 | * @return PhoneNumberDesc |
||
| 2926 | */ |
||
| 2927 | 3413 | protected function getNumberDescByType(PhoneMetadata $metadata, $type) |
|
| 2955 | |||
| 2956 | /** |
||
| 2957 | * Gets a valid number for the specified country calling code for a non-geographical entity. |
||
| 2958 | * |
||
| 2959 | * @param int $countryCallingCode the country calling code for a non-geographical entity |
||
| 2960 | * @return PhoneNumber a valid number for the non-geographical entity. Returns null when the metadata |
||
| 2961 | * does not contain such information, or the country calling code passed in does not belong |
||
| 2962 | * to a non-geographical entity. |
||
| 2963 | */ |
||
| 2964 | 9 | public function getExampleNumberForNonGeoEntity($countryCallingCode) |
|
| 2978 | |||
| 2979 | |||
| 2980 | /** |
||
| 2981 | * Takes two phone numbers and compares them for equality. |
||
| 2982 | * |
||
| 2983 | * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero |
||
| 2984 | * for Italian numbers and any extension present are the same. Returns NSN_MATCH |
||
| 2985 | * if either or both has no region specified, and the NSNs and extensions are |
||
| 2986 | * the same. Returns SHORT_NSN_MATCH if either or both has no region specified, |
||
| 2987 | * or the region specified is the same, and one NSN could be a shorter version |
||
| 2988 | * of the other number. This includes the case where one has an extension |
||
| 2989 | * specified, and the other does not. Returns NO_MATCH otherwise. For example, |
||
| 2990 | * the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH. The numbers |
||
| 2991 | * +1 345 657 1234 and 345 657 are a NO_MATCH. |
||
| 2992 | * |
||
| 2993 | * @param $firstNumberIn PhoneNumber|string First number to compare. If it is a |
||
| 2994 | * string it can contain formatting, and can have country calling code specified |
||
| 2995 | * with + at the start. |
||
| 2996 | * @param $secondNumberIn PhoneNumber|string Second number to compare. If it is a |
||
| 2997 | * string it can contain formatting, and can have country calling code specified |
||
| 2998 | * with + at the start. |
||
| 2999 | * @throws \InvalidArgumentException |
||
| 3000 | * @return int {MatchType} NOT_A_NUMBER, NO_MATCH, |
||
| 3001 | */ |
||
| 3002 | 2 | public function isNumberMatch($firstNumberIn, $secondNumberIn) |
|
| 3124 | |||
| 3125 | /** |
||
| 3126 | * Returns true when one national number is the suffix of the other or both are the same. |
||
| 3127 | * @param PhoneNumber $firstNumber |
||
| 3128 | * @param PhoneNumber $secondNumber |
||
| 3129 | * @return bool |
||
| 3130 | */ |
||
| 3131 | 2 | protected function isNationalNumberSuffixOfTheOther(PhoneNumber $firstNumber, PhoneNumber $secondNumber) |
|
| 3138 | |||
| 3139 | protected function stringEndsWithString($hayStack, $needle) |
||
| 3145 | |||
| 3146 | /** |
||
| 3147 | * Returns true if the supplied region supports mobile number portability. Returns false for |
||
| 3148 | * invalid, unknown or regions that don't support mobile number portability. |
||
| 3149 | * |
||
| 3150 | * @param string $regionCode the region for which we want to know whether it supports mobile number |
||
| 3151 | * portability or not. |
||
| 3152 | * @return bool |
||
| 3153 | */ |
||
| 3154 | 2 | public function isMobileNumberPortableRegion($regionCode) |
|
| 3163 | |||
| 3164 | /** |
||
| 3165 | * Check whether a phone number is a possible number given a number in the form of a string, and |
||
| 3166 | * the region where the number could be dialed from. It provides a more lenient check than |
||
| 3167 | * {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details. |
||
| 3168 | * |
||
| 3169 | * <p>This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)} |
||
| 3170 | * with the resultant PhoneNumber object. |
||
| 3171 | * |
||
| 3172 | * @param PhoneNumber|string $number the number that needs to be checked, in the form of a string |
||
| 3173 | * @param string $regionDialingFrom the region that we are expecting the number to be dialed from. |
||
| 3174 | * Note this is different from the region where the number belongs. For example, the number |
||
| 3175 | * +1 650 253 0000 is a number that belongs to US. When written in this form, it can be |
||
| 3176 | * dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any |
||
| 3177 | * region which uses an international dialling prefix of 00. When it is written as |
||
| 3178 | * 650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it |
||
| 3179 | * can only be dialed from within a smaller area in the US (Mountain View, CA, to be more |
||
| 3180 | * specific). |
||
| 3181 | * @return boolean true if the number is possible |
||
| 3182 | */ |
||
| 3183 | 1 | public function isPossibleNumber($number, $regionDialingFrom = null) |
|
| 3197 | |||
| 3198 | |||
| 3199 | /** |
||
| 3200 | * Check whether a phone number is a possible number. It provides a more lenient check than |
||
| 3201 | * {@link #isValidNumber} in the following sense: |
||
| 3202 | * <ol> |
||
| 3203 | * <li> It only checks the length of phone numbers. In particular, it doesn't check starting |
||
| 3204 | * digits of the number. |
||
| 3205 | * <li> It doesn't attempt to figure out the type of the number, but uses general rules which |
||
| 3206 | * applies to all types of phone numbers in a region. Therefore, it is much faster than |
||
| 3207 | * isValidNumber. |
||
| 3208 | * <li> For fixed line numbers, many regions have the concept of area code, which together with |
||
| 3209 | * subscriber number constitute the national significant number. It is sometimes okay to dial |
||
| 3210 | * the subscriber number only when dialing in the same area. This function will return |
||
| 3211 | * true if the subscriber-number-only version is passed in. On the other hand, because |
||
| 3212 | * isValidNumber validates using information on both starting digits (for fixed line |
||
| 3213 | * numbers, that would most likely be area codes) and length (obviously includes the |
||
| 3214 | * length of area codes for fixed line numbers), it will return false for the |
||
| 3215 | * subscriber-number-only version. |
||
| 3216 | * </ol> |
||
| 3217 | * @param PhoneNumber $number the number that needs to be checked |
||
| 3218 | * @return int a ValidationResult object which indicates whether the number is possible |
||
| 3219 | */ |
||
| 3220 | 1 | public function isPossibleNumberWithReason(PhoneNumber $number) |
|
| 3238 | |||
| 3239 | /** |
||
| 3240 | * Attempts to extract a valid number from a phone number that is too long to be valid, and resets |
||
| 3241 | * the PhoneNumber object passed in to that valid version. If no valid number could be extracted, |
||
| 3242 | * the PhoneNumber object passed in will not be modified. |
||
| 3243 | * @param PhoneNumber $number a PhoneNumber object which contains a number that is too long to be valid. |
||
| 3244 | * @return boolean true if a valid phone number can be successfully extracted. |
||
| 3245 | */ |
||
| 3246 | 1 | public function truncateTooLongNumber(PhoneNumber $number) |
|
| 3264 | } |
||
| 3265 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.