getInstance() public static method

The {@link PhoneNumberToCarrierMapper} is implemented as a singleton. Therefore, calling this method multiple times will only result in one instance being created.

public static getInstance ( string $mappingDir = self::MAPPING_DATA_DIRECTORY ) : PhoneNumberToCarrierMapper
$mappingDir string
return PhoneNumberToCarrierMapper
示例#1
1
 public function testChineseCarrierLookup()
 {
     $number = $this->phoneUtil->parse("+86 150 3657 7264", "CN");
     $carrier = PhoneNumberToCarrierMapper::getInstance();
     $location = $carrier->getNameForNumber($number, "en");
     $this->assertEquals("China Mobile", $location);
 }
 public function setUp()
 {
     if (!extension_loaded('intl')) {
         $this->markTestSkipped('The intl extension must be installed');
     }
     $this->carrierMapper = PhoneNumberToCarrierMapper::getInstance(self::TEST_META_DATA_FILE_PREFIX);
 }
示例#3
0
 /**
  * Test if construct set number util
  * @return void
  * @author Andraz <*****@*****.**>
  */
 public function test_construct()
 {
     $numberUtil = PhoneNumberUtil::getInstance();
     $carrierMapper = PhoneNumberToCarrierMapper::getInstance();
     $instance = new Instance($numberUtil, $carrierMapper);
     $this->assertInstanceOf('libphonenumber\\PhoneNumberUtil', $instance->getNumberUtil());
     $this->assertInstanceOf('libphonenumber\\PhoneNumberToCarrierMapper', $instance->getCarrierMapper());
 }
示例#4
0
 public function __construct($number, array $config = NULL)
 {
     $this->number = $number;
     $this->config = Kohana::$config->load('phonenumber')->as_array();
     if (!empty($config)) {
         $this->config = Arr::merge($this->config, $config);
     }
     if (!isset($this->config['country'])) {
         $this->config['country'] = '';
     }
     if (empty($this->config['country'])) {
         throw new Kohana_Exception('Country code is required');
     }
     $this->config['country'] = strtoupper($this->config['country']);
     if (!isset($this->config['language'])) {
         $this->config['language'] = '';
     }
     if (!isset($this->config['region'])) {
         $this->config['region'] = '';
     }
     $this->config['language'] = empty($this->config['language']) ? 'en' : strtolower($this->config['language']);
     $this->config['region'] = empty($this->config['region']) ? NULL : strtoupper($this->config['region']);
     $this->phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
     $this->shortNumberInfo = \libphonenumber\ShortNumberInfo::getInstance();
     $this->phoneNumberGeocoder = \libphonenumber\geocoding\PhoneNumberOfflineGeocoder::getInstance();
     if (!empty($number)) {
         try {
             $this->phoneNumber = $this->phoneNumberUtil->parse($number, $this->config['country'], NULL, TRUE);
             $this->possibleNumber = $this->phoneNumberUtil->isPossibleNumber($this->phoneNumber);
             $this->isPossibleNumberWithReason = $this->phoneNumberUtil->isPossibleNumberWithReason($this->phoneNumber);
             $this->validNumber = $this->phoneNumberUtil->isValidNumber($this->phoneNumber);
             $this->validNumberForRegion = $this->phoneNumberUtil->isValidNumberForRegion($this->phoneNumber, $input['country']);
             $this->phoneNumberRegion = $this->phoneNumberUtil->getRegionCodeForNumber($this->phoneNumber);
             $this->phoneNumberType = $this->phoneNumberUtil->getNumberType($this->phoneNumber);
             $this->geolocation = $this->phoneNumberGeocoder->getDescriptionForNumber($this->phoneNumber, $this->config['language'], $this->config['region']);
             $this->phoneNumberToCarrierInfo = \libphonenumber\PhoneNumberToCarrierMapper::getInstance()->getNameForNumber($this->phoneNumber, $this->config['language']);
             $this->timezone = \libphonenumber\PhoneNumberToTimeZonesMapper::getInstance()->getTimeZonesForNumber($this->phoneNumber);
         } catch (\libphonenumber\NumberParseException $e) {
             throw new Kohana_Exception(':error', array(':error' => $e->getMessage()), $e->getCode());
         }
     }
 }
示例#5
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage The intl extension must be installed
  */
 public function testPhoneNumberToCarrierMapper()
 {
     PhoneNumberToCarrierMapper::getInstance();
 }
示例#6
0
 /**
  * @return string
  */
 public function getCarrier() : string
 {
     $carrierMapper = PhoneNumberToCarrierMapper::getInstance();
     return $carrierMapper->getNameForNumber($this->number, self::locale());
 }
 private function parse()
 {
     if ($this->phoneNumberProto) {
         // from phoneNumberProto
         $this->country_code = $this->phoneNumberProto->getCountryCode();
         $this->country_code_plus_sign = "+" . $this->country_code;
         $this->national_number = $this->phoneNumberProto->getNationalNumber();
         $this->extension = $this->phoneNumberProto->getExtension();
         $this->country_code_source = $this->phoneNumberProto->getCountryCodeSource();
         if (isset($this->countryCodeSourcesText[$this->country_code_source])) {
             $this->country_code_source_text = $this->countryCodeSourcesText[$this->country_code_source];
         }
         $this->raw_input = $this->phoneNumberProto->getRawInput();
         $this->preferred_domestic_carrier_code = $this->phoneNumberProto->getPreferredDomesticCarrierCode();
         // from validation
         $this->is_possible_number = $this->phoneUtil->isPossibleNumber($this->phoneNumberProto);
         $this->is_valid_number = $this->phoneUtil->isValidNumber($this->phoneNumberProto);
         $this->region_code_for_number = $this->phoneUtil->getRegionCodeForNumber($this->phoneNumberProto);
         $this->number_type = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         $this->number_type_text = $this->phoneUtil->getNumberType($this->phoneNumberProto);
         if (isset($this->phoneNumberTypesText[$this->number_type])) {
             $this->number_type_text = $this->phoneNumberTypesText[$this->number_type];
         } else {
             $this->number_type_text = "OTHER";
         }
         $this->is_mobile_number = in_array($this->number_type, [PhoneNumberType::FIXED_LINE_OR_MOBILE, PhoneNumberType::MOBILE]);
         // from formatting
         $this->format_e164 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::E164);
         $this->format_international = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::INTERNATIONAL);
         $this->format_national = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::NATIONAL);
         $this->format_rfc3966 = $this->phoneUtil->format($this->phoneNumberProto, PhoneNumberFormat::RFC3966);
         // from additional
         $phoneNumberOfflineGeocoder = PhoneNumberOfflineGeocoder::getInstance();
         $this->description = $phoneNumberOfflineGeocoder->getDescriptionForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToCarrierMapper = PhoneNumberToCarrierMapper::getInstance();
         $this->carrier_name = $phoneNumberToCarrierMapper->getNameForNumber($this->phoneNumberProto, 'en');
         $phoneNumberToTimeZonesMapper = PhoneNumberToTimeZonesMapper::getInstance();
         $this->timezones = $phoneNumberToTimeZonesMapper->getTimeZonesForNumber($this->phoneNumberProto);
     }
 }
 public function setUp()
 {
     PhoneNumberUtil::resetInstance();
     $this->carrierMapper = PhoneNumberToCarrierMapper::getInstance(self::TEST_META_DATA_FILE_PREFIX);
 }
示例#9
0
文件: Phone.php 项目: iPublikuj/phone
 /**
  * {@inheritdoc}
  */
 public static function fromNumber($number, $country = 'AUTO')
 {
     $phoneNumberUtil = libphonenumber\PhoneNumberUtil::getInstance();
     $carrierMapper = libphonenumber\PhoneNumberToCarrierMapper::getInstance();
     $timeZonesMapper = libphonenumber\PhoneNumberToTimeZonesMapper::getInstance();
     // Country code have to be upper-cased
     $country = strtoupper($country);
     // Correct auto or null value
     if ($country === 'AUTO' || $country === NULL) {
         $country = 'AUTO';
     } else {
         if (strlen($country) !== 2 || ctype_alpha($country) === FALSE || !in_array($country, $phoneNumberUtil->getSupportedRegions())) {
             throw new Exceptions\NoValidCountryException('Provided country code "' . $country . '" is not valid. Provide valid country code or AUTO for automatic detection.');
         }
     }
     try {
         // Parse string into phone number
         $parsed = $phoneNumberUtil->parse($number, $country);
         // Check if number is valid
         if ($country == 'AUTO' && $phoneNumberUtil->isValidNumber($parsed) === FALSE || $country != 'AUTO' && $phoneNumberUtil->isValidNumberForRegion($parsed, $country) === FALSE) {
             throw new Exceptions\NoValidPhoneException('Provided phone number "' . $number . '" is not valid phone number. Provide valid phone number.');
         }
     } catch (libphonenumber\NumberParseException $ex) {
         switch ($ex->getErrorType()) {
             case libphonenumber\NumberParseException::INVALID_COUNTRY_CODE:
                 throw new Exceptions\NoValidCountryException('Missing or invalid country.');
             case libphonenumber\NumberParseException::NOT_A_NUMBER:
                 throw new Exceptions\NoValidPhoneException('The string supplied did not seem to be a phone number.');
             case libphonenumber\NumberParseException::TOO_SHORT_AFTER_IDD:
                 throw new Exceptions\NoValidPhoneException('Phone number had an IDD, but after this was not long enough to be a viable phone number.');
             case libphonenumber\NumberParseException::TOO_SHORT_NSN:
                 throw new Exceptions\NoValidPhoneException('The string supplied is too short to be a phone number.');
             case libphonenumber\NumberParseException::TOO_LONG:
                 throw new Exceptions\NoValidPhoneException('The string supplied was too long to parse into phone number.');
             default:
                 throw new Exceptions\NoValidPhoneException('Provided phone number "' . $number . '" is not valid phone number. Provide valid phone number.');
         }
     }
     switch ($phoneNumberUtil->getNumberType($parsed)) {
         case libphonenumber\PhoneNumberType::MOBILE:
             $numberType = IPub\Phone\Phone::TYPE_MOBILE;
             break;
         case libphonenumber\PhoneNumberType::FIXED_LINE:
             $numberType = IPub\Phone\Phone::TYPE_FIXED_LINE;
             break;
         case libphonenumber\PhoneNumberType::FIXED_LINE_OR_MOBILE:
             $numberType = IPub\Phone\Phone::TYPE_FIXED_LINE_OR_MOBILE;
             break;
         case libphonenumber\PhoneNumberType::VOIP:
             $numberType = IPub\Phone\Phone::TYPE_VOIP;
             break;
         case libphonenumber\PhoneNumberType::PAGER:
             $numberType = IPub\Phone\Phone::TYPE_PAGER;
             break;
         case libphonenumber\PhoneNumberType::EMERGENCY:
             $numberType = IPub\Phone\Phone::TYPE_EMERGENCY;
             break;
         case libphonenumber\PhoneNumberType::VOICEMAIL:
             $numberType = IPub\Phone\Phone::TYPE_VOICEMAIL;
             break;
         default:
             $numberType = IPub\Phone\Phone::TYPE_UNKNOWN;
             break;
     }
     $entity = new static($phoneNumberUtil->format($parsed, PhoneNumberFormat::E164), $phoneNumberUtil->format($parsed, PhoneNumberFormat::RFC3966), $phoneNumberUtil->format($parsed, PhoneNumberFormat::NATIONAL), $phoneNumberUtil->format($parsed, PhoneNumberFormat::INTERNATIONAL), $parsed->getCountryCode(), $phoneNumberUtil->getRegionCodeForNumber($parsed), $numberType, $carrierMapper->getNameForNumber($parsed, 'en') ?: NULL);
     $entity->setItalianLeadingZero($parsed->hasItalianLeadingZero());
     $entity->setTimeZones($timeZonesMapper->getTimeZonesForNumber($parsed));
     if ($parsed->hasExtension()) {
         $entity->setExtension($parsed->getExtension());
     }
     if ($parsed->hasNumberOfLeadingZeros()) {
         $entity->setNumberOfLeadingZeros($parsed->getNumberOfLeadingZeros());
     }
     return $entity;
 }