/** * @dataProvider localeList * @param string $regionCode * @param string $countryName */ public function testLocales($regionCode, $countryName) { if (!in_array($regionCode, $this->phoneUtil->getSupportedRegions())) { $this->markTestSkipped("{$regionCode} is not supported"); } $phoneNumber = $this->phoneUtil->getExampleNumberForType($regionCode, PhoneNumberType::FIXED_LINE_OR_MOBILE); $this->assertContains($regionCode, CountryCodeToRegionCodeMap::$countryCodeToRegionCodeMap[$phoneNumber->getCountryCode()]); $this->assertEquals($regionCode, $this->phoneUtil->getRegionCodeForNumber($phoneNumber)); $this->assertEquals($countryName, $this->geocoder->getDescriptionForValidNumber($phoneNumber, 'en', 'ZZ'), "Checking {$phoneNumber} is part of {$countryName}"); }
/** * @param string $number * @param string $country * @param string|NULL $locale * @param string|NULL $userCountry * * @return string * * @throws Exceptions\NoValidCountryException * @throws Exceptions\NoValidPhoneException */ public function getLocation($number, $country = 'AUTO', $locale = NULL, $userCountry = NULL) { if ($this->isValid((string) $number, $country)) { $country = strtoupper($country); if ($userCountry !== NULL) { // Check for valid user country $userCountry = $this->validateCountry($userCountry); } // Parse phone number $parsed = $this->phoneNumberUtil->parse((string) $number, $country); // Determine locale $locale = $locale === NULL && $this->translator && method_exists($this->translator, 'getLocale') ? $this->translator->getLocale() : 'en_US'; // Get phone number location return $this->phoneNumberGeocoder->getDescriptionForNumber($parsed, $locale, $userCountry); } else { throw new Exceptions\NoValidPhoneException('Provided phone number "' . $number . '" is not valid phone number. Provide valid phone number.'); } }
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()); } } }
/** * @expectedException \RuntimeException * @expectedExceptionMessage The intl extension must be installed */ public function testPhoneNumberOfflineGeocoder() { PhoneNumberOfflineGeocoder::getInstance(); }
public function testIsleOfManLocale() { $number = "447624806000"; $phoneNumber = $this->phoneUtil->parse($number, 'GB'); $this->assertEquals("Isle of Man", $this->geocoder->getDescriptionForNumber($phoneNumber, 'en')); }
public function testGetDescriptionForInvalidNumber() { $this->assertEquals("", $this->geocoder->getDescriptionForNumber(self::$KO_InvalidNumber, "en")); $this->assertEquals("", $this->geocoder->getDescriptionForNumber(self::$US_InvalidNumber, "en")); }
public function testChineseGeolocation() { $number = $this->phoneUtil->parse("+86 150 3657 7264", "CN"); $location = $this->geocoder->getDescriptionForNumber($number, "en"); $this->assertEquals("Luoyang, Henan", $location); }
/** * @return string */ public function getDescription() : string { list($language, $region) = explode('-', self::translator()->getIETF()); $phoneNumberGeocoder = PhoneNumberOfflineGeocoder::getInstance(); return $phoneNumberGeocoder->getDescriptionForNumber($this->number, $language, $region); }
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 testIsleOfManLocale() { $number = "447797752305"; $phoneNumber = $this->phoneUtil->parse($number, 'GB'); $this->assertEquals("Jersey", $this->geocoder->getDescriptionForNumber($phoneNumber, 'en')); }
public function testTKGeoLocation() { $number = '+6903010'; $phoneNumber = $this->phoneUtil->parse($number, RegionCode::ZZ); $this->assertEquals('TK', $this->phoneUtil->getRegionCodeForNumber($phoneNumber)); $this->assertEquals('Tokelau', $this->geocoder->getDescriptionForNumber($phoneNumber, 'en')); }