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 calling codes that have geographically assigned mobile numbers. This may not be |
||
| 161 | * complete; we add calling codes case by case, as we find geographical mobile numbers or hear |
||
| 162 | * from user reports. |
||
| 163 | * |
||
| 164 | * @var array |
||
| 165 | */ |
||
| 166 | protected static $GEO_MOBILE_COUNTRIES; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * For performance reasons, amalgamate both into one map. |
||
| 170 | * @var array |
||
| 171 | */ |
||
| 172 | protected static $ALPHA_PHONE_MAPPINGS; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Separate map of all symbols that we wish to retain when formatting alpha numbers. This |
||
| 176 | * includes digits, ASCII letters and number grouping symbols such as "-" and " ". |
||
| 177 | * @var array |
||
| 178 | */ |
||
| 179 | protected static $ALL_PLUS_NUMBER_GROUPING_SYMBOLS; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Simple ASCII digits map used to populate ALPHA_PHONE_MAPPINGS and |
||
| 183 | * ALL_PLUS_NUMBER_GROUPING_SYMBOLS. |
||
| 184 | * @var array |
||
| 185 | */ |
||
| 186 | protected static $asciiDigitMappings = array( |
||
| 187 | '0' => '0', |
||
| 188 | '1' => '1', |
||
| 189 | '2' => '2', |
||
| 190 | '3' => '3', |
||
| 191 | '4' => '4', |
||
| 192 | '5' => '5', |
||
| 193 | '6' => '6', |
||
| 194 | '7' => '7', |
||
| 195 | '8' => '8', |
||
| 196 | '9' => '9', |
||
| 197 | ); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Regexp of all possible ways to write extensions, for use when parsing. This will be run as a |
||
| 201 | * case-insensitive regexp match. Wide character versions are also provided after each ASCII |
||
| 202 | * version. |
||
| 203 | * @var String |
||
| 204 | */ |
||
| 205 | protected static $EXTN_PATTERNS_FOR_PARSING; |
||
| 206 | protected static $EXTN_PATTERN = null; |
||
| 207 | protected static $VALID_PHONE_NUMBER_PATTERN; |
||
| 208 | protected static $MIN_LENGTH_PHONE_NUMBER_PATTERN; |
||
| 209 | /** |
||
| 210 | * Regular expression of viable phone numbers. This is location independent. Checks we have at |
||
| 211 | * least three leading digits, and only valid punctuation, alpha characters and |
||
| 212 | * digits in the phone number. Does not include extension data. |
||
| 213 | * The symbol 'x' is allowed here as valid punctuation since it is often used as a placeholder for |
||
| 214 | * carrier codes, for example in Brazilian phone numbers. We also allow multiple "+" characters at |
||
| 215 | * the start. |
||
| 216 | * Corresponds to the following: |
||
| 217 | * [digits]{minLengthNsn}| |
||
| 218 | * plus_sign*(([punctuation]|[star])*[digits]){3,}([punctuation]|[star]|[digits]|[alpha])* |
||
| 219 | * |
||
| 220 | * The first reg-ex is to allow short numbers (two digits long) to be parsed if they are entered |
||
| 221 | * as "15" etc, but only if there is no punctuation in them. The second expression restricts the |
||
| 222 | * number of digits to three or more, but then allows them to be in international form, and to |
||
| 223 | * have alpha-characters and punctuation. |
||
| 224 | * |
||
| 225 | * Note VALID_PUNCTUATION starts with a -, so must be the first in the range. |
||
| 226 | * @var string |
||
| 227 | */ |
||
| 228 | protected static $VALID_PHONE_NUMBER; |
||
| 229 | protected static $numericCharacters = array( |
||
| 230 | "\xef\xbc\x90" => 0, |
||
| 231 | "\xef\xbc\x91" => 1, |
||
| 232 | "\xef\xbc\x92" => 2, |
||
| 233 | "\xef\xbc\x93" => 3, |
||
| 234 | "\xef\xbc\x94" => 4, |
||
| 235 | "\xef\xbc\x95" => 5, |
||
| 236 | "\xef\xbc\x96" => 6, |
||
| 237 | "\xef\xbc\x97" => 7, |
||
| 238 | "\xef\xbc\x98" => 8, |
||
| 239 | "\xef\xbc\x99" => 9, |
||
| 240 | |||
| 241 | "\xd9\xa0" => 0, |
||
| 242 | "\xd9\xa1" => 1, |
||
| 243 | "\xd9\xa2" => 2, |
||
| 244 | "\xd9\xa3" => 3, |
||
| 245 | "\xd9\xa4" => 4, |
||
| 246 | "\xd9\xa5" => 5, |
||
| 247 | "\xd9\xa6" => 6, |
||
| 248 | "\xd9\xa7" => 7, |
||
| 249 | "\xd9\xa8" => 8, |
||
| 250 | "\xd9\xa9" => 9, |
||
| 251 | |||
| 252 | "\xdb\xb0" => 0, |
||
| 253 | "\xdb\xb1" => 1, |
||
| 254 | "\xdb\xb2" => 2, |
||
| 255 | "\xdb\xb3" => 3, |
||
| 256 | "\xdb\xb4" => 4, |
||
| 257 | "\xdb\xb5" => 5, |
||
| 258 | "\xdb\xb6" => 6, |
||
| 259 | "\xdb\xb7" => 7, |
||
| 260 | "\xdb\xb8" => 8, |
||
| 261 | "\xdb\xb9" => 9, |
||
| 262 | |||
| 263 | "\xe1\xa0\x90" => 0, |
||
| 264 | "\xe1\xa0\x91" => 1, |
||
| 265 | "\xe1\xa0\x92" => 2, |
||
| 266 | "\xe1\xa0\x93" => 3, |
||
| 267 | "\xe1\xa0\x94" => 4, |
||
| 268 | "\xe1\xa0\x95" => 5, |
||
| 269 | "\xe1\xa0\x96" => 6, |
||
| 270 | "\xe1\xa0\x97" => 7, |
||
| 271 | "\xe1\xa0\x98" => 8, |
||
| 272 | "\xe1\xa0\x99" => 9, |
||
| 273 | ); |
||
| 274 | |||
| 275 | /** |
||
| 276 | * The set of county calling codes that map to the non-geo entity region ("001"). |
||
| 277 | * @var array |
||
| 278 | */ |
||
| 279 | protected $countryCodesForNonGeographicalRegion = array(); |
||
| 280 | /** |
||
| 281 | * The set of regions the library supports. |
||
| 282 | * @var array |
||
| 283 | */ |
||
| 284 | protected $supportedRegions = array(); |
||
| 285 | |||
| 286 | /** |
||
| 287 | * A mapping from a country calling code to the region codes which denote the region represented |
||
| 288 | * by that country calling code. In the case of multiple regions sharing a calling code, such as |
||
| 289 | * the NANPA regions, the one indicated with "isMainCountryForCode" in the metadata should be |
||
| 290 | * first. |
||
| 291 | * @var array |
||
| 292 | */ |
||
| 293 | protected $countryCallingCodeToRegionCodeMap = array(); |
||
| 294 | /** |
||
| 295 | * The set of regions that share country calling code 1. |
||
| 296 | * @var array |
||
| 297 | */ |
||
| 298 | protected $nanpaRegions = array(); |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @var MetadataSourceInterface |
||
| 302 | */ |
||
| 303 | protected $metadataSource; |
||
| 304 | |||
| 305 | /** |
||
| 306 | * This class implements a singleton, so the only constructor is protected. |
||
| 307 | * @param MetadataSourceInterface $metadataSource |
||
| 308 | * @param $countryCallingCodeToRegionCodeMap |
||
| 309 | */ |
||
| 310 | 402 | protected function __construct(MetadataSourceInterface $metadataSource, $countryCallingCodeToRegionCodeMap) |
|
| 311 | { |
||
| 312 | 402 | $this->metadataSource = $metadataSource; |
|
| 313 | 402 | $this->countryCallingCodeToRegionCodeMap = $countryCallingCodeToRegionCodeMap; |
|
| 314 | 402 | $this->init(); |
|
| 315 | 402 | static::initCapturingExtnDigits(); |
|
| 316 | 402 | static::initExtnPatterns(); |
|
| 317 | 402 | static::initExtnPattern(); |
|
| 318 | 402 | static::$PLUS_CHARS_PATTERN = "[" . static::PLUS_CHARS . "]+"; |
|
| 319 | 402 | static::$SEPARATOR_PATTERN = "[" . static::VALID_PUNCTUATION . "]+"; |
|
| 320 | 402 | static::$CAPTURING_DIGIT_PATTERN = "(" . static::DIGITS . ")"; |
|
| 321 | 402 | static::$VALID_START_CHAR_PATTERN = "[" . static::PLUS_CHARS . static::DIGITS . "]"; |
|
| 322 | |||
| 323 | 402 | static::$ALPHA_PHONE_MAPPINGS = static::$ALPHA_MAPPINGS + static::$asciiDigitMappings; |
|
| 324 | |||
| 325 | 402 | static::$DIALLABLE_CHAR_MAPPINGS = static::$asciiDigitMappings; |
|
| 326 | 402 | static::$DIALLABLE_CHAR_MAPPINGS[static::PLUS_SIGN] = static::PLUS_SIGN; |
|
| 327 | 402 | static::$DIALLABLE_CHAR_MAPPINGS['*'] = '*'; |
|
| 328 | |||
| 329 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS = array(); |
|
| 330 | // Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings. |
||
| 331 | 402 | foreach (static::$ALPHA_MAPPINGS as $c => $value) { |
|
| 332 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[strtolower($c)] = $c; |
|
| 333 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[$c] = $c; |
|
| 334 | 402 | } |
|
| 335 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS += static::$asciiDigitMappings; |
|
| 336 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-'; |
|
| 337 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xEF\xBC\x8D"] = '-'; |
|
| 338 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x80\x90"] = '-'; |
|
| 339 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x80\x91"] = '-'; |
|
| 340 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x80\x92"] = '-'; |
|
| 341 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x80\x93"] = '-'; |
|
| 342 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x80\x94"] = '-'; |
|
| 343 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x80\x95"] = '-'; |
|
| 344 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x88\x92"] = '-'; |
|
| 345 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/"; |
|
| 346 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xEF\xBC\x8F"] = "/"; |
|
| 347 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " "; |
|
| 348 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE3\x80\x80"] = " "; |
|
| 349 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xE2\x81\xA0"] = " "; |
|
| 350 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = "."; |
|
| 351 | 402 | static::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["\xEF\xBC\x8E"] = "."; |
|
| 352 | |||
| 353 | |||
| 354 | 402 | static::$MIN_LENGTH_PHONE_NUMBER_PATTERN = "[" . static::DIGITS . "]{" . static::MIN_LENGTH_FOR_NSN . "}"; |
|
| 355 | 402 | static::$VALID_PHONE_NUMBER = "[" . static::PLUS_CHARS . "]*(?:[" . static::VALID_PUNCTUATION . static::STAR_SIGN . "]*[" . static::DIGITS . "]){3,}[" . static::VALID_PUNCTUATION . static::STAR_SIGN . static::VALID_ALPHA . static::DIGITS . "]*"; |
|
| 356 | 402 | static::$VALID_PHONE_NUMBER_PATTERN = "%^" . static::$MIN_LENGTH_PHONE_NUMBER_PATTERN . "$|^" . static::$VALID_PHONE_NUMBER . "(?:" . static::$EXTN_PATTERNS_FOR_PARSING . ")?$%" . static::REGEX_FLAGS; |
|
| 357 | |||
| 358 | 402 | static::$UNWANTED_END_CHAR_PATTERN = "[^" . static::DIGITS . static::VALID_ALPHA . "#]+$"; |
|
| 359 | |||
| 360 | 402 | static::$MOBILE_TOKEN_MAPPINGS = array(); |
|
| 361 | 402 | static::$MOBILE_TOKEN_MAPPINGS['52'] = "1"; |
|
| 362 | 402 | static::$MOBILE_TOKEN_MAPPINGS['54'] = "9"; |
|
| 363 | |||
| 364 | 402 | static::$GEO_MOBILE_COUNTRIES = array(); |
|
| 365 | 402 | static::$GEO_MOBILE_COUNTRIES[] = 52; // Mexico |
|
| 366 | 402 | static::$GEO_MOBILE_COUNTRIES[] = 54; // Argentina |
|
| 367 | 402 | static::$GEO_MOBILE_COUNTRIES[] = 55; // Brazil |
|
| 368 | 402 | } |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting, |
||
| 372 | * parsing, or validation. The instance is loaded with phone number metadata for a number of most |
||
| 373 | * commonly used regions. |
||
| 374 | * |
||
| 375 | * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance |
||
| 376 | * multiple times will only result in one instance being created. |
||
| 377 | * |
||
| 378 | * @param string $baseFileLocation |
||
| 379 | * @param array|null $countryCallingCodeToRegionCodeMap |
||
| 380 | * @param MetadataLoaderInterface|null $metadataLoader |
||
| 381 | * @param MetadataSourceInterface|null $metadataSource |
||
| 382 | * @return PhoneNumberUtil instance |
||
| 383 | */ |
||
| 384 | 5530 | public static function getInstance($baseFileLocation = self::META_DATA_FILE_PREFIX, array $countryCallingCodeToRegionCodeMap = null, MetadataLoaderInterface $metadataLoader = null, MetadataSourceInterface $metadataSource = null) |
|
| 385 | { |
||
| 386 | 5530 | if (static::$instance === null) { |
|
| 387 | 402 | if ($countryCallingCodeToRegionCodeMap === null) { |
|
| 388 | 267 | $countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap; |
|
| 389 | 267 | } |
|
| 390 | |||
| 391 | 402 | if ($metadataLoader === null) { |
|
| 392 | 402 | $metadataLoader = new DefaultMetadataLoader(); |
|
| 393 | 402 | } |
|
| 394 | |||
| 395 | 402 | if ($metadataSource === null) { |
|
| 396 | 402 | $metadataSource = new MultiFileMetadataSourceImpl($metadataLoader, __DIR__ . '/data/' . $baseFileLocation); |
|
| 397 | 402 | } |
|
| 398 | |||
| 399 | 402 | static::$instance = new static($metadataSource, $countryCallingCodeToRegionCodeMap); |
|
| 400 | 402 | } |
|
| 401 | 5530 | return static::$instance; |
|
| 402 | } |
||
| 403 | |||
| 404 | 402 | protected function init() |
|
| 405 | { |
||
| 406 | 402 | foreach ($this->countryCallingCodeToRegionCodeMap as $countryCode => $regionCodes) { |
|
| 407 | // We can assume that if the country calling code maps to the non-geo entity region code then |
||
| 408 | // that's the only region code it maps to. |
||
| 409 | 402 | if (count($regionCodes) == 1 && static::REGION_CODE_FOR_NON_GEO_ENTITY === $regionCodes[0]) { |
|
| 410 | // This is the subset of all country codes that map to the non-geo entity region code. |
||
| 411 | 402 | $this->countryCodesForNonGeographicalRegion[] = $countryCode; |
|
| 412 | 402 | } else { |
|
| 413 | // The supported regions set does not include the "001" non-geo entity region code. |
||
| 414 | 402 | $this->supportedRegions = array_merge($this->supportedRegions, $regionCodes); |
|
| 415 | } |
||
| 416 | 402 | } |
|
| 417 | // If the non-geo entity still got added to the set of supported regions it must be because |
||
| 418 | // there are entries that list the non-geo entity alongside normal regions (which is wrong). |
||
| 419 | // If we discover this, remove the non-geo entity from the set of supported regions and log. |
||
| 420 | 402 | $idx_region_code_non_geo_entity = array_search(static::REGION_CODE_FOR_NON_GEO_ENTITY, $this->supportedRegions); |
|
| 421 | 402 | if ($idx_region_code_non_geo_entity !== false) { |
|
| 422 | unset($this->supportedRegions[$idx_region_code_non_geo_entity]); |
||
| 423 | } |
||
| 424 | 402 | $this->nanpaRegions = $this->countryCallingCodeToRegionCodeMap[static::NANPA_COUNTRY_CODE]; |
|
| 425 | 402 | } |
|
| 426 | |||
| 427 | 402 | protected static function initCapturingExtnDigits() |
|
| 428 | { |
||
| 429 | 402 | static::$CAPTURING_EXTN_DIGITS = "(" . static::DIGITS . "{1,7})"; |
|
| 430 | 402 | } |
|
| 431 | |||
| 432 | 402 | protected static function initExtnPatterns() |
|
| 433 | { |
||
| 434 | // One-character symbols that can be used to indicate an extension. |
||
| 435 | 402 | $singleExtnSymbolsForMatching = "x\xEF\xBD\x98#\xEF\xBC\x83~\xEF\xBD\x9E"; |
|
| 436 | // For parsing, we are slightly more lenient in our interpretation than for matching. Here we |
||
| 437 | // allow a "comma" as a possible extension indicator. When matching, this is hardly ever used to |
||
| 438 | // indicate this. |
||
| 439 | 402 | $singleExtnSymbolsForParsing = "," . $singleExtnSymbolsForMatching; |
|
| 440 | |||
| 441 | 402 | static::$EXTN_PATTERNS_FOR_PARSING = static::createExtnPattern($singleExtnSymbolsForParsing); |
|
| 442 | 402 | } |
|
| 443 | |||
| 444 | // The FIRST_GROUP_PATTERN was originally set to $1 but there are some countries for which the |
||
| 445 | // first group is not used in the national pattern (e.g. Argentina) so the $1 group does not match |
||
| 446 | // correctly. Therefore, we use \d, so that the first group actually used in the pattern will be |
||
| 447 | // matched. |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Helper initialiser method to create the regular-expression pattern to match extensions, |
||
| 451 | * allowing the one-char extension symbols provided by {@code singleExtnSymbols}. |
||
| 452 | * @param string $singleExtnSymbols |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | 402 | protected static function createExtnPattern($singleExtnSymbols) |
|
| 456 | { |
||
| 457 | // There are three regular expressions here. The first covers RFC 3966 format, where the |
||
| 458 | // extension is added using ";ext=". The second more generic one starts with optional white |
||
| 459 | // space and ends with an optional full stop (.), followed by zero or more spaces/tabs and then |
||
| 460 | // the numbers themselves. The other one covers the special case of American numbers where the |
||
| 461 | // extension is written with a hash at the end, such as "- 503#". |
||
| 462 | // Note that the only capturing groups should be around the digits that you want to capture as |
||
| 463 | // part of the extension, or else parsing will fail! |
||
| 464 | // Canonical-equivalence doesn't seem to be an option with Android java, so we allow two options |
||
| 465 | // for representing the accented o - the character itself, and one in the unicode decomposed |
||
| 466 | // form with the combining acute accent. |
||
| 467 | 402 | return (static::RFC3966_EXTN_PREFIX . static::$CAPTURING_EXTN_DIGITS . "|" . "[ \xC2\xA0\\t,]*" . |
|
| 468 | 402 | "(?:e?xt(?:ensi(?:o\xCC\x81?|\xC3\xB3))?n?|(?:\xEF\xBD\x85)?\xEF\xBD\x98\xEF\xBD\x94(?:\xEF\xBD\x8E)?|" . |
|
| 469 | 402 | "[" . $singleExtnSymbols . "]|int|\xEF\xBD\x89\xEF\xBD\x8E\xEF\xBD\x94|anexo)" . |
|
| 470 | 402 | "[:\\.\xEF\xBC\x8E]?[ \xC2\xA0\\t,-]*" . static::$CAPTURING_EXTN_DIGITS . "#?|" . |
|
| 471 | 402 | "[- ]+(" . static::DIGITS . "{1,5})#"); |
|
| 472 | } |
||
| 473 | |||
| 474 | 402 | protected static function initExtnPattern() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Used for testing purposes only to reset the PhoneNumberUtil singleton to null. |
||
| 481 | */ |
||
| 482 | 402 | public static function resetInstance() |
|
| 483 | { |
||
| 484 | 402 | static::$instance = null; |
|
| 485 | 402 | } |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Converts all alpha characters in a number to their respective digits on a keypad, but retains |
||
| 489 | * existing formatting. |
||
| 490 | * @param string $number |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | 1 | public static function convertAlphaCharactersInNumber($number) |
|
| 494 | { |
||
| 495 | 1 | return static::normalizeHelper($number, static::$ALPHA_PHONE_MAPPINGS, false); |
|
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Normalizes a string of characters representing a phone number by replacing all characters found |
||
| 500 | * in the accompanying map with the values therein, and stripping all other characters if |
||
| 501 | * removeNonMatches is true. |
||
| 502 | * |
||
| 503 | * @param string $number a string of characters representing a phone number |
||
| 504 | * @param array $normalizationReplacements a mapping of characters to what they should be replaced by in |
||
| 505 | * the normalized version of the phone number |
||
| 506 | * @param bool $removeNonMatches indicates whether characters that are not able to be replaced |
||
| 507 | * should be stripped from the number. If this is false, they will be left unchanged in the number. |
||
| 508 | * @return string the normalized string version of the phone number |
||
| 509 | */ |
||
| 510 | 11 | protected static function normalizeHelper($number, array $normalizationReplacements, $removeNonMatches) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Helper function to check if the national prefix formatting rule has the first group only, i.e., |
||
| 530 | * does not start with the national prefix. |
||
| 531 | * @param string $nationalPrefixFormattingRule |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | public static function formattingRuleHasFirstGroupOnly($nationalPrefixFormattingRule) |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Convenience method to get a list of what regions the library has metadata for. |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | 249 | public function getSupportedRegions() |
|
| 545 | { |
||
| 546 | 249 | return $this->supportedRegions; |
|
| 547 | } |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Convenience method to get a list of what global network calling codes the library has metadata |
||
| 551 | * for. |
||
| 552 | * @return array |
||
| 553 | */ |
||
| 554 | 5 | public function getSupportedGlobalNetworkCallingCodes() |
|
| 555 | { |
||
| 556 | 5 | return $this->countryCodesForNonGeographicalRegion; |
|
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Gets the length of the geographical area code from the {@code nationalNumber} field of the |
||
| 561 | * PhoneNumber object passed in, so that clients could use it to split a national significant |
||
| 562 | * number into geographical area code and subscriber number. It works in such a way that the |
||
| 563 | * resultant subscriber number should be diallable, at least on some devices. An example of how |
||
| 564 | * this could be used: |
||
| 565 | * |
||
| 566 | * <code> |
||
| 567 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 568 | * $number = $phoneUtil->parse("16502530000", "US"); |
||
| 569 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 570 | * |
||
| 571 | * $areaCodeLength = $phoneUtil->getLengthOfGeographicalAreaCode($number); |
||
| 572 | * if ($areaCodeLength > 0) |
||
| 573 | * { |
||
| 574 | * $areaCode = substr($nationalSignificantNumber, 0,$areaCodeLength); |
||
| 575 | * $subscriberNumber = substr($nationalSignificantNumber, $areaCodeLength); |
||
| 576 | * } else { |
||
| 577 | * $areaCode = ""; |
||
| 578 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 579 | * } |
||
| 580 | * </code> |
||
| 581 | * |
||
| 582 | * N.B.: area code is a very ambiguous concept, so the I18N team generally recommends against |
||
| 583 | * using it for most purposes, but recommends using the more general {@code nationalNumber} |
||
| 584 | * instead. Read the following carefully before deciding to use this method: |
||
| 585 | * <ul> |
||
| 586 | * <li> geographical area codes change over time, and this method honors those changes; |
||
| 587 | * therefore, it doesn't guarantee the stability of the result it produces. |
||
| 588 | * <li> subscriber numbers may not be diallable from all devices (notably mobile devices, which |
||
| 589 | * typically requires the full national_number to be dialled in most regions). |
||
| 590 | * <li> most non-geographical numbers have no area codes, including numbers from non-geographical |
||
| 591 | * entities |
||
| 592 | * <li> some geographical numbers have no area codes. |
||
| 593 | * </ul> |
||
| 594 | * @param PhoneNumber $number PhoneNumber object for which clients want to know the length of the area code. |
||
| 595 | * @return int the length of area code of the PhoneNumber object passed in. |
||
| 596 | */ |
||
| 597 | 1 | public function getLengthOfGeographicalAreaCode(PhoneNumber $number) |
|
| 598 | { |
||
| 599 | 1 | $metadata = $this->getMetadataForRegion($this->getRegionCodeForNumber($number)); |
|
| 600 | 1 | if ($metadata === null) { |
|
| 601 | 1 | return 0; |
|
| 602 | } |
||
| 603 | // If a country doesn't use a national prefix, and this number doesn't have an Italian leading |
||
| 604 | // zero, we assume it is a closed dialling plan with no area codes. |
||
| 605 | 1 | if (!$metadata->hasNationalPrefix() && !$number->isItalianLeadingZero()) { |
|
|
|
|||
| 606 | 1 | return 0; |
|
| 607 | } |
||
| 608 | |||
| 609 | 1 | if (!$this->isNumberGeographical($number)) { |
|
| 610 | 1 | return 0; |
|
| 611 | } |
||
| 612 | |||
| 613 | 1 | return $this->getLengthOfNationalDestinationCode($number); |
|
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Returns the metadata for the given region code or {@code null} if the region code is invalid |
||
| 618 | * or unknown. |
||
| 619 | * @param string $regionCode |
||
| 620 | * @return PhoneMetadata |
||
| 621 | */ |
||
| 622 | 4383 | public function getMetadataForRegion($regionCode) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Helper function to check region code is not unknown or null. |
||
| 633 | * @param string $regionCode |
||
| 634 | * @return bool |
||
| 635 | */ |
||
| 636 | 4383 | protected function isValidRegionCode($regionCode) |
|
| 637 | { |
||
| 638 | 4383 | return $regionCode !== null && in_array($regionCode, $this->supportedRegions); |
|
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Returns the region where a phone number is from. This could be used for geocoding at the region |
||
| 643 | * level. |
||
| 644 | * |
||
| 645 | * @param PhoneNumber $number the phone number whose origin we want to know |
||
| 646 | * @return null|string the region where the phone number is from, or null if no region matches this calling |
||
| 647 | * code |
||
| 648 | */ |
||
| 649 | 2140 | public function getRegionCodeForNumber(PhoneNumber $number) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * @param PhoneNumber $number |
||
| 665 | * @param array $regionCodes |
||
| 666 | * @return null|string |
||
| 667 | */ |
||
| 668 | 518 | protected function getRegionCodeForNumberFromRegionList(PhoneNumber $number, array $regionCodes) |
|
| 691 | |||
| 692 | /** |
||
| 693 | * Gets the national significant number of the a phone number. Note a national significant number |
||
| 694 | * doesn't contain a national prefix or any formatting. |
||
| 695 | * |
||
| 696 | * @param PhoneNumber $number the phone number for which the national significant number is needed |
||
| 697 | * @return string the national significant number of the PhoneNumber object passed in |
||
| 698 | */ |
||
| 699 | 1984 | public function getNationalSignificantNumber(PhoneNumber $number) |
|
| 710 | |||
| 711 | /** |
||
| 712 | * @param string $nationalNumber |
||
| 713 | * @param PhoneMetadata $metadata |
||
| 714 | * @return int PhoneNumberType constant |
||
| 715 | */ |
||
| 716 | 1925 | protected function getNumberTypeHelper($nationalNumber, PhoneMetadata $metadata) |
|
| 765 | |||
| 766 | /** |
||
| 767 | * @param string $nationalNumber |
||
| 768 | * @param PhoneNumberDesc $numberDesc |
||
| 769 | * @return bool |
||
| 770 | */ |
||
| 771 | 1948 | public function isNumberMatchingDesc($nationalNumber, PhoneNumberDesc $numberDesc) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * |
||
| 780 | * Helper method to check whether a number is too short to be a regular length phone number in a |
||
| 781 | * region. |
||
| 782 | * |
||
| 783 | * @param PhoneMetadata $regionMetadata |
||
| 784 | * @param string $number |
||
| 785 | * @return bool |
||
| 786 | */ |
||
| 787 | 2726 | protected function isShorterThanPossibleNormalNumber(PhoneMetadata $regionMetadata, $number) |
|
| 792 | |||
| 793 | /** |
||
| 794 | * @param string $nationalNumber |
||
| 795 | * @param PhoneNumberDesc $numberDesc |
||
| 796 | * @return bool |
||
| 797 | */ |
||
| 798 | 1948 | public function isNumberPossibleForDesc($nationalNumber, PhoneNumberDesc $numberDesc) |
|
| 804 | |||
| 805 | /** |
||
| 806 | * Tests whether a phone number has a geographical association. It checks if the number is |
||
| 807 | * associated to a certain region in the country where it belongs to. Note that this doesn't |
||
| 808 | * verify if the number is actually in use. |
||
| 809 | * @param PhoneNumber $phoneNumber |
||
| 810 | * @return bool |
||
| 811 | */ |
||
| 812 | 2 | public function isNumberGeographical(PhoneNumber $phoneNumber) |
|
| 821 | |||
| 822 | /** |
||
| 823 | * Gets the type of a phone number. |
||
| 824 | * @param PhoneNumber $number the number the phone number that we want to know the type |
||
| 825 | * @return int PhoneNumberType the type of the phone number |
||
| 826 | */ |
||
| 827 | 1359 | public function getNumberType(PhoneNumber $number) |
|
| 837 | |||
| 838 | /** |
||
| 839 | * @param int $countryCallingCode |
||
| 840 | * @param string $regionCode |
||
| 841 | * @return PhoneMetadata |
||
| 842 | */ |
||
| 843 | 1916 | protected function getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode) |
|
| 848 | |||
| 849 | /** |
||
| 850 | * @param int $countryCallingCode |
||
| 851 | * @return PhoneMetadata |
||
| 852 | */ |
||
| 853 | 35 | public function getMetadataForNonGeographicalRegion($countryCallingCode) |
|
| 860 | |||
| 861 | /** |
||
| 862 | * Gets the length of the national destination code (NDC) from the PhoneNumber object passed in, |
||
| 863 | * so that clients could use it to split a national significant number into NDC and subscriber |
||
| 864 | * number. The NDC of a phone number is normally the first group of digit(s) right after the |
||
| 865 | * country calling code when the number is formatted in the international format, if there is a |
||
| 866 | * subscriber number part that follows. An example of how this could be used: |
||
| 867 | * |
||
| 868 | * <code> |
||
| 869 | * $phoneUtil = PhoneNumberUtil::getInstance(); |
||
| 870 | * $number = $phoneUtil->parse("18002530000", "US"); |
||
| 871 | * $nationalSignificantNumber = $phoneUtil->getNationalSignificantNumber($number); |
||
| 872 | * |
||
| 873 | * $nationalDestinationCodeLength = $phoneUtil->getLengthOfNationalDestinationCode($number); |
||
| 874 | * if ($nationalDestinationCodeLength > 0) { |
||
| 875 | * $nationalDestinationCode = substr($nationalSignificantNumber, 0, $nationalDestinationCodeLength); |
||
| 876 | * $subscriberNumber = substr($nationalSignificantNumber, $nationalDestinationCodeLength); |
||
| 877 | * } else { |
||
| 878 | * $nationalDestinationCode = ""; |
||
| 879 | * $subscriberNumber = $nationalSignificantNumber; |
||
| 880 | * } |
||
| 881 | * </code> |
||
| 882 | * |
||
| 883 | * Refer to the unit tests to see the difference between this function and |
||
| 884 | * {@link #getLengthOfGeographicalAreaCode}. |
||
| 885 | * |
||
| 886 | * @param PhoneNumber $number the PhoneNumber object for which clients want to know the length of the NDC. |
||
| 887 | * @return int the length of NDC of the PhoneNumber object passed in. |
||
| 888 | */ |
||
| 889 | 2 | public function getLengthOfNationalDestinationCode(PhoneNumber $number) |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Formats a phone number in the specified format using default rules. Note that this does not |
||
| 929 | * promise to produce a phone number that the user can dial from where they are - although we do |
||
| 930 | * format in either 'national' or 'international' format depending on what the client asks for, we |
||
| 931 | * do not currently support a more abbreviated format, such as for users in the same "area" who |
||
| 932 | * could potentially dial the number without area code. Note that if the phone number has a |
||
| 933 | * country calling code of 0 or an otherwise invalid country calling code, we cannot work out |
||
| 934 | * which formatting rules to apply so we return the national significant number with no formatting |
||
| 935 | * applied. |
||
| 936 | * |
||
| 937 | * @param PhoneNumber $number the phone number to be formatted |
||
| 938 | * @param int $numberFormat the PhoneNumberFormat the phone number should be formatted into |
||
| 939 | * @return string the formatted phone number |
||
| 940 | */ |
||
| 941 | 289 | public function format(PhoneNumber $number, $numberFormat) |
|
| 942 | { |
||
| 943 | 289 | if ($number->getNationalNumber() == 0 && $number->hasRawInput()) { |
|
| 944 | // Unparseable numbers that kept their raw input just use that. |
||
| 945 | // This is the only case where a number can be formatted as E164 without a |
||
| 946 | // leading '+' symbol (but the original number wasn't parseable anyway). |
||
| 947 | // TODO: Consider removing the 'if' above so that unparseable |
||
| 948 | // strings without raw input format to the empty string instead of "+00" |
||
| 949 | 1 | $rawInput = $number->getRawInput(); |
|
| 950 | 1 | if (mb_strlen($rawInput) > 0) { |
|
| 951 | 1 | return $rawInput; |
|
| 952 | } |
||
| 953 | } |
||
| 954 | 289 | $metadata = null; |
|
| 955 | 289 | $formattedNumber = ""; |
|
| 956 | 289 | $countryCallingCode = $number->getCountryCode(); |
|
| 957 | 289 | $nationalSignificantNumber = $this->getNationalSignificantNumber($number); |
|
| 958 | 289 | if ($numberFormat == PhoneNumberFormat::E164) { |
|
| 959 | // Early exit for E164 case (even if the country calling code is invalid) since no formatting |
||
| 960 | // of the national number needs to be applied. Extensions are not formatted. |
||
| 961 | 265 | $formattedNumber .= $nationalSignificantNumber; |
|
| 962 | 265 | $this->prefixNumberWithCountryCallingCode($countryCallingCode, PhoneNumberFormat::E164, $formattedNumber); |
|
| 963 | 289 | } elseif (!$this->hasValidCountryCallingCode($countryCallingCode)) { |
|
| 964 | 1 | $formattedNumber .= $nationalSignificantNumber; |
|
| 965 | 1 | } else { |
|
| 966 | // Note getRegionCodeForCountryCode() is used because formatting information for regions which |
||
| 967 | // share a country calling code is contained by only one region for performance reasons. For |
||
| 968 | // example, for NANPA regions it will be contained in the metadata for US. |
||
| 969 | 41 | $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode); |
|
| 970 | // Metadata cannot be null because the country calling code is valid (which means that the |
||
| 971 | // region code cannot be ZZ and must be one of our supported region codes). |
||
| 972 | 41 | $metadata = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode); |
|
| 973 | 41 | $formattedNumber .= $this->formatNsn($nationalSignificantNumber, $metadata, $numberFormat); |
|
| 974 | 41 | $this->prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, $formattedNumber); |
|
| 975 | } |
||
| 976 | 289 | $this->maybeAppendFormattedExtension($number, $metadata, $numberFormat, $formattedNumber); |
|
| 977 | 289 | return $formattedNumber; |
|
| 978 | } |
||
| 979 | |||
| 980 | /** |
||
| 981 | * A helper function that is used by format and formatByPattern. |
||
| 982 | * @param int $countryCallingCode |
||
| 983 | * @param int $numberFormat PhoneNumberFormat |
||
| 984 | * @param string $formattedNumber |
||
| 985 | */ |
||
| 986 | 290 | protected function prefixNumberWithCountryCallingCode($countryCallingCode, $numberFormat, &$formattedNumber) |
|
| 987 | { |
||
| 988 | switch ($numberFormat) { |
||
| 989 | 290 | case PhoneNumberFormat::E164: |
|
| 990 | 265 | $formattedNumber = static::PLUS_SIGN . $countryCallingCode . $formattedNumber; |
|
| 991 | 265 | return; |
|
| 992 | 42 | case PhoneNumberFormat::INTERNATIONAL: |
|
| 993 | 19 | $formattedNumber = static::PLUS_SIGN . $countryCallingCode . " " . $formattedNumber; |
|
| 994 | 19 | return; |
|
| 995 | 39 | case PhoneNumberFormat::RFC3966: |
|
| 996 | 4 | $formattedNumber = static::RFC3966_PREFIX . static::PLUS_SIGN . $countryCallingCode . "-" . $formattedNumber; |
|
| 997 | 4 | return; |
|
| 998 | 39 | case PhoneNumberFormat::NATIONAL: |
|
| 999 | 39 | default: |
|
| 1000 | 39 | return; |
|
| 1001 | } |
||
| 1002 | } |
||
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Helper function to check the country calling code is valid. |
||
| 1006 | * @param int $countryCallingCode |
||
| 1007 | * @return bool |
||
| 1008 | */ |
||
| 1009 | 47 | protected function hasValidCountryCallingCode($countryCallingCode) |
|
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Returns the region code that matches the specific country calling code. In the case of no |
||
| 1016 | * region code being found, ZZ will be returned. In the case of multiple regions, the one |
||
| 1017 | * designated in the metadata as the "main" region for this calling code will be returned. If the |
||
| 1018 | * countryCallingCode entered is valid but doesn't match a specific region (such as in the case of |
||
| 1019 | * non-geographical calling codes like 800) the value "001" will be returned (corresponding to |
||
| 1020 | * the value for World in the UN M.49 schema). |
||
| 1021 | * |
||
| 1022 | * @param int $countryCallingCode |
||
| 1023 | * @return string |
||
| 1024 | */ |
||
| 1025 | 336 | public function getRegionCodeForCountryCode($countryCallingCode) |
|
| 1026 | { |
||
| 1027 | 336 | $regionCodes = isset($this->countryCallingCodeToRegionCodeMap[$countryCallingCode]) ? $this->countryCallingCodeToRegionCodeMap[$countryCallingCode] : null; |
|
| 1028 | 336 | return $regionCodes === null ? static::UNKNOWN_REGION : $regionCodes[0]; |
|
| 1029 | } |
||
| 1030 | |||
| 1031 | /** |
||
| 1032 | * Note in some regions, the national number can be written in two completely different ways |
||
| 1033 | * depending on whether it forms part of the NATIONAL format or INTERNATIONAL format. The |
||
| 1034 | * numberFormat parameter here is used to specify which format to use for those cases. If a |
||
| 1035 | * carrierCode is specified, this will be inserted into the formatted string to replace $CC. |
||
| 1036 | * @param string $number |
||
| 1037 | * @param PhoneMetadata $metadata |
||
| 1038 | * @param int $numberFormat PhoneNumberFormat |
||
| 1039 | * @param null|string $carrierCode |
||
| 1040 | * @return string |
||
| 1041 | */ |
||
| 1042 | 42 | protected function formatNsn($number, PhoneMetadata $metadata, $numberFormat, $carrierCode = null) |
|
| 1043 | { |
||
| 1044 | 42 | $intlNumberFormats = $metadata->intlNumberFormats(); |
|
| 1045 | // When the intlNumberFormats exists, we use that to format national number for the |
||
| 1046 | // INTERNATIONAL format instead of using the numberDesc.numberFormats. |
||
| 1047 | 42 | $availableFormats = (count($intlNumberFormats) == 0 || $numberFormat == PhoneNumberFormat::NATIONAL) |
|
| 1048 | 42 | ? $metadata->numberFormats() |
|
| 1049 | 42 | : $metadata->intlNumberFormats(); |
|
| 1050 | 42 | $formattingPattern = $this->chooseFormattingPatternForNumber($availableFormats, $number); |
|
| 1051 | 42 | return ($formattingPattern === null) |
|
| 1052 | 42 | ? $number |
|
| 1053 | 42 | : $this->formatNsnUsingPattern($number, $formattingPattern, $numberFormat, $carrierCode); |
|
| 1054 | } |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * @param NumberFormat[] $availableFormats |
||
| 1058 | * @param string $nationalNumber |
||
| 1059 | * @return NumberFormat|null |
||
| 1060 | */ |
||
| 1061 | 43 | public function chooseFormattingPatternForNumber(array $availableFormats, $nationalNumber) |
|
| 1062 | { |
||
| 1063 | 43 | foreach ($availableFormats as $numFormat) { |
|
| 1064 | 43 | $leadingDigitsPatternMatcher = null; |
|
| 1065 | 43 | $size = $numFormat->leadingDigitsPatternSize(); |
|
| 1066 | // We always use the last leading_digits_pattern, as it is the most detailed. |
||
| 1067 | 43 | if ($size > 0) { |
|
| 1068 | 38 | $leadingDigitsPatternMatcher = new Matcher( |
|
| 1069 | 38 | $numFormat->getLeadingDigitsPattern($size - 1), |
|
| 1070 | $nationalNumber |
||
| 1071 | 38 | ); |
|
| 1072 | 38 | } |
|
| 1073 | 43 | if ($size == 0 || $leadingDigitsPatternMatcher->lookingAt()) { |
|
| 1074 | 42 | $m = new Matcher($numFormat->getPattern(), $nationalNumber); |
|
| 1075 | 42 | if ($m->matches() > 0) { |
|
| 1076 | 42 | return $numFormat; |
|
| 1077 | } |
||
| 1078 | 12 | } |
|
| 1079 | 32 | } |
|
| 1080 | 9 | return null; |
|
| 1081 | } |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * Note that carrierCode is optional - if null or an empty string, no carrier code replacement |
||
| 1085 | * will take place. |
||
| 1086 | * @param string $nationalNumber |
||
| 1087 | * @param NumberFormat $formattingPattern |
||
| 1088 | * @param int $numberFormat PhoneNumberFormat |
||
| 1089 | * @param null|string $carrierCode |
||
| 1090 | * @return string |
||
| 1091 | */ |
||
| 1092 | 42 | protected function formatNsnUsingPattern( |
|
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Appends the formatted extension of a phone number to formattedNumber, if the phone number had |
||
| 1143 | * an extension specified. |
||
| 1144 | * |
||
| 1145 | * @param PhoneNumber $number |
||
| 1146 | * @param PhoneMetadata|null $metadata |
||
| 1147 | * @param int $numberFormat PhoneNumberFormat |
||
| 1148 | * @param string $formattedNumber |
||
| 1149 | */ |
||
| 1150 | 291 | protected function maybeAppendFormattedExtension(PhoneNumber $number, $metadata, $numberFormat, &$formattedNumber) |
|
| 1164 | |||
| 1165 | /** |
||
| 1166 | * Returns the mobile token for the provided country calling code if it has one, otherwise |
||
| 1167 | * returns an empty string. A mobile token is a number inserted before the area code when dialing |
||
| 1168 | * a mobile number from that country from abroad. |
||
| 1169 | * |
||
| 1170 | * @param int $countryCallingCode the country calling code for which we want the mobile token |
||
| 1171 | * @return string the mobile token, as a string, for the given country calling code |
||
| 1172 | */ |
||
| 1173 | 16 | public static function getCountryMobileToken($countryCallingCode) |
|
| 1180 | |||
| 1181 | /** |
||
| 1182 | * Checks if the number is a valid vanity (alpha) number such as 800 MICROSOFT. A valid vanity |
||
| 1183 | * number will start with at least 3 digits and will have three or more alpha characters. This |
||
| 1184 | * does not do region-specific checks - to work out if this number is actually valid for a region, |
||
| 1185 | * it should be parsed and methods such as {@link #isPossibleNumberWithReason} and |
||
| 1186 | * {@link #isValidNumber} should be used. |
||
| 1187 | * |
||
| 1188 | * @param string $number the number that needs to be checked |
||
| 1189 | * @return bool true if the number is a valid vanity number |
||
| 1190 | */ |
||
| 1191 | 1 | public function isAlphaNumber($number) |
|
| 1192 | { |
||
| 1193 | 1 | if (!$this->isViablePhoneNumber($number)) { |
|
| 1194 | // Number is too short, or doesn't match the basic phone number pattern. |
||
| 1195 | 1 | return false; |
|
| 1196 | } |
||
| 1197 | 1 | $this->maybeStripExtension($number); |
|
| 1198 | 1 | return (bool)preg_match('/' . static::VALID_ALPHA_PHONE_PATTERN . '/' . static::REGEX_FLAGS, $number); |
|
| 1199 | } |
||
| 1200 | |||
| 1201 | /** |
||
| 1202 | * Checks to see if the string of characters could possibly be a phone number at all. At the |
||
| 1203 | * moment, checks to see that the string begins with at least 2 digits, ignoring any punctuation |
||
| 1204 | * commonly found in phone numbers. |
||
| 1205 | * This method does not require the number to be normalized in advance - but does assume that |
||
| 1206 | * leading non-number symbols have been removed, such as by the method extractPossibleNumber. |
||
| 1207 | * |
||
| 1208 | * @param string $number to be checked for viability as a phone number |
||
| 1209 | * @return boolean true if the number could be a phone number of some sort, otherwise false |
||
| 1210 | */ |
||
| 1211 | 2730 | public static function isViablePhoneNumber($number) |
|
| 1212 | { |
||
| 1213 | 2730 | if (mb_strlen($number) < static::MIN_LENGTH_FOR_NSN) { |
|
| 1214 | 4 | return false; |
|
| 1215 | } |
||
| 1216 | |||
| 1217 | 2730 | $validPhoneNumberPattern = static::getValidPhoneNumberPattern(); |
|
| 1218 | |||
| 1219 | 2730 | $m = preg_match($validPhoneNumberPattern, $number); |
|
| 1220 | 2730 | return $m > 0; |
|
| 1221 | } |
||
| 1222 | |||
| 1223 | /** |
||
| 1224 | * We append optionally the extension pattern to the end here, as a valid phone number may |
||
| 1225 | * have an extension prefix appended, followed by 1 or more digits. |
||
| 1226 | * @return string |
||
| 1227 | */ |
||
| 1228 | 2730 | protected static function getValidPhoneNumberPattern() |
|
| 1229 | { |
||
| 1230 | 2730 | return static::$VALID_PHONE_NUMBER_PATTERN; |
|
| 1231 | } |
||
| 1232 | |||
| 1233 | /** |
||
| 1234 | * Strips any extension (as in, the part of the number dialled after the call is connected, |
||
| 1235 | * usually indicated with extn, ext, x or similar) from the end of the number, and returns it. |
||
| 1236 | * |
||
| 1237 | * @param string $number the non-normalized telephone number that we wish to strip the extension from |
||
| 1238 | * @return string the phone extension |
||
| 1239 | */ |
||
| 1240 | 2727 | protected function maybeStripExtension(&$number) |
|
| 1261 | |||
| 1262 | /** |
||
| 1263 | * Parses a string and returns it in proto buffer format. This method differs from {@link #parse} |
||
| 1264 | * in that it always populates the raw_input field of the protocol buffer with numberToParse as |
||
| 1265 | * well as the country_code_source field. |
||
| 1266 | * |
||
| 1267 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
||
| 1268 | * such as +, ( and -, as well as a phone number extension. It can also |
||
| 1269 | * be provided in RFC3966 format. |
||
| 1270 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
||
| 1271 | * if the number being parsed is not written in international format. |
||
| 1272 | * The country calling code for the number in this case would be stored |
||
| 1273 | * as that of the default region supplied. |
||
| 1274 | * @param PhoneNumber $phoneNumber |
||
| 1275 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 1276 | */ |
||
| 1277 | 3 | public function parseAndKeepRawInput($numberToParse, $defaultRegion, PhoneNumber $phoneNumber = null) |
|
| 1285 | |||
| 1286 | /** |
||
| 1287 | * A helper function to set the values related to leading zeros in a PhoneNumber. |
||
| 1288 | * @param string $nationalNumber |
||
| 1289 | * @param PhoneNumber $phoneNumber |
||
| 1290 | */ |
||
| 1291 | 2724 | public static function setItalianLeadingZerosForPhoneNumber($nationalNumber, PhoneNumber $phoneNumber) |
|
| 1308 | |||
| 1309 | /** |
||
| 1310 | * Parses a string and fills up the phoneNumber. This method is the same as the public |
||
| 1311 | * parse() method, with the exception that it allows the default region to be null, for use by |
||
| 1312 | * isNumberMatch(). checkRegion should be set to false if it is permitted for the default region |
||
| 1313 | * to be null or unknown ("ZZ"). |
||
| 1314 | * @param string $numberToParse |
||
| 1315 | * @param string $defaultRegion |
||
| 1316 | * @param bool $keepRawInput |
||
| 1317 | * @param bool $checkRegion |
||
| 1318 | * @param PhoneNumber $phoneNumber |
||
| 1319 | * @throws NumberParseException |
||
| 1320 | */ |
||
| 1321 | 2729 | protected function parseHelper($numberToParse, $defaultRegion, $keepRawInput, $checkRegion, PhoneNumber $phoneNumber) |
|
| 1322 | { |
||
| 1323 | 2729 | if ($numberToParse === null) { |
|
| 1324 | 2 | throw new NumberParseException(NumberParseException::NOT_A_NUMBER, "The phone number supplied was null."); |
|
| 1325 | } |
||
| 1326 | |||
| 1327 | 2728 | $numberToParse = trim($numberToParse); |
|
| 1328 | |||
| 1329 | 2728 | if (mb_strlen($numberToParse) > static::MAX_INPUT_STRING_LENGTH) { |
|
| 1330 | 1 | throw new NumberParseException( |
|
| 1331 | 1 | NumberParseException::TOO_LONG, |
|
| 1332 | "The string supplied was too long to parse." |
||
| 1333 | 1 | ); |
|
| 1334 | } |
||
| 1335 | |||
| 1336 | 2727 | $nationalNumber = ''; |
|
| 1337 | 2727 | $this->buildNationalNumberForParsing($numberToParse, $nationalNumber); |
|
| 1338 | |||
| 1339 | 2727 | if (!$this->isViablePhoneNumber($nationalNumber)) { |
|
| 1340 | 4 | throw new NumberParseException( |
|
| 1341 | 4 | NumberParseException::NOT_A_NUMBER, |
|
| 1342 | "The string supplied did not seem to be a phone number." |
||
| 1343 | 4 | ); |
|
| 1344 | } |
||
| 1345 | |||
| 1346 | // Check the region supplied is valid, or that the extracted number starts with some sort of + |
||
| 1347 | // sign so the number's region can be determined. |
||
| 1348 | 2726 | if ($checkRegion && !$this->checkRegionForParsing($nationalNumber, $defaultRegion)) { |
|
| 1349 | 5 | throw new NumberParseException( |
|
| 1350 | 5 | NumberParseException::INVALID_COUNTRY_CODE, |
|
| 1351 | "Missing or invalid default region." |
||
| 1352 | 5 | ); |
|
| 1353 | } |
||
| 1354 | |||
| 1355 | 2726 | if ($keepRawInput) { |
|
| 1356 | 3 | $phoneNumber->setRawInput($numberToParse); |
|
| 1357 | 3 | } |
|
| 1358 | // Attempt to parse extension first, since it doesn't require region-specific data and we want |
||
| 1359 | // to have the non-normalised number here. |
||
| 1360 | 2726 | $extension = $this->maybeStripExtension($nationalNumber); |
|
| 1361 | 2726 | if (mb_strlen($extension) > 0) { |
|
| 1362 | 4 | $phoneNumber->setExtension($extension); |
|
| 1363 | 4 | } |
|
| 1364 | |||
| 1365 | 2726 | $regionMetadata = $this->getMetadataForRegion($defaultRegion); |
|
| 1366 | // Check to see if the number is given in international format so we know whether this number is |
||
| 1367 | // from the default region or not. |
||
| 1368 | 2726 | $normalizedNationalNumber = ""; |
|
| 1369 | try { |
||
| 1370 | // TODO: This method should really just take in the string buffer that has already |
||
| 1371 | // been created, and just remove the prefix, rather than taking in a string and then |
||
| 1372 | // outputting a string buffer. |
||
| 1373 | 2726 | $countryCode = $this->maybeExtractCountryCode( |
|
| 1374 | 2726 | $nationalNumber, |
|
| 1375 | 2726 | $regionMetadata, |
|
| 1376 | 2726 | $normalizedNationalNumber, |
|
| 1377 | 2726 | $keepRawInput, |
|
| 1378 | $phoneNumber |
||
| 1379 | 2726 | ); |
|
| 1380 | 2726 | } catch (NumberParseException $e) { |
|
| 1381 | 2 | $matcher = new Matcher(static::$PLUS_CHARS_PATTERN, $nationalNumber); |
|
| 1382 | 2 | if ($e->getErrorType() == NumberParseException::INVALID_COUNTRY_CODE && $matcher->lookingAt()) { |
|
| 1383 | // Strip the plus-char, and try again. |
||
| 1384 | 2 | $countryCode = $this->maybeExtractCountryCode( |
|
| 1385 | 2 | substr($nationalNumber, $matcher->end()), |
|
| 1386 | 2 | $regionMetadata, |
|
| 1387 | 2 | $normalizedNationalNumber, |
|
| 1388 | 2 | $keepRawInput, |
|
| 1389 | $phoneNumber |
||
| 1390 | 2 | ); |
|
| 1391 | 2 | if ($countryCode == 0) { |
|
| 1392 | 1 | throw new NumberParseException( |
|
| 1393 | 1 | NumberParseException::INVALID_COUNTRY_CODE, |
|
| 1394 | "Could not interpret numbers after plus-sign." |
||
| 1395 | 1 | ); |
|
| 1396 | } |
||
| 1397 | 1 | } else { |
|
| 1398 | 1 | throw new NumberParseException($e->getErrorType(), $e->getMessage(), $e); |
|
| 1399 | } |
||
| 1400 | } |
||
| 1401 | 2726 | if ($countryCode !== 0) { |
|
| 1402 | 288 | $phoneNumberRegion = $this->getRegionCodeForCountryCode($countryCode); |
|
| 1403 | 288 | if ($phoneNumberRegion != $defaultRegion) { |
|
| 1404 | // Metadata cannot be null because the country calling code is valid. |
||
| 1405 | 275 | $regionMetadata = $this->getMetadataForRegionOrCallingCode($countryCode, $phoneNumberRegion); |
|
| 1406 | 275 | } |
|
| 1407 | 288 | } else { |
|
| 1408 | // If no extracted country calling code, use the region supplied instead. The national number |
||
| 1409 | // is just the normalized version of the number we were given to parse. |
||
| 1410 | |||
| 1411 | 2696 | $normalizedNationalNumber .= $this->normalize($nationalNumber); |
|
| 1412 | 2696 | if ($defaultRegion !== null) { |
|
| 1413 | 2696 | $countryCode = $regionMetadata->getCountryCode(); |
|
| 1414 | 2696 | $phoneNumber->setCountryCode($countryCode); |
|
| 1415 | 2696 | } else if ($keepRawInput) { |
|
| 1416 | $phoneNumber->clearCountryCodeSource(); |
||
| 1417 | } |
||
| 1418 | } |
||
| 1419 | 2726 | if (mb_strlen($normalizedNationalNumber) < static::MIN_LENGTH_FOR_NSN) { |
|
| 1420 | 2 | throw new NumberParseException( |
|
| 1421 | 2 | NumberParseException::TOO_SHORT_NSN, |
|
| 1422 | "The string supplied is too short to be a phone number." |
||
| 1423 | 2 | ); |
|
| 1424 | } |
||
| 1425 | 2725 | if ($regionMetadata !== null) { |
|
| 1426 | 2725 | $carrierCode = ""; |
|
| 1427 | 2725 | $potentialNationalNumber = $normalizedNationalNumber; |
|
| 1428 | 2725 | $this->maybeStripNationalPrefixAndCarrierCode($potentialNationalNumber, $regionMetadata, $carrierCode); |
|
| 1429 | // We require that the NSN remaining after stripping the national prefix and carrier code be |
||
| 1430 | // of a possible length for the region. Otherwise, we don't do the stripping, since the |
||
| 1431 | // original number could be a valid short number. |
||
| 1432 | 2725 | if (!$this->isShorterThanPossibleNormalNumber($regionMetadata, $potentialNationalNumber)) { |
|
| 1433 | 2076 | $normalizedNationalNumber = $potentialNationalNumber; |
|
| 1434 | 2076 | if ($keepRawInput) { |
|
| 1435 | 3 | $phoneNumber->setPreferredDomesticCarrierCode($carrierCode); |
|
| 1436 | 3 | } |
|
| 1437 | 2076 | } |
|
| 1438 | 2725 | } |
|
| 1439 | 2725 | $lengthOfNationalNumber = mb_strlen($normalizedNationalNumber); |
|
| 1440 | 2725 | if ($lengthOfNationalNumber < static::MIN_LENGTH_FOR_NSN) { |
|
| 1441 | throw new NumberParseException( |
||
| 1442 | NumberParseException::TOO_SHORT_NSN, |
||
| 1443 | "The string supplied is too short to be a phone number." |
||
| 1444 | ); |
||
| 1445 | } |
||
| 1446 | 2725 | if ($lengthOfNationalNumber > static::MAX_LENGTH_FOR_NSN) { |
|
| 1447 | 1 | throw new NumberParseException( |
|
| 1448 | 1 | NumberParseException::TOO_LONG, |
|
| 1449 | "The string supplied is too long to be a phone number." |
||
| 1450 | 1 | ); |
|
| 1451 | } |
||
| 1452 | 2724 | $this->setItalianLeadingZerosForPhoneNumber($normalizedNationalNumber, $phoneNumber); |
|
| 1453 | |||
| 1454 | |||
| 1455 | /* |
||
| 1456 | * We have to store the National Number as a string instead of a "long" as Google do |
||
| 1457 | * |
||
| 1458 | * Since PHP doesn't always support 64 bit INTs, this was a float, but that had issues |
||
| 1459 | * with long numbers. |
||
| 1460 | * |
||
| 1461 | * We have to remove the leading zeroes ourself though |
||
| 1462 | */ |
||
| 1463 | 2724 | if ((int)$normalizedNationalNumber == 0) { |
|
| 1464 | 3 | $normalizedNationalNumber = "0"; |
|
| 1465 | 3 | } else { |
|
| 1466 | 2722 | $normalizedNationalNumber = ltrim($normalizedNationalNumber, '0'); |
|
| 1467 | } |
||
| 1468 | |||
| 1469 | 2724 | $phoneNumber->setNationalNumber($normalizedNationalNumber); |
|
| 1470 | 2724 | } |
|
| 1471 | |||
| 1472 | /** |
||
| 1473 | * Converts numberToParse to a form that we can parse and write it to nationalNumber if it is |
||
| 1474 | * written in RFC3966; otherwise extract a possible number out of it and write to nationalNumber. |
||
| 1475 | * @param string $numberToParse |
||
| 1476 | * @param string $nationalNumber |
||
| 1477 | */ |
||
| 1478 | 2727 | protected function buildNationalNumberForParsing($numberToParse, &$nationalNumber) |
|
| 1522 | |||
| 1523 | /** |
||
| 1524 | * Attempts to extract a possible number from the string passed in. This currently strips all |
||
| 1525 | * leading characters that cannot be used to start a phone number. Characters that can be used to |
||
| 1526 | * start a phone number are defined in the VALID_START_CHAR_PATTERN. If none of these characters |
||
| 1527 | * are found in the number passed in, an empty string is returned. This function also attempts to |
||
| 1528 | * strip off any alternative extensions or endings if two or more are present, such as in the case |
||
| 1529 | * of: (530) 583-6985 x302/x2303. The second extension here makes this actually two phone numbers, |
||
| 1530 | * (530) 583-6985 x302 and (530) 583-6985 x2303. We remove the second extension so that the first |
||
| 1531 | * number is parsed correctly. |
||
| 1532 | * |
||
| 1533 | * @param int $number the string that might contain a phone number |
||
| 1534 | * @return string the number, stripped of any non-phone-number prefix (such as "Tel:") or an empty |
||
| 1535 | * string if no character used to start phone numbers (such as + or any digit) is |
||
| 1536 | * found in the number |
||
| 1537 | */ |
||
| 1538 | 2765 | public static function extractPossibleNumber($number) |
|
| 1539 | { |
||
| 1540 | 2765 | $matches = array(); |
|
| 1541 | 2765 | $match = preg_match('/' . static::$VALID_START_CHAR_PATTERN . '/ui', $number, $matches, PREG_OFFSET_CAPTURE); |
|
| 1542 | 2765 | if ($match > 0) { |
|
| 1543 | 2765 | $number = substr($number, $matches[0][1]); |
|
| 1544 | // Remove trailing non-alpha non-numerical characters. |
||
| 1545 | 2765 | $trailingCharsMatcher = new Matcher(static::$UNWANTED_END_CHAR_PATTERN, $number); |
|
| 1546 | 2765 | if ($trailingCharsMatcher->find() && $trailingCharsMatcher->start() > 0) { |
|
| 1547 | 2 | $number = substr($number, 0, $trailingCharsMatcher->start()); |
|
| 1548 | 2 | } |
|
| 1549 | |||
| 1550 | // Check for extra numbers at the end. |
||
| 1551 | 2765 | $match = preg_match('%' . static::$SECOND_NUMBER_START_PATTERN . '%', $number, $matches, PREG_OFFSET_CAPTURE); |
|
| 1552 | 2765 | if ($match > 0) { |
|
| 1553 | 1 | $number = substr($number, 0, $matches[0][1]); |
|
| 1554 | 1 | } |
|
| 1555 | |||
| 1556 | 2765 | return $number; |
|
| 1557 | } else { |
||
| 1558 | 4 | return ""; |
|
| 1559 | } |
||
| 1560 | } |
||
| 1561 | |||
| 1562 | /** |
||
| 1563 | * Checks to see that the region code used is valid, or if it is not valid, that the number to |
||
| 1564 | * parse starts with a + symbol so that we can attempt to infer the region from the number. |
||
| 1565 | * Returns false if it cannot use the region provided and the region cannot be inferred. |
||
| 1566 | * @param string $numberToParse |
||
| 1567 | * @param string $defaultRegion |
||
| 1568 | * @return bool |
||
| 1569 | */ |
||
| 1570 | 2726 | protected function checkRegionForParsing($numberToParse, $defaultRegion) |
|
| 1571 | { |
||
| 1572 | 2726 | if (!$this->isValidRegionCode($defaultRegion)) { |
|
| 1573 | // If the number is null or empty, we can't infer the region. |
||
| 1574 | 269 | $plusCharsPatternMatcher = new Matcher(static::$PLUS_CHARS_PATTERN, $numberToParse); |
|
| 1575 | 269 | if ($numberToParse === null || mb_strlen($numberToParse) == 0 || !$plusCharsPatternMatcher->lookingAt()) { |
|
| 1576 | 5 | return false; |
|
| 1577 | } |
||
| 1578 | 267 | } |
|
| 1579 | 2726 | return true; |
|
| 1580 | } |
||
| 1581 | |||
| 1582 | /** |
||
| 1583 | * Tries to extract a country calling code from a number. This method will return zero if no |
||
| 1584 | * country calling code is considered to be present. Country calling codes are extracted in the |
||
| 1585 | * following ways: |
||
| 1586 | * <ul> |
||
| 1587 | * <li> by stripping the international dialing prefix of the region the person is dialing from, |
||
| 1588 | * if this is present in the number, and looking at the next digits |
||
| 1589 | * <li> by stripping the '+' sign if present and then looking at the next digits |
||
| 1590 | * <li> by comparing the start of the number and the country calling code of the default region. |
||
| 1591 | * If the number is not considered possible for the numbering plan of the default region |
||
| 1592 | * initially, but starts with the country calling code of this region, validation will be |
||
| 1593 | * reattempted after stripping this country calling code. If this number is considered a |
||
| 1594 | * possible number, then the first digits will be considered the country calling code and |
||
| 1595 | * removed as such. |
||
| 1596 | * </ul> |
||
| 1597 | * It will throw a NumberParseException if the number starts with a '+' but the country calling |
||
| 1598 | * code supplied after this does not match that of any known region. |
||
| 1599 | * |
||
| 1600 | * @param string $number non-normalized telephone number that we wish to extract a country calling |
||
| 1601 | * code from - may begin with '+' |
||
| 1602 | * @param PhoneMetadata $defaultRegionMetadata metadata about the region this number may be from |
||
| 1603 | * @param string $nationalNumber a string buffer to store the national significant number in, in the case |
||
| 1604 | * that a country calling code was extracted. The number is appended to any existing contents. |
||
| 1605 | * If no country calling code was extracted, this will be left unchanged. |
||
| 1606 | * @param bool $keepRawInput true if the country_code_source and preferred_carrier_code fields of |
||
| 1607 | * phoneNumber should be populated. |
||
| 1608 | * @param PhoneNumber $phoneNumber the PhoneNumber object where the country_code and country_code_source need |
||
| 1609 | * to be populated. Note the country_code is always populated, whereas country_code_source is |
||
| 1610 | * only populated when keepCountryCodeSource is true. |
||
| 1611 | * @return int the country calling code extracted or 0 if none could be extracted |
||
| 1612 | * @throws NumberParseException |
||
| 1613 | */ |
||
| 1614 | 2727 | public function maybeExtractCountryCode( |
|
| 1615 | $number, |
||
| 1616 | PhoneMetadata $defaultRegionMetadata = null, |
||
| 1617 | &$nationalNumber, |
||
| 1618 | $keepRawInput, |
||
| 1619 | PhoneNumber $phoneNumber |
||
| 1620 | ) { |
||
| 1621 | 2727 | if (mb_strlen($number) == 0) { |
|
| 1622 | return 0; |
||
| 1623 | } |
||
| 1624 | 2727 | $fullNumber = $number; |
|
| 1625 | // Set the default prefix to be something that will never match. |
||
| 1626 | 2727 | $possibleCountryIddPrefix = "NonMatch"; |
|
| 1627 | 2727 | if ($defaultRegionMetadata !== null) { |
|
| 1628 | 2710 | $possibleCountryIddPrefix = $defaultRegionMetadata->getInternationalPrefix(); |
|
| 1629 | 2710 | } |
|
| 1630 | 2727 | $countryCodeSource = $this->maybeStripInternationalPrefixAndNormalize($fullNumber, $possibleCountryIddPrefix); |
|
| 1631 | |||
| 1632 | 2727 | if ($keepRawInput) { |
|
| 1633 | 4 | $phoneNumber->setCountryCodeSource($countryCodeSource); |
|
| 1634 | 4 | } |
|
| 1635 | 2727 | if ($countryCodeSource != CountryCodeSource::FROM_DEFAULT_COUNTRY) { |
|
| 1636 | 285 | if (mb_strlen($fullNumber) <= static::MIN_LENGTH_FOR_NSN) { |
|
| 1637 | 1 | throw new NumberParseException( |
|
| 1638 | 1 | NumberParseException::TOO_SHORT_AFTER_IDD, |
|
| 1639 | "Phone number had an IDD, but after this was not long enough to be a viable phone number." |
||
| 1640 | 1 | ); |
|
| 1641 | } |
||
| 1642 | 285 | $potentialCountryCode = $this->extractCountryCode($fullNumber, $nationalNumber); |
|
| 1643 | |||
| 1644 | 285 | if ($potentialCountryCode != 0) { |
|
| 1645 | 285 | $phoneNumber->setCountryCode($potentialCountryCode); |
|
| 1646 | 285 | return $potentialCountryCode; |
|
| 1647 | } |
||
| 1648 | |||
| 1649 | // If this fails, they must be using a strange country calling code that we don't recognize, |
||
| 1650 | // or that doesn't exist. |
||
| 1651 | 3 | throw new NumberParseException( |
|
| 1652 | 3 | NumberParseException::INVALID_COUNTRY_CODE, |
|
| 1653 | "Country calling code supplied was not recognised." |
||
| 1654 | 3 | ); |
|
| 1655 | 2703 | } else if ($defaultRegionMetadata !== null) { |
|
| 1656 | // Check to see if the number starts with the country calling code for the default region. If |
||
| 1657 | // so, we remove the country calling code, and do some checks on the validity of the number |
||
| 1658 | // before and after. |
||
| 1659 | 2703 | $defaultCountryCode = $defaultRegionMetadata->getCountryCode(); |
|
| 1660 | 2703 | $defaultCountryCodeString = (string)$defaultCountryCode; |
|
| 1661 | 2703 | $normalizedNumber = (string)$fullNumber; |
|
| 1662 | 2703 | if (strpos($normalizedNumber, $defaultCountryCodeString) === 0) { |
|
| 1663 | 59 | $potentialNationalNumber = substr($normalizedNumber, mb_strlen($defaultCountryCodeString)); |
|
| 1664 | 59 | $generalDesc = $defaultRegionMetadata->getGeneralDesc(); |
|
| 1665 | 59 | $validNumberPattern = $generalDesc->getNationalNumberPattern(); |
|
| 1666 | // Don't need the carrier code. |
||
| 1667 | 59 | $carriercode = null; |
|
| 1668 | 59 | $this->maybeStripNationalPrefixAndCarrierCode( |
|
| 1669 | 59 | $potentialNationalNumber, |
|
| 1670 | 59 | $defaultRegionMetadata, |
|
| 1671 | $carriercode |
||
| 1672 | 59 | ); |
|
| 1673 | 59 | $possibleNumberPattern = $generalDesc->getPossibleNumberPattern(); |
|
| 1674 | // If the number was not valid before but is valid now, or if it was too long before, we |
||
| 1675 | // consider the number with the country calling code stripped to be a better result and |
||
| 1676 | // keep that instead. |
||
| 1677 | 59 | if ((preg_match('/^(' . $validNumberPattern . ')$/x', $fullNumber) == 0 && |
|
| 1678 | 22 | preg_match('/^(' . $validNumberPattern . ')$/x', $potentialNationalNumber) > 0) || |
|
| 1679 | 50 | $this->testNumberLengthAgainstPattern($possibleNumberPattern, (string)$fullNumber) |
|
| 1680 | == ValidationResult::TOO_LONG |
||
| 1681 | 59 | ) { |
|
| 1682 | 12 | $nationalNumber .= $potentialNationalNumber; |
|
| 1683 | 12 | if ($keepRawInput) { |
|
| 1684 | 3 | $phoneNumber->setCountryCodeSource(CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN); |
|
| 1685 | 3 | } |
|
| 1686 | 12 | $phoneNumber->setCountryCode($defaultCountryCode); |
|
| 1687 | 12 | return $defaultCountryCode; |
|
| 1688 | } |
||
| 1689 | 49 | } |
|
| 1690 | 2697 | } |
|
| 1691 | // No country calling code present. |
||
| 1692 | 2697 | $phoneNumber->setCountryCode(0); |
|
| 1693 | 2697 | return 0; |
|
| 1694 | } |
||
| 1695 | |||
| 1696 | /** |
||
| 1697 | * Strips any international prefix (such as +, 00, 011) present in the number provided, normalizes |
||
| 1698 | * the resulting number, and indicates if an international prefix was present. |
||
| 1699 | * |
||
| 1700 | * @param string $number the non-normalized telephone number that we wish to strip any international |
||
| 1701 | * dialing prefix from. |
||
| 1702 | * @param string $possibleIddPrefix string the international direct dialing prefix from the region we |
||
| 1703 | * think this number may be dialed in |
||
| 1704 | * @return int the corresponding CountryCodeSource if an international dialing prefix could be |
||
| 1705 | * removed from the number, otherwise CountryCodeSource.FROM_DEFAULT_COUNTRY if the number did |
||
| 1706 | * not seem to be in international format. |
||
| 1707 | */ |
||
| 1708 | 2728 | public function maybeStripInternationalPrefixAndNormalize(&$number, $possibleIddPrefix) |
|
| 1709 | { |
||
| 1710 | 2728 | if (mb_strlen($number) == 0) { |
|
| 1711 | return CountryCodeSource::FROM_DEFAULT_COUNTRY; |
||
| 1712 | } |
||
| 1713 | 2728 | $matches = array(); |
|
| 1714 | // Check to see if the number begins with one or more plus signs. |
||
| 1715 | 2728 | $match = preg_match('/^' . static::$PLUS_CHARS_PATTERN . '/' . static::REGEX_FLAGS, $number, $matches, PREG_OFFSET_CAPTURE); |
|
| 1716 | 2728 | if ($match > 0) { |
|
| 1717 | 284 | $number = mb_substr($number, $matches[0][1] + mb_strlen($matches[0][0])); |
|
| 1718 | // Can now normalize the rest of the number since we've consumed the "+" sign at the start. |
||
| 1719 | 284 | $number = $this->normalize($number); |
|
| 1720 | 284 | return CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN; |
|
| 1721 | } |
||
| 1722 | // Attempt to parse the first digits as an international prefix. |
||
| 1723 | 2705 | $iddPattern = $possibleIddPrefix; |
|
| 1724 | 2705 | $number = $this->normalize($number); |
|
| 1725 | 2705 | return $this->parsePrefixAsIdd($iddPattern, $number) |
|
| 1726 | 2705 | ? CountryCodeSource::FROM_NUMBER_WITH_IDD |
|
| 1727 | 2705 | : CountryCodeSource::FROM_DEFAULT_COUNTRY; |
|
| 1728 | } |
||
| 1729 | |||
| 1730 | /** |
||
| 1731 | * Normalizes a string of characters representing a phone number. This performs |
||
| 1732 | * the following conversions: |
||
| 1733 | * Punctuation is stripped. |
||
| 1734 | * For ALPHA/VANITY numbers: |
||
| 1735 | * Letters are converted to their numeric representation on a telephone |
||
| 1736 | * keypad. The keypad used here is the one defined in ITU Recommendation |
||
| 1737 | * E.161. This is only done if there are 3 or more letters in the number, |
||
| 1738 | * to lessen the risk that such letters are typos. |
||
| 1739 | * For other numbers: |
||
| 1740 | * Wide-ascii digits are converted to normal ASCII (European) digits. |
||
| 1741 | * Arabic-Indic numerals are converted to European numerals. |
||
| 1742 | * Spurious alpha characters are stripped. |
||
| 1743 | * |
||
| 1744 | * @param string $number a string of characters representing a phone number. |
||
| 1745 | * @return string the normalized string version of the phone number. |
||
| 1746 | */ |
||
| 1747 | 2731 | public static function normalize(&$number) |
|
| 1748 | { |
||
| 1749 | 2731 | $m = new Matcher(static::VALID_ALPHA_PHONE_PATTERN, $number); |
|
| 1750 | 2731 | if ($m->matches()) { |
|
| 1751 | 6 | return static::normalizeHelper($number, static::$ALPHA_PHONE_MAPPINGS, true); |
|
| 1752 | } else { |
||
| 1753 | 2730 | return static::normalizeDigitsOnly($number); |
|
| 1754 | } |
||
| 1755 | } |
||
| 1756 | |||
| 1757 | /** |
||
| 1758 | * Normalizes a string of characters representing a phone number. This converts wide-ascii and |
||
| 1759 | * arabic-indic numerals to European numerals, and strips punctuation and alpha characters. |
||
| 1760 | * |
||
| 1761 | * @param $number string a string of characters representing a phone number |
||
| 1762 | * @return string the normalized string version of the phone number |
||
| 1763 | */ |
||
| 1764 | 2764 | public static function normalizeDigitsOnly($number) |
|
| 1765 | { |
||
| 1766 | 2764 | return static::normalizeDigits($number, false /* strip non-digits */); |
|
| 1767 | } |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * @param string $number |
||
| 1771 | * @param bool $keepNonDigits |
||
| 1772 | * @return string |
||
| 1773 | */ |
||
| 1774 | 2764 | public static function normalizeDigits($number, $keepNonDigits) |
|
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Strips the IDD from the start of the number if present. Helper function used by |
||
| 1796 | * maybeStripInternationalPrefixAndNormalize. |
||
| 1797 | * @param string $iddPattern |
||
| 1798 | * @param string $number |
||
| 1799 | * @return bool |
||
| 1800 | */ |
||
| 1801 | 2705 | protected function parsePrefixAsIdd($iddPattern, &$number) |
|
| 1802 | { |
||
| 1803 | 2705 | $m = new Matcher($iddPattern, $number); |
|
| 1804 | 2705 | if ($m->lookingAt()) { |
|
| 1805 | 12 | $matchEnd = $m->end(); |
|
| 1806 | // Only strip this if the first digit after the match is not a 0, since country calling codes |
||
| 1807 | // cannot begin with 0. |
||
| 1808 | 12 | $digitMatcher = new Matcher(static::$CAPTURING_DIGIT_PATTERN, substr($number, $matchEnd)); |
|
| 1809 | 12 | if ($digitMatcher->find()) { |
|
| 1810 | 12 | $normalizedGroup = $this->normalizeDigitsOnly($digitMatcher->group(1)); |
|
| 1811 | 12 | if ($normalizedGroup == "0") { |
|
| 1812 | 4 | return false; |
|
| 1813 | } |
||
| 1814 | 10 | } |
|
| 1815 | 10 | $number = substr($number, $matchEnd); |
|
| 1816 | 10 | return true; |
|
| 1817 | } |
||
| 1818 | 2702 | return false; |
|
| 1819 | } |
||
| 1820 | |||
| 1821 | /** |
||
| 1822 | * Extracts country calling code from fullNumber, returns it and places the remaining number in nationalNumber. |
||
| 1823 | * It assumes that the leading plus sign or IDD has already been removed. |
||
| 1824 | * Returns 0 if fullNumber doesn't start with a valid country calling code, and leaves nationalNumber unmodified. |
||
| 1825 | * @param string $fullNumber |
||
| 1826 | * @param string $nationalNumber |
||
| 1827 | * @return int |
||
| 1828 | */ |
||
| 1829 | 285 | protected function extractCountryCode(&$fullNumber, &$nationalNumber) |
|
| 1830 | { |
||
| 1831 | 285 | if ((mb_strlen($fullNumber) == 0) || ($fullNumber[0] == '0')) { |
|
| 1832 | // Country codes do not begin with a '0'. |
||
| 1833 | 2 | return 0; |
|
| 1834 | } |
||
| 1835 | 285 | $numberLength = mb_strlen($fullNumber); |
|
| 1836 | 285 | for ($i = 1; $i <= static::MAX_LENGTH_COUNTRY_CODE && $i <= $numberLength; $i++) { |
|
| 1837 | 285 | $potentialCountryCode = (int)substr($fullNumber, 0, $i); |
|
| 1838 | 285 | if (isset($this->countryCallingCodeToRegionCodeMap[$potentialCountryCode])) { |
|
| 1839 | 285 | $nationalNumber .= substr($fullNumber, $i); |
|
| 1840 | 285 | return $potentialCountryCode; |
|
| 1841 | } |
||
| 1842 | 256 | } |
|
| 1843 | 2 | return 0; |
|
| 1844 | } |
||
| 1845 | |||
| 1846 | /** |
||
| 1847 | * Strips any national prefix (such as 0, 1) present in the number provided. |
||
| 1848 | * |
||
| 1849 | * @param string $number the normalized telephone number that we wish to strip any national |
||
| 1850 | * dialing prefix from |
||
| 1851 | * @param PhoneMetadata $metadata the metadata for the region that we think this number is from |
||
| 1852 | * @param string $carrierCode a place to insert the carrier code if one is extracted |
||
| 1853 | * @return bool true if a national prefix or carrier code (or both) could be extracted. |
||
| 1854 | */ |
||
| 1855 | 2727 | public function maybeStripNationalPrefixAndCarrierCode(&$number, PhoneMetadata $metadata, &$carrierCode) |
|
| 1856 | { |
||
| 1857 | 2727 | $numberLength = mb_strlen($number); |
|
| 1858 | 2727 | $possibleNationalPrefix = $metadata->getNationalPrefixForParsing(); |
|
| 1859 | 2727 | if ($numberLength == 0 || $possibleNationalPrefix === null || mb_strlen($possibleNationalPrefix) == 0) { |
|
| 1860 | // Early return for numbers of zero length. |
||
| 1861 | 974 | return false; |
|
| 1862 | } |
||
| 1863 | |||
| 1864 | // Attempt to parse the first digits as a national prefix. |
||
| 1865 | 1762 | $prefixMatcher = new Matcher($possibleNationalPrefix, $number); |
|
| 1866 | 1762 | if ($prefixMatcher->lookingAt()) { |
|
| 1867 | 70 | $nationalNumberRule = $metadata->getGeneralDesc()->getNationalNumberPattern(); |
|
| 1868 | // Check if the original number is viable. |
||
| 1869 | 70 | $nationalNumberRuleMatcher = new Matcher($nationalNumberRule, $number); |
|
| 1870 | 70 | $isViableOriginalNumber = $nationalNumberRuleMatcher->matches(); |
|
| 1871 | // $prefixMatcher->group($numOfGroups) === null implies nothing was captured by the capturing |
||
| 1872 | // groups in $possibleNationalPrefix; therefore, no transformation is necessary, and we just |
||
| 1873 | // remove the national prefix |
||
| 1874 | 70 | $numOfGroups = $prefixMatcher->groupCount(); |
|
| 1875 | 70 | $transformRule = $metadata->getNationalPrefixTransformRule(); |
|
| 1876 | if ($transformRule === null |
||
| 1877 | 70 | || mb_strlen($transformRule) == 0 |
|
| 1878 | 24 | || $prefixMatcher->group($numOfGroups - 1) === null |
|
| 1879 | 70 | ) { |
|
| 1880 | // If the original number was viable, and the resultant number is not, we return. |
||
| 1881 | 65 | $matcher = new Matcher($nationalNumberRule, substr($number, $prefixMatcher->end())); |
|
| 1882 | 65 | if ($isViableOriginalNumber && !$matcher->matches()) { |
|
| 1883 | 14 | return false; |
|
| 1884 | } |
||
| 1885 | 54 | if ($carrierCode !== null && $numOfGroups > 0 && $prefixMatcher->group($numOfGroups) !== null) { |
|
| 1886 | 2 | $carrierCode .= $prefixMatcher->group(1); |
|
| 1887 | 2 | } |
|
| 1888 | |||
| 1889 | 54 | $number = substr($number, $prefixMatcher->end()); |
|
| 1890 | 54 | return true; |
|
| 1891 | } else { |
||
| 1892 | // Check that the resultant number is still viable. If not, return. Check this by copying |
||
| 1893 | // the string and making the transformation on the copy first. |
||
| 1894 | 9 | $transformedNumber = $number; |
|
| 1895 | 9 | $transformedNumber = substr_replace( |
|
| 1896 | 9 | $transformedNumber, |
|
| 1897 | 9 | $prefixMatcher->replaceFirst($transformRule), |
|
| 1898 | 9 | 0, |
|
| 1899 | $numberLength |
||
| 1900 | 9 | ); |
|
| 1901 | 9 | $matcher = new Matcher($nationalNumberRule, $transformedNumber); |
|
| 1902 | 9 | if ($isViableOriginalNumber && !$matcher->matches()) { |
|
| 1903 | return false; |
||
| 1904 | } |
||
| 1905 | 9 | if ($carrierCode !== null && $numOfGroups > 1) { |
|
| 1906 | $carrierCode .= $prefixMatcher->group(1); |
||
| 1907 | } |
||
| 1908 | 9 | $number = substr_replace($number, $transformedNumber, 0, mb_strlen($number)); |
|
| 1909 | 9 | return true; |
|
| 1910 | } |
||
| 1911 | } |
||
| 1912 | 1708 | return false; |
|
| 1913 | } |
||
| 1914 | |||
| 1915 | /** |
||
| 1916 | * Helper method to check a number against a particular pattern and determine whether it matches, |
||
| 1917 | * or is too short or too long. Currently, if a number pattern suggests that numbers of length 7 |
||
| 1918 | * and 10 are possible, and a number in between these possible lengths is entered, such as of |
||
| 1919 | * length 8, this will return TOO_LONG. |
||
| 1920 | * @param string $numberPattern |
||
| 1921 | * @param string $number |
||
| 1922 | * @return int ValidationResult |
||
| 1923 | */ |
||
| 1924 | 2729 | protected function testNumberLengthAgainstPattern($numberPattern, $number) |
|
| 1925 | { |
||
| 1926 | 2729 | $numberMatcher = new Matcher($numberPattern, $number); |
|
| 1927 | 2729 | if ($numberMatcher->matches()) { |
|
| 1928 | 2042 | return ValidationResult::IS_POSSIBLE; |
|
| 1929 | } |
||
| 1930 | 700 | if ($numberMatcher->lookingAt()) { |
|
| 1931 | 46 | return ValidationResult::TOO_LONG; |
|
| 1932 | } else { |
||
| 1933 | 661 | return ValidationResult::TOO_SHORT; |
|
| 1934 | } |
||
| 1935 | } |
||
| 1936 | |||
| 1937 | /** |
||
| 1938 | * Returns a list with the region codes that match the specific country calling code. For |
||
| 1939 | * non-geographical country calling codes, the region code 001 is returned. Also, in the case |
||
| 1940 | * of no region code being found, an empty list is returned. |
||
| 1941 | * @param int $countryCallingCode |
||
| 1942 | * @return array|null |
||
| 1943 | */ |
||
| 1944 | 9 | public function getRegionCodesForCountryCode($countryCallingCode) |
|
| 1945 | { |
||
| 1946 | 9 | $regionCodes = isset($this->countryCallingCodeToRegionCodeMap[$countryCallingCode]) ? $this->countryCallingCodeToRegionCodeMap[$countryCallingCode] : null; |
|
| 1947 | 9 | return $regionCodes === null ? array() : $regionCodes; |
|
| 1948 | } |
||
| 1949 | |||
| 1950 | /** |
||
| 1951 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 1952 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 1953 | * |
||
| 1954 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 1955 | * @return int the country calling code for the region denoted by regionCode |
||
| 1956 | */ |
||
| 1957 | 2 | public function getCountryCodeForRegion($regionCode) |
|
| 1958 | { |
||
| 1959 | 2 | if (!$this->isValidRegionCode($regionCode)) { |
|
| 1960 | 1 | return 0; |
|
| 1961 | } |
||
| 1962 | 2 | return $this->getCountryCodeForValidRegion($regionCode); |
|
| 1963 | } |
||
| 1964 | |||
| 1965 | /** |
||
| 1966 | * Returns the country calling code for a specific region. For example, this would be 1 for the |
||
| 1967 | * United States, and 64 for New Zealand. Assumes the region is already valid. |
||
| 1968 | * |
||
| 1969 | * @param string $regionCode the region that we want to get the country calling code for |
||
| 1970 | * @return int the country calling code for the region denoted by regionCode |
||
| 1971 | * @throws \InvalidArgumentException if the region is invalid |
||
| 1972 | */ |
||
| 1973 | 1816 | protected function getCountryCodeForValidRegion($regionCode) |
|
| 1981 | |||
| 1982 | /** |
||
| 1983 | * Returns a number formatted in such a way that it can be dialed from a mobile phone in a |
||
| 1984 | * specific region. If the number cannot be reached from the region (e.g. some countries block |
||
| 1985 | * toll-free numbers from being called outside of the country), the method returns an empty |
||
| 1986 | * string. |
||
| 1987 | * |
||
| 1988 | * @param PhoneNumber $number the phone number to be formatted |
||
| 1989 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 1990 | * @param boolean $withFormatting whether the number should be returned with formatting symbols, such as |
||
| 1991 | * spaces and dashes. |
||
| 1992 | * @return string the formatted phone number |
||
| 1993 | */ |
||
| 1994 | 1 | public function formatNumberForMobileDialing(PhoneNumber $number, $regionCallingFrom, $withFormatting) |
|
| 1995 | { |
||
| 1996 | 1 | $countryCallingCode = $number->getCountryCode(); |
|
| 1997 | 1 | if (!$this->hasValidCountryCallingCode($countryCallingCode)) { |
|
| 1998 | return $number->hasRawInput() ? $number->getRawInput() : ""; |
||
| 1999 | } |
||
| 2000 | |||
| 2001 | 1 | $formattedNumber = ""; |
|
| 2002 | // Clear the extension, as that part cannot normally be dialed together with the main number. |
||
| 2003 | 1 | $numberNoExt = new PhoneNumber(); |
|
| 2004 | 1 | $numberNoExt->mergeFrom($number)->clearExtension(); |
|
| 2005 | 1 | $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode); |
|
| 2006 | 1 | $numberType = $this->getNumberType($numberNoExt); |
|
| 2007 | 1 | $isValidNumber = ($numberType !== PhoneNumberType::UNKNOWN); |
|
| 2008 | 1 | if ($regionCallingFrom == $regionCode) { |
|
| 2009 | 1 | $isFixedLineOrMobile = ($numberType == PhoneNumberType::FIXED_LINE) || ($numberType == PhoneNumberType::MOBILE) || ($numberType == PhoneNumberType::FIXED_LINE_OR_MOBILE); |
|
| 2010 | // Carrier codes may be needed in some countries. We handle this here. |
||
| 2011 | 1 | if ($regionCode == "CO" && $numberType == PhoneNumberType::FIXED_LINE) { |
|
| 2012 | $formattedNumber = $this->formatNationalNumberWithCarrierCode( |
||
| 2013 | $numberNoExt, |
||
| 2014 | static::COLOMBIA_MOBILE_TO_FIXED_LINE_PREFIX |
||
| 2015 | ); |
||
| 2016 | 1 | } elseif ($regionCode == "BR" && $isFixedLineOrMobile) { |
|
| 2017 | // Brazilian fixed line and mobile numbers need to be dialed with a carrier code when |
||
| 2018 | // called within Brazil. Without that, most of the carriers won't connect the call. |
||
| 2019 | // Because of that, we return an empty string here. |
||
| 2020 | $formattedNumber = $numberNoExt->hasPreferredDomesticCarrierCode( |
||
| 2021 | ) ? $this->formatNationalNumberWithCarrierCode($numberNoExt, "") : ""; |
||
| 2022 | 1 | } elseif ($isValidNumber && $regionCode == "HU") { |
|
| 2023 | // The national format for HU numbers doesn't contain the national prefix, because that is |
||
| 2024 | // how numbers are normally written down. However, the national prefix is obligatory when |
||
| 2025 | // dialing from a mobile phone, except for short numbers. As a result, we add it back here |
||
| 2026 | // if it is a valid regular length phone number. |
||
| 2027 | 1 | $formattedNumber = $this->getNddPrefixForRegion( |
|
| 2028 | 1 | $regionCode, |
|
| 2029 | true /* strip non-digits */ |
||
| 2030 | 1 | ) . " " . $this->format($numberNoExt, PhoneNumberFormat::NATIONAL); |
|
| 2031 | 1 | } elseif ($countryCallingCode === static::NANPA_COUNTRY_CODE) { |
|
| 2032 | // For NANPA countries, we output international format for numbers that can be dialed |
||
| 2033 | // internationally, since that always works, except for numbers which might potentially be |
||
| 2034 | // short numbers, which are always dialled in national format. |
||
| 2035 | 1 | $regionMetadata = $this->getMetadataForRegion($regionCallingFrom); |
|
| 2036 | 1 | if ($this->canBeInternationallyDialled($numberNoExt) && |
|
| 2037 | 1 | !$this->isShorterThanPossibleNormalNumber( |
|
| 2038 | 1 | $regionMetadata, |
|
| 2039 | 1 | $this->getNationalSignificantNumber($numberNoExt) |
|
| 2040 | 1 | ) |
|
| 2041 | 1 | ) { |
|
| 2042 | 1 | $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::INTERNATIONAL); |
|
| 2043 | 1 | } else { |
|
| 2044 | 1 | $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::NATIONAL); |
|
| 2045 | } |
||
| 2046 | 1 | } else { |
|
| 2047 | // For non-geographical countries, Mexican and Chilean fixed line and mobile numbers, we |
||
| 2048 | // output international format for numbers that can be dialed internationally as that always |
||
| 2049 | // works. |
||
| 2050 | 1 | if (($regionCode == static::REGION_CODE_FOR_NON_GEO_ENTITY || |
|
| 2051 | // MX fixed line and mobile numbers should always be formatted in international format, |
||
| 2052 | // even when dialed within MX. For national format to work, a carrier code needs to be |
||
| 2053 | // used, and the correct carrier code depends on if the caller and callee are from the |
||
| 2054 | // same local area. It is trickier to get that to work correctly than using |
||
| 2055 | // international format, which is tested to work fine on all carriers. |
||
| 2056 | // CL fixed line numbers need the national prefix when dialing in the national format, |
||
| 2057 | // but don't have it when used for display. The reverse is true for mobile numbers. |
||
| 2058 | // As a result, we output them in the international format to make it work. |
||
| 2059 | 1 | (($regionCode == "MX" || $regionCode == "CL") && $isFixedLineOrMobile)) && $this->canBeInternationallyDialled( |
|
| 2060 | $numberNoExt |
||
| 2061 | 1 | ) |
|
| 2062 | 1 | ) { |
|
| 2063 | 1 | $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::INTERNATIONAL); |
|
| 2064 | 1 | } else { |
|
| 2065 | 1 | $formattedNumber = $this->format($numberNoExt, PhoneNumberFormat::NATIONAL); |
|
| 2066 | } |
||
| 2067 | } |
||
| 2068 | 1 | } elseif ($isValidNumber && $this->canBeInternationallyDialled($numberNoExt)) { |
|
| 2069 | // We assume that short numbers are not diallable from outside their region, so if a number |
||
| 2070 | // is not a valid regular length phone number, we treat it as if it cannot be internationally |
||
| 2071 | // dialled. |
||
| 2072 | return $withFormatting ? |
||
| 2073 | 1 | $this->format($numberNoExt, PhoneNumberFormat::INTERNATIONAL) : |
|
| 2074 | 1 | $this->format($numberNoExt, PhoneNumberFormat::E164); |
|
| 2075 | } |
||
| 2076 | 1 | return $withFormatting ? $formattedNumber : $this->normalizeDiallableCharsOnly($formattedNumber); |
|
| 2077 | } |
||
| 2078 | |||
| 2079 | /** |
||
| 2080 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2081 | * {@code carrierCode}. The {@code carrierCode} will always be used regardless of whether the |
||
| 2082 | * phone number already has a preferred domestic carrier code stored. If {@code carrierCode} |
||
| 2083 | * contains an empty string, returns the number in national format without any carrier code. |
||
| 2084 | * |
||
| 2085 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2086 | * @param string $carrierCode the carrier selection code to be used |
||
| 2087 | * @return string the formatted phone number in national format for dialing using the carrier as |
||
| 2088 | * specified in the {@code carrierCode} |
||
| 2089 | */ |
||
| 2090 | 2 | public function formatNationalNumberWithCarrierCode(PhoneNumber $number, $carrierCode) |
|
| 2119 | |||
| 2120 | /** |
||
| 2121 | * Formats a phone number in national format for dialing using the carrier as specified in the |
||
| 2122 | * preferredDomesticCarrierCode field of the PhoneNumber object passed in. If that is missing, |
||
| 2123 | * use the {@code fallbackCarrierCode} passed in instead. If there is no |
||
| 2124 | * {@code preferredDomesticCarrierCode}, and the {@code fallbackCarrierCode} contains an empty |
||
| 2125 | * string, return the number in national format without any carrier code. |
||
| 2126 | * |
||
| 2127 | * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier code passed in |
||
| 2128 | * should take precedence over the number's {@code preferredDomesticCarrierCode} when formatting. |
||
| 2129 | * |
||
| 2130 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2131 | * @param string $fallbackCarrierCode the carrier selection code to be used, if none is found in the |
||
| 2132 | * phone number itself |
||
| 2133 | * @return string the formatted phone number in national format for dialing using the number's |
||
| 2134 | * {@code preferredDomesticCarrierCode}, or the {@code fallbackCarrierCode} passed in if |
||
| 2135 | * none is found |
||
| 2136 | */ |
||
| 2137 | 1 | public function formatNationalNumberWithPreferredCarrierCode(PhoneNumber $number, $fallbackCarrierCode) |
|
| 2146 | |||
| 2147 | /** |
||
| 2148 | * Returns true if the number can be dialled from outside the region, or unknown. If the number |
||
| 2149 | * can only be dialled from within the region, returns false. Does not check the number is a valid |
||
| 2150 | * number. |
||
| 2151 | * TODO: Make this method public when we have enough metadata to make it worthwhile. |
||
| 2152 | * |
||
| 2153 | * @param PhoneNumber $number the phone-number for which we want to know whether it is diallable from outside the region |
||
| 2154 | * @return bool |
||
| 2155 | */ |
||
| 2156 | 31 | public function canBeInternationallyDialled(PhoneNumber $number) |
|
| 2157 | { |
||
| 2158 | 31 | $metadata = $this->getMetadataForRegion($this->getRegionCodeForNumber($number)); |
|
| 2159 | 31 | if ($metadata === null) { |
|
| 2160 | // Note numbers belonging to non-geographical entities (e.g. +800 numbers) are always |
||
| 2161 | // internationally diallable, and will be caught here. |
||
| 2162 | 2 | return true; |
|
| 2163 | } |
||
| 2164 | 31 | $nationalSignificantNumber = $this->getNationalSignificantNumber($number); |
|
| 2165 | 31 | return !$this->isNumberMatchingDesc($nationalSignificantNumber, $metadata->getNoInternationalDialling()); |
|
| 2166 | } |
||
| 2167 | |||
| 2168 | /** |
||
| 2169 | * Normalizes a string of characters representing a phone number. This strips all characters which |
||
| 2170 | * are not diallable on a mobile phone keypad (including all non-ASCII digits). |
||
| 2171 | * |
||
| 2172 | * @param string $number a string of characters representing a phone number |
||
| 2173 | * @return string the normalized string version of the phone number |
||
| 2174 | */ |
||
| 2175 | 3 | public static function normalizeDiallableCharsOnly($number) |
|
| 2176 | { |
||
| 2177 | 3 | return static::normalizeHelper($number, static::$DIALLABLE_CHAR_MAPPINGS, true /* remove non matches */); |
|
| 2178 | } |
||
| 2179 | |||
| 2180 | /** |
||
| 2181 | * Formats a phone number for out-of-country dialing purposes. |
||
| 2182 | * |
||
| 2183 | * Note that in this version, if the number was entered originally using alpha characters and |
||
| 2184 | * this version of the number is stored in raw_input, this representation of the number will be |
||
| 2185 | * used rather than the digit representation. Grouping information, as specified by characters |
||
| 2186 | * such as "-" and " ", will be retained. |
||
| 2187 | * |
||
| 2188 | * <p><b>Caveats:</b></p> |
||
| 2189 | * <ul> |
||
| 2190 | * <li> This will not produce good results if the country calling code is both present in the raw |
||
| 2191 | * input _and_ is the start of the national number. This is not a problem in the regions |
||
| 2192 | * which typically use alpha numbers. |
||
| 2193 | * <li> This will also not produce good results if the raw input has any grouping information |
||
| 2194 | * within the first three digits of the national number, and if the function needs to strip |
||
| 2195 | * preceding digits/words in the raw input before these digits. Normally people group the |
||
| 2196 | * first three digits together so this is not a huge problem - and will be fixed if it |
||
| 2197 | * proves to be so. |
||
| 2198 | * </ul> |
||
| 2199 | * |
||
| 2200 | * @param PhoneNumber $number the phone number that needs to be formatted |
||
| 2201 | * @param String $regionCallingFrom the region where the call is being placed |
||
| 2202 | * @return String the formatted phone number |
||
| 2203 | */ |
||
| 2204 | 1 | public function formatOutOfCountryKeepingAlphaChars(PhoneNumber $number, $regionCallingFrom) |
|
| 2297 | |||
| 2298 | /** |
||
| 2299 | * Formats a phone number for out-of-country dialing purposes. If no regionCallingFrom is |
||
| 2300 | * supplied, we format the number in its INTERNATIONAL format. If the country calling code is the |
||
| 2301 | * same as that of the region where the number is from, then NATIONAL formatting will be applied. |
||
| 2302 | * |
||
| 2303 | * <p>If the number itself has a country calling code of zero or an otherwise invalid country |
||
| 2304 | * calling code, then we return the number with no formatting applied. |
||
| 2305 | * |
||
| 2306 | * <p>Note this function takes care of the case for calling inside of NANPA and between Russia and |
||
| 2307 | * Kazakhstan (who share the same country calling code). In those cases, no international prefix |
||
| 2308 | * is used. For regions which have multiple international prefixes, the number in its |
||
| 2309 | * INTERNATIONAL format will be returned instead. |
||
| 2310 | * |
||
| 2311 | * @param PhoneNumber $number the phone number to be formatted |
||
| 2312 | * @param string $regionCallingFrom the region where the call is being placed |
||
| 2313 | * @return string the formatted phone number |
||
| 2314 | */ |
||
| 2315 | 8 | public function formatOutOfCountryCallingNumber(PhoneNumber $number, $regionCallingFrom) |
|
| 2316 | { |
||
| 2317 | 8 | if (!$this->isValidRegionCode($regionCallingFrom)) { |
|
| 2318 | 1 | return $this->format($number, PhoneNumberFormat::INTERNATIONAL); |
|
| 2319 | } |
||
| 2320 | 7 | $countryCallingCode = $number->getCountryCode(); |
|
| 2321 | 7 | $nationalSignificantNumber = $this->getNationalSignificantNumber($number); |
|
| 2322 | 7 | if (!$this->hasValidCountryCallingCode($countryCallingCode)) { |
|
| 2323 | return $nationalSignificantNumber; |
||
| 2324 | } |
||
| 2325 | 7 | if ($countryCallingCode == static::NANPA_COUNTRY_CODE) { |
|
| 2326 | 4 | if ($this->isNANPACountry($regionCallingFrom)) { |
|
| 2327 | // For NANPA regions, return the national format for these regions but prefix it with the |
||
| 2328 | // country calling code. |
||
| 2329 | 1 | return $countryCallingCode . " " . $this->format($number, PhoneNumberFormat::NATIONAL); |
|
| 2330 | } |
||
| 2331 | 7 | } else if ($countryCallingCode == $this->getCountryCodeForValidRegion($regionCallingFrom)) { |
|
| 2332 | // If regions share a country calling code, the country calling code need not be dialled. |
||
| 2333 | // This also applies when dialling within a region, so this if clause covers both these cases. |
||
| 2334 | // Technically this is the case for dialling from La Reunion to other overseas departments of |
||
| 2335 | // France (French Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover this |
||
| 2336 | // edge case for now and for those cases return the version including country calling code. |
||
| 2337 | // Details here: http://www.petitfute.com/voyage/225-info-pratiques-reunion |
||
| 2338 | 2 | return $this->format($number, PhoneNumberFormat::NATIONAL); |
|
| 2339 | } |
||
| 2340 | // Metadata cannot be null because we checked 'isValidRegionCode()' above. |
||
| 2341 | 7 | $metadataForRegionCallingFrom = $this->getMetadataForRegion($regionCallingFrom); |
|
| 2342 | |||
| 2343 | 7 | $internationalPrefix = $metadataForRegionCallingFrom->getInternationalPrefix(); |
|
| 2344 | |||
| 2345 | // For regions that have multiple international prefixes, the international format of the |
||
| 2346 | // number is returned, unless there is a preferred international prefix. |
||
| 2347 | 7 | $internationalPrefixForFormatting = ""; |
|
| 2348 | 7 | $uniqueInternationalPrefixMatcher = new Matcher(static::UNIQUE_INTERNATIONAL_PREFIX, $internationalPrefix); |
|
| 2349 | |||
| 2350 | 7 | if ($uniqueInternationalPrefixMatcher->matches()) { |
|
| 2351 | 6 | $internationalPrefixForFormatting = $internationalPrefix; |
|
| 2352 | 7 | } else if ($metadataForRegionCallingFrom->hasPreferredInternationalPrefix()) { |
|
| 2353 | 3 | $internationalPrefixForFormatting = $metadataForRegionCallingFrom->getPreferredInternationalPrefix(); |
|
| 2354 | 3 | } |
|
| 2355 | |||
| 2356 | 7 | $regionCode = $this->getRegionCodeForCountryCode($countryCallingCode); |
|
| 2357 | // Metadata cannot be null because the country calling code is valid. |
||
| 2358 | 7 | $metadataForRegion = $this->getMetadataForRegionOrCallingCode($countryCallingCode, $regionCode); |
|
| 2359 | 7 | $formattedNationalNumber = $this->formatNsn( |
|
| 2360 | 7 | $nationalSignificantNumber, |
|
| 2361 | 7 | $metadataForRegion, |
|
| 2362 | PhoneNumberFormat::INTERNATIONAL |
||
| 2363 | 7 | ); |
|
| 2364 | 7 | $formattedNumber = $formattedNationalNumber; |
|
| 2365 | 7 | $this->maybeAppendFormattedExtension( |
|
| 2366 | 7 | $number, |
|
| 2367 | 7 | $metadataForRegion, |
|
| 2368 | 7 | PhoneNumberFormat::INTERNATIONAL, |
|
| 2369 | $formattedNumber |
||
| 2370 | 7 | ); |
|
| 2371 | 7 | if (mb_strlen($internationalPrefixForFormatting) > 0) { |
|
| 2372 | 7 | $formattedNumber = $internationalPrefixForFormatting . " " . $countryCallingCode . " " . $formattedNumber; |
|
| 2373 | 7 | } else { |
|
| 2374 | 1 | $this->prefixNumberWithCountryCallingCode( |
|
| 2375 | 1 | $countryCallingCode, |
|
| 2376 | 1 | PhoneNumberFormat::INTERNATIONAL, |
|
| 2377 | $formattedNumber |
||
| 2378 | 1 | ); |
|
| 2379 | } |
||
| 2380 | 7 | return $formattedNumber; |
|
| 2381 | } |
||
| 2382 | |||
| 2383 | /** |
||
| 2384 | * Checks if this is a region under the North American Numbering Plan Administration (NANPA). |
||
| 2385 | * @param string $regionCode |
||
| 2386 | * @return boolean true if regionCode is one of the regions under NANPA |
||
| 2387 | */ |
||
| 2388 | 5 | public function isNANPACountry($regionCode) |
|
| 2389 | { |
||
| 2390 | 5 | return in_array($regionCode, $this->nanpaRegions); |
|
| 2391 | } |
||
| 2392 | |||
| 2393 | /** |
||
| 2394 | * Formats a phone number using the original phone number format that the number is parsed from. |
||
| 2395 | * The original format is embedded in the country_code_source field of the PhoneNumber object |
||
| 2396 | * passed in. If such information is missing, the number will be formatted into the NATIONAL |
||
| 2397 | * format by default. When the number contains a leading zero and this is unexpected for this |
||
| 2398 | * country, or we don't have a formatting pattern for the number, the method returns the raw input |
||
| 2399 | * when it is available. |
||
| 2400 | * |
||
| 2401 | * Note this method guarantees no digit will be inserted, removed or modified as a result of |
||
| 2402 | * formatting. |
||
| 2403 | * |
||
| 2404 | * @param PhoneNumber $number the phone number that needs to be formatted in its original number format |
||
| 2405 | * @param string $regionCallingFrom the region whose IDD needs to be prefixed if the original number |
||
| 2406 | * has one |
||
| 2407 | * @return string the formatted phone number in its original number format |
||
| 2408 | */ |
||
| 2409 | 1 | public function formatInOriginalFormat(PhoneNumber $number, $regionCallingFrom) |
|
| 2410 | { |
||
| 2411 | 1 | if ($number->hasRawInput() && |
|
| 2412 | 1 | ($this->hasUnexpectedItalianLeadingZero($number) || !$this->hasFormattingPatternForNumber($number)) |
|
| 2413 | 1 | ) { |
|
| 2414 | // We check if we have the formatting pattern because without that, we might format the number |
||
| 2415 | // as a group without national prefix. |
||
| 2416 | 1 | return $number->getRawInput(); |
|
| 2417 | } |
||
| 2418 | 1 | if (!$number->hasCountryCodeSource()) { |
|
| 2419 | 1 | return $this->format($number, PhoneNumberFormat::NATIONAL); |
|
| 2420 | } |
||
| 2421 | 1 | switch ($number->getCountryCodeSource()) { |
|
| 2422 | 1 | case CountryCodeSource::FROM_NUMBER_WITH_PLUS_SIGN: |
|
| 2423 | 1 | $formattedNumber = $this->format($number, PhoneNumberFormat::INTERNATIONAL); |
|
| 2424 | 1 | break; |
|
| 2425 | 1 | case CountryCodeSource::FROM_NUMBER_WITH_IDD: |
|
| 2426 | 1 | $formattedNumber = $this->formatOutOfCountryCallingNumber($number, $regionCallingFrom); |
|
| 2427 | 1 | break; |
|
| 2428 | 1 | case CountryCodeSource::FROM_NUMBER_WITHOUT_PLUS_SIGN: |
|
| 2429 | 1 | $formattedNumber = substr($this->format($number, PhoneNumberFormat::INTERNATIONAL), 1); |
|
| 2430 | 1 | break; |
|
| 2431 | 1 | case CountryCodeSource::FROM_DEFAULT_COUNTRY: |
|
| 2432 | // Fall-through to default case. |
||
| 2433 | 1 | default: |
|
| 2434 | |||
| 2435 | 1 | $regionCode = $this->getRegionCodeForCountryCode($number->getCountryCode()); |
|
| 2436 | // We strip non-digits from the NDD here, and from the raw input later, so that we can |
||
| 2437 | // compare them easily. |
||
| 2438 | 1 | $nationalPrefix = $this->getNddPrefixForRegion($regionCode, true /* strip non-digits */); |
|
| 2439 | 1 | $nationalFormat = $this->format($number, PhoneNumberFormat::NATIONAL); |
|
| 2440 | 1 | if ($nationalPrefix === null || mb_strlen($nationalPrefix) == 0) { |
|
| 2441 | // If the region doesn't have a national prefix at all, we can safely return the national |
||
| 2442 | // format without worrying about a national prefix being added. |
||
| 2443 | 1 | $formattedNumber = $nationalFormat; |
|
| 2444 | 1 | break; |
|
| 2445 | } |
||
| 2446 | // Otherwise, we check if the original number was entered with a national prefix. |
||
| 2447 | 1 | if ($this->rawInputContainsNationalPrefix( |
|
| 2448 | 1 | $number->getRawInput(), |
|
| 2449 | 1 | $nationalPrefix, |
|
| 2450 | $regionCode |
||
| 2451 | 1 | ) |
|
| 2452 | 1 | ) { |
|
| 2453 | // If so, we can safely return the national format. |
||
| 2454 | 1 | $formattedNumber = $nationalFormat; |
|
| 2455 | 1 | break; |
|
| 2456 | } |
||
| 2457 | // Metadata cannot be null here because getNddPrefixForRegion() (above) returns null if |
||
| 2458 | // there is no metadata for the region. |
||
| 2459 | 1 | $metadata = $this->getMetadataForRegion($regionCode); |
|
| 2460 | 1 | $nationalNumber = $this->getNationalSignificantNumber($number); |
|
| 2461 | 1 | $formatRule = $this->chooseFormattingPatternForNumber($metadata->numberFormats(), $nationalNumber); |
|
| 2462 | // The format rule could still be null here if the national number was 0 and there was no |
||
| 2463 | // raw input (this should not be possible for numbers generated by the phonenumber library |
||
| 2464 | // as they would also not have a country calling code and we would have exited earlier). |
||
| 2465 | 1 | if ($formatRule === null) { |
|
| 2466 | $formattedNumber = $nationalFormat; |
||
| 2467 | break; |
||
| 2468 | } |
||
| 2469 | // When the format we apply to this number doesn't contain national prefix, we can just |
||
| 2470 | // return the national format. |
||
| 2471 | // TODO: Refactor the code below with the code in isNationalPrefixPresentIfRequired. |
||
| 2472 | 1 | $candidateNationalPrefixRule = $formatRule->getNationalPrefixFormattingRule(); |
|
| 2473 | // We assume that the first-group symbol will never be _before_ the national prefix. |
||
| 2474 | 1 | $indexOfFirstGroup = strpos($candidateNationalPrefixRule, '$1'); |
|
| 2475 | 1 | if ($indexOfFirstGroup <= 0) { |
|
| 2476 | 1 | $formattedNumber = $nationalFormat; |
|
| 2477 | 1 | break; |
|
| 2478 | } |
||
| 2479 | 1 | $candidateNationalPrefixRule = substr($candidateNationalPrefixRule, 0, $indexOfFirstGroup); |
|
| 2480 | 1 | $candidateNationalPrefixRule = $this->normalizeDigitsOnly($candidateNationalPrefixRule); |
|
| 2481 | 1 | if (mb_strlen($candidateNationalPrefixRule) == 0) { |
|
| 2482 | // National prefix not used when formatting this number. |
||
| 2483 | $formattedNumber = $nationalFormat; |
||
| 2484 | break; |
||
| 2485 | } |
||
| 2486 | // Otherwise, we need to remove the national prefix from our output. |
||
| 2487 | 1 | $numFormatCopy = new NumberFormat(); |
|
| 2488 | 1 | $numFormatCopy->mergeFrom($formatRule); |
|
| 2489 | 1 | $numFormatCopy->clearNationalPrefixFormattingRule(); |
|
| 2490 | 1 | $numberFormats = array(); |
|
| 2491 | 1 | $numberFormats[] = $numFormatCopy; |
|
| 2492 | 1 | $formattedNumber = $this->formatByPattern($number, PhoneNumberFormat::NATIONAL, $numberFormats); |
|
| 2493 | 1 | break; |
|
| 2494 | 1 | } |
|
| 2495 | 1 | $rawInput = $number->getRawInput(); |
|
| 2496 | // If no digit is inserted/removed/modified as a result of our formatting, we return the |
||
| 2497 | // formatted phone number; otherwise we return the raw input the user entered. |
||
| 2498 | 1 | if ($formattedNumber !== null && mb_strlen($rawInput) > 0) { |
|
| 2499 | 1 | $normalizedFormattedNumber = $this->normalizeDiallableCharsOnly($formattedNumber); |
|
| 2500 | 1 | $normalizedRawInput = $this->normalizeDiallableCharsOnly($rawInput); |
|
| 2501 | 1 | if ($normalizedFormattedNumber != $normalizedRawInput) { |
|
| 2502 | 1 | $formattedNumber = $rawInput; |
|
| 2503 | 1 | } |
|
| 2504 | 1 | } |
|
| 2505 | 1 | return $formattedNumber; |
|
| 2506 | } |
||
| 2507 | |||
| 2508 | /** |
||
| 2509 | * Returns true if a number is from a region whose national significant number couldn't contain a |
||
| 2510 | * leading zero, but has the italian_leading_zero field set to true. |
||
| 2511 | * @param PhoneNumber $number |
||
| 2512 | * @return bool |
||
| 2513 | */ |
||
| 2514 | 1 | protected function hasUnexpectedItalianLeadingZero(PhoneNumber $number) |
|
| 2515 | { |
||
| 2516 | 1 | return $number->isItalianLeadingZero() && !$this->isLeadingZeroPossible($number->getCountryCode()); |
|
| 2517 | } |
||
| 2518 | |||
| 2519 | /** |
||
| 2520 | * Checks whether the country calling code is from a region whose national significant number |
||
| 2521 | * could contain a leading zero. An example of such a region is Italy. Returns false if no |
||
| 2522 | * metadata for the country is found. |
||
| 2523 | * @param int $countryCallingCode |
||
| 2524 | * @return bool |
||
| 2525 | */ |
||
| 2526 | 2 | public function isLeadingZeroPossible($countryCallingCode) |
|
| 2527 | { |
||
| 2528 | 2 | $mainMetadataForCallingCode = $this->getMetadataForRegionOrCallingCode( |
|
| 2529 | 2 | $countryCallingCode, |
|
| 2530 | 2 | $this->getRegionCodeForCountryCode($countryCallingCode) |
|
| 2531 | 2 | ); |
|
| 2532 | 2 | if ($mainMetadataForCallingCode === null) { |
|
| 2533 | 1 | return false; |
|
| 2534 | } |
||
| 2535 | 2 | return (bool)$mainMetadataForCallingCode->isLeadingZeroPossible(); |
|
| 2536 | } |
||
| 2537 | |||
| 2538 | /** |
||
| 2539 | * @param PhoneNumber $number |
||
| 2540 | * @return bool |
||
| 2541 | */ |
||
| 2542 | 1 | protected function hasFormattingPatternForNumber(PhoneNumber $number) |
|
| 2554 | |||
| 2555 | /** |
||
| 2556 | * Returns the national dialling prefix for a specific region. For example, this would be 1 for |
||
| 2557 | * the United States, and 0 for New Zealand. Set stripNonDigits to true to strip symbols like "~" |
||
| 2558 | * (which indicates a wait for a dialling tone) from the prefix returned. If no national prefix is |
||
| 2559 | * present, we return null. |
||
| 2560 | * |
||
| 2561 | * <p>Warning: Do not use this method for do-your-own formatting - for some regions, the |
||
| 2562 | * national dialling prefix is used only for certain types of numbers. Use the library's |
||
| 2563 | * formatting functions to prefix the national prefix when required. |
||
| 2564 | * |
||
| 2565 | * @param string $regionCode the region that we want to get the dialling prefix for |
||
| 2566 | * @param boolean $stripNonDigits true to strip non-digits from the national dialling prefix |
||
| 2567 | * @return string the dialling prefix for the region denoted by regionCode |
||
| 2568 | */ |
||
| 2569 | 3 | public function getNddPrefixForRegion($regionCode, $stripNonDigits) |
|
| 2570 | { |
||
| 2571 | 3 | $metadata = $this->getMetadataForRegion($regionCode); |
|
| 2572 | 3 | if ($metadata === null) { |
|
| 2573 | 1 | return null; |
|
| 2574 | } |
||
| 2575 | 3 | $nationalPrefix = $metadata->getNationalPrefix(); |
|
| 2576 | // If no national prefix was found, we return null. |
||
| 2577 | 3 | if (mb_strlen($nationalPrefix) == 0) { |
|
| 2578 | 1 | return null; |
|
| 2579 | } |
||
| 2580 | 3 | if ($stripNonDigits) { |
|
| 2581 | // Note: if any other non-numeric symbols are ever used in national prefixes, these would have |
||
| 2582 | // to be removed here as well. |
||
| 2583 | 3 | $nationalPrefix = str_replace("~", "", $nationalPrefix); |
|
| 2584 | 3 | } |
|
| 2585 | 3 | return $nationalPrefix; |
|
| 2586 | } |
||
| 2587 | |||
| 2588 | /** |
||
| 2589 | * Check if rawInput, which is assumed to be in the national format, has a national prefix. The |
||
| 2590 | * national prefix is assumed to be in digits-only form. |
||
| 2591 | * @param string $rawInput |
||
| 2592 | * @param string $nationalPrefix |
||
| 2593 | * @param string $regionCode |
||
| 2594 | * @return bool |
||
| 2595 | */ |
||
| 2596 | 1 | protected function rawInputContainsNationalPrefix($rawInput, $nationalPrefix, $regionCode) |
|
| 2614 | |||
| 2615 | /** |
||
| 2616 | * Tests whether a phone number matches a valid pattern. Note this doesn't verify the number |
||
| 2617 | * is actually in use, which is impossible to tell by just looking at a number itself. |
||
| 2618 | * |
||
| 2619 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2620 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2621 | */ |
||
| 2622 | 1836 | public function isValidNumber(PhoneNumber $number) |
|
| 2627 | |||
| 2628 | /** |
||
| 2629 | * Tests whether a phone number is valid for a certain region. Note this doesn't verify the number |
||
| 2630 | * is actually in use, which is impossible to tell by just looking at a number itself. If the |
||
| 2631 | * country calling code is not the same as the country calling code for the region, this |
||
| 2632 | * immediately exits with false. After this, the specific number pattern rules for the region are |
||
| 2633 | * examined. This is useful for determining for example whether a particular number is valid for |
||
| 2634 | * Canada, rather than just a valid NANPA number. |
||
| 2635 | * Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this |
||
| 2636 | * method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for |
||
| 2637 | * the region "GB" (United Kingdom), since it has its own region code, "IM", which may be |
||
| 2638 | * undesirable. |
||
| 2639 | * |
||
| 2640 | * @param PhoneNumber $number the phone number that we want to validate |
||
| 2641 | * @param string $regionCode the region that we want to validate the phone number for |
||
| 2642 | * @return boolean that indicates whether the number is of a valid pattern |
||
| 2643 | */ |
||
| 2644 | 1842 | public function isValidNumberForRegion(PhoneNumber $number, $regionCode) |
|
| 2660 | |||
| 2661 | /** |
||
| 2662 | * Cleans all non-digit characters off of a phone number preserving a leading + sign if one is supplied. |
||
| 2663 | * @param $phoneNumber String phone number that you want to clean |
||
| 2664 | * @return string |
||
| 2665 | */ |
||
| 2666 | public function cleanPhoneNumber($phoneNumber) |
||
| 2676 | |||
| 2677 | /** |
||
| 2678 | * Parses a string and returns it as a phone number in proto buffer format. The method is quite |
||
| 2679 | * lenient and looks for a number in the input text (raw input) and does not check whether the |
||
| 2680 | * string is definitely only a phone number. To do this, it ignores punctuation and white-space, |
||
| 2681 | * as well as any text before the number (e.g. a leading “Tel: ”) and trims the non-number bits. |
||
| 2682 | * It will accept a number in any format (E164, national, international etc), assuming it can |
||
| 2683 | * interpreted with the defaultRegion supplied. It also attempts to convert any alpha characters |
||
| 2684 | * into digits if it thinks this is a vanity number of the type "1800 MICROSOFT". |
||
| 2685 | * |
||
| 2686 | * <p> This method will throw a {@link NumberParseException} if the number is not considered to |
||
| 2687 | * be a possible number. Note that validation of whether the number is actually a valid number |
||
| 2688 | * for a particular region is not performed. This can be done separately with {@link #isValidnumber}. |
||
| 2689 | * |
||
| 2690 | 2728 | * @param string $numberToParse number that we are attempting to parse. This can contain formatting |
|
| 2691 | * such as +, ( and -, as well as a phone number extension. |
||
| 2692 | 2728 | * @param string $defaultRegion region that we are expecting the number to be from. This is only used |
|
| 2693 | 2728 | * if the number being parsed is not written in international format. |
|
| 2694 | 2728 | * The country_code for the number in this case would be stored as that |
|
| 2695 | 2728 | * of the default region supplied. If the number is guaranteed to |
|
| 2696 | 2723 | * start with a '+' followed by the country calling code, then |
|
| 2697 | * "ZZ" or null can be supplied. |
||
| 2698 | * @param PhoneNumber|null $phoneNumber |
||
| 2699 | * @param bool $keepRawInput |
||
| 2700 | * @return PhoneNumber a phone number proto buffer filled with the parsed number |
||
| 2701 | * @throws NumberParseException if the string is not considered to be a viable phone number (e.g. |
||
| 2702 | * too few or too many digits) or if no default region was supplied |
||
| 2703 | * and the number is not in international format (does not start |
||
| 2704 | * with +) |
||
| 2705 | */ |
||
| 2706 | public function parse($numberToParse, $defaultRegion='ZZ', PhoneNumber $phoneNumber = null, $keepRawInput = false) |
||
| 2715 | |||
| 2716 | /** |
||
| 2717 | * Formats a phone number in the specified format using client-defined formatting rules. Note that |
||
| 2718 | * if the phone number has a country calling code of zero or an otherwise invalid country calling |
||
| 2719 | * code, we cannot work out things like whether there should be a national prefix applied, or how |
||
| 2720 | 2 | * to format extensions, so we return the national significant number with no formatting applied. |
|
| 2721 | * |
||
| 2722 | 2 | * @param PhoneNumber $number the phone number to be formatted |
|
| 2723 | * @param int $numberFormat the format the phone number should be formatted into |
||
| 2724 | 2 | * @param array $userDefinedFormats formatting rules specified by clients |
|
| 2725 | * @return String the formatted phone number |
||
| 2726 | 2 | */ |
|
| 2727 | 2 | public function formatByPattern(PhoneNumber $number, $numberFormat, array $userDefinedFormats) |
|
| 2774 | |||
| 2775 | /** |
||
| 2776 | * Gets a valid number for the specified region. |
||
| 2777 | * |
||
| 2778 | * @param string regionCode the region for which an example number is needed |
||
| 2779 | * @return PhoneNumber a valid fixed-line number for the specified region. Returns null when the metadata |
||
| 2780 | * does not contain such information, or the region 001 is passed in. For 001 (representing |
||
| 2781 | * non-geographical numbers), call {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 2782 | 244 | */ |
|
| 2783 | public function getExampleNumber($regionCode) |
||
| 2787 | |||
| 2788 | /** |
||
| 2789 | * Gets an invalid number for the specified region. This is useful for unit-testing purposes, |
||
| 2790 | * where you want to test what will happen with an invalid number. Note that the number that is |
||
| 2791 | * returned will always be able to be parsed and will have the correct country code. It may also |
||
| 2792 | * be a valid *short* number/code for this region. Validity checking such numbers is handled with |
||
| 2793 | 244 | * {@link ShortNumberInfo}. |
|
| 2794 | * |
||
| 2795 | 244 | * @param string $regionCode The region for which an example number is needed |
|
| 2796 | * @return PhoneNumber|null An invalid number for the specified region. Returns null when an unsupported region |
||
| 2797 | * or the region 001 (Earth) is passed in. |
||
| 2798 | */ |
||
| 2799 | public function getInvalidExampleNumber($regionCode) |
||
| 2845 | |||
| 2846 | /** |
||
| 2847 | * Gets a valid number for the specified region and number type. |
||
| 2848 | 15 | * |
|
| 2849 | 15 | * @param string $regionCodeOrType the region for which an example number is needed |
|
| 2850 | 15 | * @param int $type the PhoneNumberType of number that is needed |
|
| 2851 | 11 | * @return PhoneNumber a valid number for the specified region and type. Returns null when the metadata |
|
| 2852 | * does not contain such information or if an invalid region or region 001 was entered. |
||
| 2853 | 9 | * For 001 (representing non-geographical numbers), call |
|
| 2854 | * {@link #getExampleNumberForNonGeoEntity} instead. |
||
| 2855 | * |
||
| 2856 | 4 | * If $regionCodeOrType is the only parameter supplied, then a valid number for the specified number type |
|
| 2857 | 4 | * will be returned that may belong to any country. |
|
| 2858 | */ |
||
| 2859 | 4 | public function getExampleNumberForType($regionCodeOrType, $type = null) |
|
| 2899 | 1214 | ||
| 2900 | 1474 | /** |
|
| 2901 | 245 | * @param PhoneMetadata $metadata |
|
| 2902 | 1229 | * @param int $type PhoneNumberType |
|
| 2903 | 245 | * @return PhoneNumberDesc |
|
| 2904 | 984 | */ |
|
| 2905 | 245 | protected function getNumberDescByType(PhoneMetadata $metadata, $type) |
|
| 2933 | |||
| 2934 | /** |
||
| 2935 | * Gets a valid number for the specified country calling code for a non-geographical entity. |
||
| 2936 | * |
||
| 2937 | * @param int $countryCallingCode the country calling code for a non-geographical entity |
||
| 2938 | * @return PhoneNumber a valid number for the non-geographical entity. Returns null when the metadata |
||
| 2939 | * does not contain such information, or the country calling code passed in does not belong |
||
| 2940 | * to a non-geographical entity. |
||
| 2941 | */ |
||
| 2942 | public function getExampleNumberForNonGeoEntity($countryCallingCode) |
||
| 2956 | |||
| 2957 | |||
| 2958 | /** |
||
| 2959 | * Takes two phone numbers and compares them for equality. |
||
| 2960 | * |
||
| 2961 | * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero |
||
| 2962 | * for Italian numbers and any extension present are the same. Returns NSN_MATCH |
||
| 2963 | 4 | * if either or both has no region specified, and the NSNs and extensions are |
|
| 2964 | * the same. Returns SHORT_NSN_MATCH if either or both has no region specified, |
||
| 2965 | 4 | * or the region specified is the same, and one NSN could be a shorter version |
|
| 2966 | * of the other number. This includes the case where one has an extension |
||
| 2967 | 4 | * specified, and the other does not. Returns NO_MATCH otherwise. For example, |
|
| 2968 | 4 | * the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH. The numbers |
|
| 2969 | 3 | * +1 345 657 1234 and 345 657 are a NO_MATCH. |
|
| 2970 | 3 | * |
|
| 2971 | * @param $firstNumberIn PhoneNumber|string First number to compare. If it is a |
||
| 2972 | 3 | * string it can contain formatting, and can have country calling code specified |
|
| 2973 | 2 | * with + at the start. |
|
| 2974 | 3 | * @param $secondNumberIn PhoneNumber|string Second number to compare. If it is a |
|
| 2975 | 3 | * string it can contain formatting, and can have country calling code specified |
|
| 2976 | * with + at the start. |
||
| 2977 | 3 | * @throws \InvalidArgumentException |
|
| 2978 | 3 | * @return int {MatchType} NOT_A_NUMBER, NO_MATCH, |
|
| 2979 | 3 | */ |
|
| 2980 | 3 | public function isNumberMatch($firstNumberIn, $secondNumberIn) |
|
| 3102 | 3 | ||
| 3103 | 3 | /** |
|
| 3104 | 3 | * Returns true when one national number is the suffix of the other or both are the same. |
|
| 3105 | * @param PhoneNumber $firstNumber |
||
| 3106 | * @param PhoneNumber $secondNumber |
||
| 3107 | * @return bool |
||
| 3108 | */ |
||
| 3109 | protected function isNationalNumberSuffixOfTheOther(PhoneNumber $firstNumber, PhoneNumber $secondNumber) |
||
| 3116 | |||
| 3117 | 3 | protected function stringEndsWithString($hayStack, $needle) |
|
| 3123 | |||
| 3124 | /** |
||
| 3125 | * Returns true if the supplied region supports mobile number portability. Returns false for |
||
| 3126 | * invalid, unknown or regions that don't support mobile number portability. |
||
| 3127 | * |
||
| 3128 | * @param string $regionCode the region for which we want to know whether it supports mobile number |
||
| 3129 | * portability or not. |
||
| 3130 | * @return bool |
||
| 3131 | */ |
||
| 3132 | public function isMobileNumberPortableRegion($regionCode) |
||
| 3141 | |||
| 3142 | /** |
||
| 3143 | * Check whether a phone number is a possible number given a number in the form of a string, and |
||
| 3144 | 2 | * the region where the number could be dialed from. It provides a more lenient check than |
|
| 3145 | * {@link #isValidNumber}. See {@link #isPossibleNumber(PhoneNumber)} for details. |
||
| 3146 | 2 | * |
|
| 3147 | * <p>This method first parses the number, then invokes {@link #isPossibleNumber(PhoneNumber)} |
||
| 3148 | 2 | * with the resultant PhoneNumber object. |
|
| 3149 | 2 | * |
|
| 3150 | 2 | * @param PhoneNumber|string $number the number that needs to be checked, in the form of a string |
|
| 3151 | 1 | * @param string $regionDialingFrom the region that we are expecting the number to be dialed from. |
|
| 3152 | 1 | * Note this is different from the region where the number belongs. For example, the number |
|
| 3153 | * +1 650 253 0000 is a number that belongs to US. When written in this form, it can be |
||
| 3154 | * dialed from any region. When it is written as 00 1 650 253 0000, it can be dialed from any |
||
| 3155 | 2 | * region which uses an international dialling prefix of 00. When it is written as |
|
| 3156 | * 650 253 0000, it can only be dialed from within the US, and when written as 253 0000, it |
||
| 3157 | * can only be dialed from within a smaller area in the US (Mountain View, CA, to be more |
||
| 3158 | * specific). |
||
| 3159 | * @return boolean true if the number is possible |
||
| 3160 | */ |
||
| 3161 | public function isPossibleNumber($number, $regionDialingFrom = null) |
||
| 3175 | |||
| 3176 | |||
| 3177 | /** |
||
| 3178 | * Check whether a phone number is a possible number. It provides a more lenient check than |
||
| 3179 | * {@link #isValidNumber} in the following sense: |
||
| 3180 | * <ol> |
||
| 3181 | 4 | * <li> It only checks the length of phone numbers. In particular, it doesn't check starting |
|
| 3182 | * digits of the number. |
||
| 3183 | 4 | * <li> It doesn't attempt to figure out the type of the number, but uses general rules which |
|
| 3184 | 4 | * applies to all types of phone numbers in a region. Therefore, it is much faster than |
|
| 3185 | * isValidNumber. |
||
| 3186 | * <li> For fixed line numbers, many regions have the concept of area code, which together with |
||
| 3187 | * subscriber number constitute the national significant number. It is sometimes okay to dial |
||
| 3188 | * the subscriber number only when dialing in the same area. This function will return |
||
| 3189 | 4 | * true if the subscriber-number-only version is passed in. On the other hand, because |
|
| 3190 | 1 | * isValidNumber validates using information on both starting digits (for fixed line |
|
| 3191 | * numbers, that would most likely be area codes) and length (obviously includes the |
||
| 3192 | * length of area codes for fixed line numbers), it will return false for the |
||
| 3193 | 4 | * subscriber-number-only version. |
|
| 3194 | * </ol> |
||
| 3195 | 4 | * @param PhoneNumber $number the number that needs to be checked |
|
| 3196 | * @return int a ValidationResult object which indicates whether the number is possible |
||
| 3197 | 4 | */ |
|
| 3198 | 4 | public function isPossibleNumberWithReason(PhoneNumber $number) |
|
| 3217 | 1 | ||
| 3218 | 1 | /** |
|
| 3219 | 1 | * Attempts to extract a valid number from a phone number that is too long to be valid, and resets |
|
| 3220 | 1 | * the PhoneNumber object passed in to that valid version. If no valid number could be extracted, |
|
| 3221 | * the PhoneNumber object passed in will not be modified. |
||
| 3222 | 1 | * @param PhoneNumber $number a PhoneNumber object which contains a number that is too long to be valid. |
|
| 3223 | 1 | * @return boolean true if a valid phone number can be successfully extracted. |
|
| 3224 | 1 | */ |
|
| 3225 | public function truncateTooLongNumber(PhoneNumber $number) |
||
| 3243 | } |
||
| 3244 |
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.