/**
  * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting,
  * parsing, or validation. The instance is loaded with phone number metadata for a number of most
  * commonly used regions.
  *
  * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance
  * multiple times will only result in one instance being created.
  *
  * @param string $baseFileLocation
  * @param array|null $countryCallingCodeToRegionCodeMap
  * @return PhoneNumberUtil instance
  */
 public static function getInstance($baseFileLocation = self::META_DATA_FILE_PREFIX, array $countryCallingCodeToRegionCodeMap = NULL)
 {
     if ($countryCallingCodeToRegionCodeMap === NULL) {
         $countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap;
     }
     if (self::$instance == null) {
         self::$instance = new PhoneNumberUtil();
         self::$instance->countryCallingCodeToRegionCodeMap = $countryCallingCodeToRegionCodeMap;
         self::$instance->init($baseFileLocation);
         self::initExtnPatterns();
         self::initAsciiDigitMappings();
         self::initCapturingExtnDigits();
         self::initExtnPattern();
         self::$PLUS_CHARS_PATTERN = "[" . self::PLUS_CHARS . "]+";
         self::$SEPARATOR_PATTERN = "[" . self::VALID_PUNCTUATION . "]+";
         self::$CAPTURING_DIGIT_PATTERN = "(" . self::DIGITS . ")";
         self::$VALID_START_CHAR_PATTERN = "[" . self::PLUS_CHARS . self::DIGITS . "]";
         self::$ALPHA_PHONE_MAPPINGS = self::$ALPHA_MAPPINGS + self::$asciiDigitMappings;
         self::$DIALLABLE_CHAR_MAPPINGS = self::$asciiDigitMappings;
         self::$DIALLABLE_CHAR_MAPPINGS['+'] = '+';
         self::$DIALLABLE_CHAR_MAPPINGS['*'] = '*';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS = array();
         // Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
         foreach (self::$ALPHA_MAPPINGS as $c => $value) {
             self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[strtolower($c)] = $c;
             self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[$c] = $c;
         }
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS += self::$asciiDigitMappings;
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‐"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‑"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‒"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["–"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["—"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["―"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["−"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " ";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " ";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["⁠"] = " ";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = ".";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = ".";
     }
     return self::$instance;
 }
Пример #2
0
 /**
  * This class implements a singleton, so the only constructor is private.
  * @param MetadataSourceInterface $metadataSource
  * @param $countryCallingCodeToRegionCodeMap
  */
 private function __construct(MetadataSourceInterface $metadataSource, $countryCallingCodeToRegionCodeMap)
 {
     $this->metadataSource = $metadataSource;
     $this->countryCallingCodeToRegionCodeMap = $countryCallingCodeToRegionCodeMap;
     $this->init();
     self::initCapturingExtnDigits();
     self::initExtnPatterns();
     self::initExtnPattern();
     self::$PLUS_CHARS_PATTERN = "[" . self::PLUS_CHARS . "]+";
     self::$SEPARATOR_PATTERN = "[" . self::VALID_PUNCTUATION . "]+";
     self::$CAPTURING_DIGIT_PATTERN = "(" . self::DIGITS . ")";
     self::$VALID_START_CHAR_PATTERN = "[" . self::PLUS_CHARS . self::DIGITS . "]";
     self::$ALPHA_PHONE_MAPPINGS = self::$ALPHA_MAPPINGS + self::$asciiDigitMappings;
     self::$DIALLABLE_CHAR_MAPPINGS = self::$asciiDigitMappings;
     self::$DIALLABLE_CHAR_MAPPINGS[self::PLUS_SIGN] = self::PLUS_SIGN;
     self::$DIALLABLE_CHAR_MAPPINGS['*'] = '*';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS = array();
     // Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
     foreach (self::$ALPHA_MAPPINGS as $c => $value) {
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[strtolower($c)] = $c;
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[$c] = $c;
     }
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS += self::$asciiDigitMappings;
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‐"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‑"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‒"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["–"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["—"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["―"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["−"] = '-';
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/";
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/";
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " ";
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " ";
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["⁠"] = " ";
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = ".";
     self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = ".";
     self::$MIN_LENGTH_PHONE_NUMBER_PATTERN = "[" . self::DIGITS . "]{" . self::MIN_LENGTH_FOR_NSN . "}";
     self::$VALID_PHONE_NUMBER = "[" . self::PLUS_CHARS . "]*(?:[" . self::VALID_PUNCTUATION . self::STAR_SIGN . "]*[" . self::DIGITS . "]){3,}[" . self::VALID_PUNCTUATION . self::STAR_SIGN . self::VALID_ALPHA . self::DIGITS . "]*";
     self::$VALID_PHONE_NUMBER_PATTERN = "%^" . self::$MIN_LENGTH_PHONE_NUMBER_PATTERN . "\$|^" . self::$VALID_PHONE_NUMBER . "(?:" . self::$EXTN_PATTERNS_FOR_PARSING . ")?\$%" . self::REGEX_FLAGS;
     self::$UNWANTED_END_CHAR_PATTERN = "[^" . self::DIGITS . self::VALID_ALPHA . "#]+\$";
     self::$MOBILE_TOKEN_MAPPINGS = array();
     self::$MOBILE_TOKEN_MAPPINGS['52'] = "1";
     self::$MOBILE_TOKEN_MAPPINGS['54'] = "9";
 }
 /**
  * Gets a {@link PhoneNumberUtil} instance to carry out international phone number formatting,
  * parsing, or validation. The instance is loaded with phone number metadata for a number of most
  * commonly used regions.
  *
  * <p>The {@link PhoneNumberUtil} is implemented as a singleton. Therefore, calling getInstance
  * multiple times will only result in one instance being created.
  *
  * @param string $baseFileLocation
  * @param array|null $countryCallingCodeToRegionCodeMap
  * @return PhoneNumberUtil instance
  */
 public static function getInstance($baseFileLocation = self::META_DATA_FILE_PREFIX, array $countryCallingCodeToRegionCodeMap = null)
 {
     if ($countryCallingCodeToRegionCodeMap === null) {
         $countryCallingCodeToRegionCodeMap = CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap;
     }
     if (self::$instance === null) {
         self::$instance = new PhoneNumberUtil();
         self::$instance->countryCallingCodeToRegionCodeMap = $countryCallingCodeToRegionCodeMap;
         self::$instance->init($baseFileLocation);
         self::initCapturingExtnDigits();
         self::initExtnPatterns();
         self::initAsciiDigitMappings();
         self::initExtnPattern();
         self::$PLUS_CHARS_PATTERN = "[" . self::PLUS_CHARS . "]+";
         self::$SEPARATOR_PATTERN = "[" . self::VALID_PUNCTUATION . "]+";
         self::$CAPTURING_DIGIT_PATTERN = "(" . self::DIGITS . ")";
         self::$VALID_START_CHAR_PATTERN = "[" . self::PLUS_CHARS . self::DIGITS . "]";
         self::$ALPHA_PHONE_MAPPINGS = self::$ALPHA_MAPPINGS + self::$asciiDigitMappings;
         self::$DIALLABLE_CHAR_MAPPINGS = self::$asciiDigitMappings;
         self::$DIALLABLE_CHAR_MAPPINGS[self::PLUS_SIGN] = self::PLUS_SIGN;
         self::$DIALLABLE_CHAR_MAPPINGS['*'] = '*';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS = array();
         // Put (lower letter -> upper letter) and (upper letter -> upper letter) mappings.
         foreach (self::$ALPHA_MAPPINGS as $c => $value) {
             self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[strtolower($c)] = $c;
             self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[$c] = $c;
         }
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS += self::$asciiDigitMappings;
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["-"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‐"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‑"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["‒"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["–"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["—"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["―"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["−"] = '-';
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["/"] = "/";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " ";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS[" "] = " ";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["⁠"] = " ";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = ".";
         self::$ALL_PLUS_NUMBER_GROUPING_SYMBOLS["."] = ".";
         self::$MIN_LENGTH_PHONE_NUMBER_PATTERN = "[" . self::DIGITS . "]{" . self::MIN_LENGTH_FOR_NSN . "}";
         self::$VALID_PHONE_NUMBER = "[" . self::PLUS_CHARS . "]*(?:[" . self::VALID_PUNCTUATION . self::STAR_SIGN . "]*[" . self::DIGITS . "]){3,}[" . self::VALID_PUNCTUATION . self::STAR_SIGN . self::VALID_ALPHA . self::DIGITS . "]*";
         self::$VALID_PHONE_NUMBER_PATTERN = "%^" . self::$MIN_LENGTH_PHONE_NUMBER_PATTERN . "\$|^" . self::$VALID_PHONE_NUMBER . "(?:" . self::$EXTN_PATTERNS_FOR_PARSING . ")?%" . self::REGEX_FLAGS;
         self::$UNWANTED_END_CHAR_PATTERN = "[^" . self::DIGITS . self::VALID_ALPHA . "#]+\$";
         self::$MOBILE_TOKEN_MAPPINGS = array();
         self::$MOBILE_TOKEN_MAPPINGS['52'] = "1";
         self::$MOBILE_TOKEN_MAPPINGS['54'] = "9";
         self::loadNumericCharacters();
     }
     return self::$instance;
 }