public function testGBNumber() { $number = new PhoneNumber(); $number->setCountryCode(44)->setNationalNumber(1614960000); $timeZone = PhoneNumberToTimeZonesMapper::getInstance(); $this->assertEquals(array("Europe/London"), $timeZone->getTimeZonesForNumber($number)); }
public function testEqualWithPreferredDomesticCarrierCodeSetToDefault() { $numberA = new PhoneNumber(); $numberA->setCountryCode(1)->setNationalNumber(6502530000)->setPreferredDomesticCarrierCode(""); $numberB = new PhoneNumber(); $numberB->setCountryCode(1)->setNationalNumber(6502530000)->setPreferredDomesticCarrierCode(""); $this->assertEquals($numberA, $numberB); }
public function send(Message $message, PhoneNumber $recipient) { foreach ($this->whitelist as $whitelisted) { if ($recipient->equals($whitelisted)) { return $this->sender->send($message, $recipient); } } return true; }
/** * Returns the description of the geographical area the {@code number} corresponds to. This method * distinguishes the case of an invalid prefix and a prefix for which the name is not available in * the current language. If the description is not available in the current language an empty * string is returned. If no description was found for the provided number, null is returned. * * @internal param \libphonenumber\PhoneNumber $number the phone number to look up * @param \libphonenumber\PhoneNumber $phoneNumber * @return string|null bla the description of the geographical area */ public function lookup(PhoneNumber $phoneNumber) { if (count($this->areaCodeMapStorage) == 0) { return null; } $phonePrefix = $phoneNumber->getCountryCode() . $this->phoneUtil->getNationalSignificantNumber($phoneNumber); while (strlen($phonePrefix) > 0) { if (array_key_exists($phonePrefix, $this->areaCodeMapStorage)) { return $this->areaCodeMapStorage[$phonePrefix]; } $phonePrefix = substr($phonePrefix, 0, -1); } return null; }
public function testSerializingPhoneNumber2() { $phoneNumber = new PhoneNumber(); $phoneNumber->setCountryCode(1); $phoneNumber->setNationalNumber(1); $phoneNumber->setExtension(1); $phoneNumber->setItalianLeadingZero(1); $phoneNumber->setNumberOfLeadingZeros(1); $phoneNumber->setRawInput(1); $phoneNumber->setCountryCodeSource(1); $phoneNumber->setPreferredDomesticCarrierCode(1); $serializedString = serialize($phoneNumber); $phoneObject2 = unserialize($serializedString); $this->assertTrue($phoneObject2->equals($phoneNumber)); }
/** * @param Message $message * @param PhoneNumber $recipient * @throws SenderException * @return bool */ public function send(Message $message, PhoneNumber $recipient) { $params = ['api_key' => $this->apiKey, 'api_secret' => $this->apiSecret, 'from' => $this->senderName, 'to' => $recipient->getCountryCode() . $recipient->getNationalNumber(), 'text' => $message->getContent(), 'type' => $message->getType() === Message::TYPE_UNICODE ? "unicode" : "text"]; $url = self::NEXTMO_ENDPOINT . '?' . http_build_query($params); $response = $this->getResponse($url); if (false === isset($response['messages'])) { throw new SenderException('Invalid nexmo response: ' . var_export($response, true)); } foreach ($response['messages'] as $message) { if ($message['status'] !== "0") { throw new SenderException($message['error-text'], $message['status']); } } return true; }
public function equals(PhoneNumberModel $other) { if (empty($this->isValid()) || empty($other->isValid())) { return false; } return $this->phoneNumberProto->equals($other->phoneNumberProto); }
public function testLookupTimeZonesForNumber_RU() { $number = new PhoneNumber(); $number->setCountryCode(7)->setNationalNumber(87945154); $this->assertEquals(array(self::MOSCOW_TZ), self::$prefixTimeZonesMapForRU->lookupTimeZonesForNumber($number)); $number->setNationalNumber(421548578); $this->assertEquals(array(self::VLADIVOSTOK_TZ), self::$prefixTimeZonesMapForRU->lookupTimeZonesForNumber($number)); $number->setNationalNumber(342457897); $this->assertEquals(array(self::YEKATERINBURG_TZ), self::$prefixTimeZonesMapForRU->lookupTimeZonesForNumber($number)); // A mobile number $number->setNationalNumber(9342457897); $this->assertEquals(array(), self::$prefixTimeZonesMapForRU->lookupTimeZonesForNumber($number)); // An invalid number (too short) $number->setNationalNumber(3951); $this->assertEquals(array(self::IRKUTSK_TZ), self::$prefixTimeZonesMapForRU->lookupTimeZonesForNumber($number)); }
public function send(Message $message, PhoneNumber $recipient) { $number = $recipient->getCountryCode() . $recipient->getNationalNumber(); $text = $message->getContent(); try { $maxLen = $message->getType() === Message::TYPE_ASCII ? self::MAX_LENGTH_ASCII : self::MAX_LENGTH_UNICODE; $textPreparedForSms = $this->transport->prepareTextForMessage($text); if (mb_strlen($textPreparedForSms) > $maxLen) { $text = $this->transport->splitMessage($textPreparedForSms, $maxLen); $text = array_map($this->transport, $text); } if (false === is_array($text)) { $text = array($text); } foreach ($text as $contentPart) { $this->sender->sendMessage($number, $contentPart, $this->getTypeForMessage($message)); } return true; } catch (MobitexException $e) { throw new SenderException($e->getMessage(), $e->getCode(), $e); } }
/** * Returns the list of time zones {@code number}'s calling country code corresponds to. * * @param $number PhoneNumber the phone number to look up * @return array the list of corresponding time zones */ public function lookupCountryLevelTimeZonesForNumber(PhoneNumber $number) { return $this->lookupTimeZonesForNumberKey($number->getCountryCode()); }
public function testEmergencyNumberForSharedCountryCallingCode() { // Test the emergency number 112, which is valid in both Australia and the Christmas Islands. $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::AU)); $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse("112", RegionCode::AU), RegionCode::AU)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCostForRegion($this->parse("112", RegionCode::AU), RegionCode::AU)); $this->assertTrue($this->shortInfo->isEmergencyNumber("112", RegionCode::CX)); $this->assertTrue($this->shortInfo->isValidShortNumberForRegion($this->parse("112", RegionCode::CX), RegionCode::CX)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCostForRegion($this->parse("112", RegionCode::CX), RegionCode::CX)); $sharedEmergencyNumber = new PhoneNumber(); $sharedEmergencyNumber->setCountryCode(61)->setNationalNumber(112); $this->assertTrue($this->shortInfo->isValidShortNumber($sharedEmergencyNumber)); $this->assertEquals(ShortNumberCost::TOLL_FREE, $this->shortInfo->getExpectedCost($sharedEmergencyNumber)); }
public function testSpecialCaseIndianMobile() { $fixedLineAndMobileNumber = new PhoneNumber(); $fixedLineAndMobileNumber->setCountryCode(91)->setNationalNumber(7290412345); $this->assertEquals(PhoneNumberType::FIXED_LINE_OR_MOBILE, $this->phoneUtil->getNumberType($fixedLineAndMobileNumber)); }
public function testIsNumberMatchShortNsnMatches() { // Short NSN matches with the country not specified for either one or both numbers. $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("+64 3 331-6005", "331 6005")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("+64 3 331-6005", "tel:331-6005;phone-context=abc.nz")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("+64 3 331-6005", "tel:331-6005;isub=1234;phone-context=abc.nz")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("+64 3 331-6005", "tel:331-6005;isub=1234;phone-context=abc.nz;a=%A1")); // We did not know that the "0" was a national prefix since neither number has a country code, // so this is considered a SHORT_NSN_MATCH. $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("3 331-6005", "03 331 6005")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("3 331-6005", "331 6005")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("3 331-6005", "tel:331-6005;phone-context=abc.nz")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("3 331-6005", "+64 331 6005")); // Short NSN match with the country specified. $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("03 331-6005", "331 6005")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("1 234 345 6789", "345 6789")); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("+1 (234) 345 6789", "345 6789")); // NSN matches, country calling code omitted for one number, extension missing for one. $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch("+64 3 331-6005", "3 331 6005#1234")); // One has Italian leading zero, one does not. $italianNumberOne = new PhoneNumber(); $italianNumberOne->setCountryCode(39)->setNationalNumber(1234)->setItalianLeadingZero(true); $italianNumberTwo = new PhoneNumber(); $italianNumberTwo->setCountryCode(39)->setNationalNumber(1234); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch($italianNumberOne, $italianNumberTwo)); // One has an extension, the other has an extension of "". $italianNumberOne->setExtension("1234")->clearItalianLeadingZero(); $italianNumberTwo->setExtension(""); $this->assertEquals(MatchType::SHORT_NSN_MATCH, $this->phoneUtil->isNumberMatch($italianNumberOne, $italianNumberTwo)); }
/** * Returns a text description for the given phone number, in the language provided. The * description might consist of the name of the country where the phone number is from, or the * name of the geographical area the phone number is from if more detailed information is * available. * * <p>This method assumes the validity of the number passed in has already been checked, and that * the number is suitable for geocoding. We consider fixed-line and mobile numbers possible * candidates for geocoding. * * <p>If $userRegion is set, we also consider the region of the user. If the phone number is from * the same region as the user, only a lower-level description will be returned, if one exists. * Otherwise, the phone number's region will be returned, with optionally some more detailed * information. * * <p>For example, for a user from the region "US" (United States), we would show "Mountain View, * CA" for a particular number, omitting the United States from the description. For a user from * the United Kingdom (region "GB"), for the same number we may show "Mountain View, CA, United * States" or even just "United States". * * @param PhoneNumber $number a valid phone number for which we want to get a text description * @param string $locale the language code for which the description should be written * @param string $userRegion the region code for a given user. This region will be omitted from the * description if the phone number comes from this region. It is a two-letter uppercase ISO * country code as defined by ISO 3166-1. * @return string a text description for the given language code for the given phone number */ public function getDescriptionForValidNumber(PhoneNumber $number, $locale, $userRegion = null) { // If the user region matches the number's region, then we just show the lower-level // description, if one exists - if no description exists, we will show the region(country) name // for the number. $regionCode = $this->phoneUtil->getRegionCodeForNumber($number); if ($userRegion == null || $userRegion == $regionCode) { $languageStr = Locale::getPrimaryLanguage($locale); $scriptStr = ""; $regionStr = Locale::getRegion($locale); $mobileToken = $this->phoneUtil->getCountryMobileToken($number->getCountryCode()); $nationalNumber = $this->phoneUtil->getNationalSignificantNumber($number); if ($mobileToken !== "" && !strncmp($nationalNumber, $mobileToken, strlen($mobileToken))) { // In some countries, eg. Argentina, mobile numbers have a mobile token before the national // destination code, this should be removed before geocoding. $nationalNumber = substr($nationalNumber, strlen($mobileToken)); $copiedNumber = new PhoneNumber(); $copiedNumber->setCountryCode($number->getCountryCode()); $copiedNumber->setNationalNumber($nationalNumber); if (substr($nationalNumber, 0, 1) == "0") { $copiedNumber->setItalianLeadingZero(true); } $areaDescription = $this->prefixFileReader->getDescriptionForNumber($copiedNumber, $languageStr, $scriptStr, $regionStr); } else { $areaDescription = $this->prefixFileReader->getDescriptionForNumber($number, $languageStr, $scriptStr, $regionStr); } return strlen($areaDescription) > 0 ? $areaDescription : $this->getCountryNameForNumber($number, $locale); } // Otherwise, we just show the region(country) name for now. return $this->getRegionDisplayName($regionCode, $locale); // TODO: Concatenate the lower-level and country-name information in an appropriate // way for each language. }
/** * Returns the customary display name in the given language for the given territory the phone * number is from. If it could be from many territories, nothing is returned. * * @param PhoneNumber $number * @param string $locale * @return string */ protected function getCountryNameForNumber(PhoneNumber $number, $locale) { $regionCodes = $this->phoneUtil->getRegionCodesForCountryCode($number->getCountryCode()); if (count($regionCodes) === 1) { return $this->getRegionDisplayName($regionCodes[0], $locale); } else { $regionWhereNumberIsValid = 'ZZ'; foreach ($regionCodes as $regionCode) { if ($this->phoneUtil->isValidNumberForRegion($number, $regionCode)) { if ($regionWhereNumberIsValid !== 'ZZ') { // If we can't assign the phone number as definitely belonging to only one territory, // then we return nothing. return ""; } $regionWhereNumberIsValid = $regionCode; } } return $this->getRegionDisplayName($regionWhereNumberIsValid, $locale); } }
/** * Returns the description of the {@code $number}. This method distinguishes the case of an invalid * prefix and a prefix for which the name is not available in the current language. If the * description is not available in the current language an empty string is returned. If no * description was found for the provided number, null is returned. * * @param PhoneNumber $number The phone number to look up * @return string|null the description of the number */ public function lookup(PhoneNumber $number) { $phonePrefix = $number->getCountryCode() . $this->phoneUtil->getNationalSignificantNumber($number); return $this->lookupKey($phonePrefix); }
/** * {@inheritdoc} */ public function __toString() { return parent::__toString(); }
/** * Returns a text description in the given language for the given phone number. * * @param PhoneNumber $number the phone number for which we want to get a text description * @param string $language two-letter lowercase ISO language codes as defined by ISO 639-1 * @param string $script four-letter titlecase (the first letter is uppercase and the rest of the letters * are lowercase) ISO script codes as defined in ISO 15924 * @param string $region two-letter uppercase ISO country codes as defined by ISO 3166-1 * @return string a text description in the given language for the given phone number, or an empty * string if a description is not available */ public function getDescriptionForNumber(PhoneNumber $number, $language, $script, $region) { $phonePrefix = $number->getCountryCode() . PhoneNumberUtil::getInstance()->getNationalSignificantNumber($number); $phonePrefixDescriptions = $this->getPhonePrefixDescriptions($phonePrefix, $language, $script, $region); $description = $phonePrefixDescriptions !== null ? $phonePrefixDescriptions->lookup($number) : null; // When a location is not available in the requested language, fall back to English. if (($description === null || strlen($description) === 0) && $this->mayFallBackToEnglish($language)) { $defaultMap = $this->getPhonePrefixDescriptions($phonePrefix, "en", "", ""); if ($defaultMap === null) { return ""; } $description = $defaultMap->lookup($number); } return $description !== null ? $description : ""; }
/** * Returns a text description in the given language for the given phone number. * * @param PhoneNumber $number the phone number for which we want to get a text description * @param string $language two-letter lowercase ISO language codes as defined by ISO 639-1 * @param string $script four-letter titlecase (the first letter is uppercase and the rest of the letters * are lowercase) ISO script codes as defined in ISO 15924 * @param string $region two-letter uppercase ISO country codes as defined by ISO 3166-1 * @return string a text description in the given language for the given phone number, or an empty * string if a description is not available */ public function getDescriptionForNumber(PhoneNumber $number, $language, $script, $region) { $countryCallingCode = $number->getCountryCode(); // As the NANPA data is split into multiple files covering 3-digit areas, use a phone number // prefix of 4 digits for NANPA instead, e.g. 1650. $phonePrefix = $countryCallingCode !== 1 ? $countryCallingCode : 1000 + intval($number->getNationalNumber() / 10000000); $phonePrefixDescriptions = $this->getPhonePrefixDescriptions($phonePrefix, $language, $script, $region); $description = $phonePrefixDescriptions !== null ? $phonePrefixDescriptions->lookup($number) : null; // When a location is not available in the requested language, fall back to English. if (($description === null || strlen($description) === 0) && $this->mayFallBackToEnglish($language)) { $defaultMap = $this->getPhonePrefixDescriptions($phonePrefix, "en", "", ""); if ($defaultMap === null) { return ""; } $description = $defaultMap->lookup($number); } return $description !== null ? $description : ""; }
public function testParseNationalNumber() { // National prefix attached. $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("033316005", RegionCode::NZ)); $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("33316005", RegionCode::NZ)); // National prefix attached and some formatting present. $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("03-331 6005", RegionCode::NZ)); $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("03 331 6005", RegionCode::NZ)); // Testing international prefixes. // Should strip country calling code. $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("0064 3 331 6005", RegionCode::NZ)); // Try again, but this time we have an international number with Region Code US. It should // recognise the country calling code and parse accordingly. $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("01164 3 331 6005", RegionCode::US)); $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("+64 3 331 6005", RegionCode::US)); // We should ignore the leading plus here, since it is not followed by a valid country code but // instead is followed by the IDD for the US. $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("+01164 3 331 6005", RegionCode::US)); $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("+0064 3 331 6005", RegionCode::NZ)); $this->assertEquals(self::$nzNumber, $this->phoneUtil->parse("+ 00 64 3 331 6005", RegionCode::NZ)); $nzNumber = new PhoneNumber(); $nzNumber->setCountryCode(64)->setNationalNumber(64123456); $this->assertEquals($nzNumber, $this->phoneUtil->parse("64(0)64123456", RegionCode::NZ)); // Check that using a "/" is fine in a phone number. $this->assertEquals(self::$deNumber, $this->phoneUtil->parse("301/23456", RegionCode::DE)); $usNumber = new PhoneNumber(); // Check it doesn't use the '1' as a country calling code when parsing if the phone number was // already possible. $usNumber->setCountryCode(1)->setNationalNumber(1234567890); $this->assertEquals($usNumber, $this->phoneUtil->parse("123-456-7890", RegionCode::US)); // Test star numbers. Although this is not strictly valid, we would like to make sure we can // parse the output we produce when formatting the number. $this->assertEquals(self::$jpStarNumber, $this->phoneUtil->parse("+81 *2345", RegionCode::JP)); // Test national number bigger than max 32-bit signed integer $inNumber = new PhoneNumber(); $inNumber->setCountryCode(55)->setNationalNumber(9876543210); $this->assertEquals($inNumber, $this->phoneUtil->parse("9876543210", RegionCode::BR)); }
/** * Returns the national number of this PhoneNumber. * * The national number is a series of digits. * * @return string */ public function getNationalNumber() { return $this->phoneNumber->getNationalNumber(); }
/** * Returns a text description in the given language for the given phone number. * * @param PhoneNumber $number the phone number for which we want to get a text description * @param string $language two-letter lowercase ISO language codes as defined by ISO 639-1 * @param string $script four-letter titlecase (the first letter is uppercase and the rest of the letters * are lowercase) ISO script codes as defined in ISO 15924 * @param string $region two-letter uppercase ISO country codes as defined by ISO 3166-1 * @return string a text description in the given language for the given phone number, or an empty * string if a description is not available */ public function getDescriptionForNumber(PhoneNumber $number, $language, $script, $region) { $countryCallingCode = $number->getCountryCode(); // As the NANPA data is split into multiple files covering 3-digit areas, use a phone number // prefix of 4 digits for NANPA instead, e.g. 1650. if ($countryCallingCode === 1) { $phonePrefix = 1000 + intval($number->getNationalNumber() / 10000000); } elseif ($countryCallingCode === 86) { // Split China into multiple files to reduce PHP memory usage // @see https://github.com/giggsey/libphonenumber-for-php/issues/44 $phonePrefix = '86' . substr($number->getNationalNumber(), 0, 3); } else { $phonePrefix = $countryCallingCode; } $phonePrefixDescriptions = $this->getPhonePrefixDescriptions($phonePrefix, $language, $script, $region); $description = $phonePrefixDescriptions !== null ? $phonePrefixDescriptions->lookup($number) : null; // When a location is not available in the requested language, fall back to English. if (($description === null || strlen($description) === 0) && $this->mayFallBackToEnglish($language)) { $defaultMap = $this->getPhonePrefixDescriptions($phonePrefix, "en", "", ""); if ($defaultMap === null) { return ""; } $description = $defaultMap->lookup($number); } return $description !== null ? $description : ""; }
/** * @return int */ public function getCountryCode() : int { return $this->number->getCountryCode(); }
/** * Attempts to extract a valid number from a phone number that is too long to be valid, and resets * the PhoneNumber object passed in to that valid version. If no valid number could be extracted, * the PhoneNumber object passed in will not be modified. * @param $number PhoneNumber a PhoneNumber object which contains a number that is too long to be valid. * @return boolean true if a valid phone number can be successfully extracted. */ public function truncateTooLongNumber(PhoneNumber $number) { if ($this->isValidNumber($number)) { return true; } $numberCopy = new PhoneNumber(); $numberCopy->mergeFrom($number); $nationalNumber = $number->getNationalNumber(); do { $nationalNumber = floor($nationalNumber / 10); $numberCopy->setNationalNumber($nationalNumber); if ($this->isPossibleNumberWithReason($numberCopy) == ValidationResult::TOO_SHORT || $nationalNumber == 0) { return false; } } while (!$this->isValidNumber($numberCopy)); $number->setNationalNumber($nationalNumber); return true; }