Returns the country code of this phone number.
public getCountryCode ( ) : integer | null | ||
return | integer | null | The country code, or null if not set. |
/** * 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; }
/** * @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; }
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 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()); }
/** * 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); }
/** * 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 : ""; }
/** * 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. 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(); }
/** * Takes two phone numbers and compares them for equality. * * <p>Returns EXACT_MATCH if the country_code, NSN, presence of a leading zero * for Italian numbers and any extension present are the same. Returns NSN_MATCH * if either or both has no region specified, and the NSNs and extensions are * the same. Returns SHORT_NSN_MATCH if either or both has no region specified, * or the region specified is the same, and one NSN could be a shorter version * of the other number. This includes the case where one has an extension * specified, and the other does not. Returns NO_MATCH otherwise. For example, * the numbers +1 345 657 1234 and 657 1234 are a SHORT_NSN_MATCH. The numbers * +1 345 657 1234 and 345 657 are a NO_MATCH. * * @param $firstNumberIn PhoneNumber|string First number to compare. If it is a * string it can contain formatting, and can have country calling code specified * with + at the start. * @param $secondNumberIn PhoneNumber|string Second number to compare. If it is a * string it can contain formatting, and can have country calling code specified * with + at the start. * @throws \InvalidArgumentException * @return int {MatchType} NOT_A_NUMBER, NO_MATCH, */ public function isNumberMatch($firstNumberIn, $secondNumberIn) { if (is_string($firstNumberIn) && is_string($secondNumberIn)) { try { $firstNumberAsProto = $this->parse($firstNumberIn, self::UNKNOWN_REGION); return $this->isNumberMatch($firstNumberAsProto, $secondNumberIn); } catch (NumberParseException $e) { if ($e->getErrorType() === NumberParseException::INVALID_COUNTRY_CODE) { try { $secondNumberAsProto = $this->parse($secondNumberIn, self::UNKNOWN_REGION); return $this->isNumberMatch($secondNumberAsProto, $firstNumberIn); } catch (NumberParseException $e2) { if ($e2->getErrorType() === NumberParseException::INVALID_COUNTRY_CODE) { try { $firstNumberProto = new PhoneNumber(); $secondNumberProto = new PhoneNumber(); $this->parseHelper($firstNumberIn, null, false, false, $firstNumberProto); $this->parseHelper($secondNumberIn, null, false, false, $secondNumberProto); return $this->isNumberMatch($firstNumberProto, $secondNumberProto); } catch (NumberParseException $e3) { // Fall through and return MatchType::NOT_A_NUMBER } } } } } return MatchType::NOT_A_NUMBER; } if ($firstNumberIn instanceof PhoneNumber && is_string($secondNumberIn)) { // First see if the second number has an implicit country calling code, by attempting to parse // it. try { $secondNumberAsProto = $this->parse($secondNumberIn, self::UNKNOWN_REGION); return $this->isNumberMatch($firstNumberIn, $secondNumberAsProto); } catch (NumberParseException $e) { if ($e->getErrorType() === NumberParseException::INVALID_COUNTRY_CODE) { // The second number has no country calling code. EXACT_MATCH is no longer possible. // We parse it as if the region was the same as that for the first number, and if // EXACT_MATCH is returned, we replace this with NSN_MATCH. $firstNumberRegion = $this->getRegionCodeForCountryCode($firstNumberIn->getCountryCode()); try { if ($firstNumberRegion != self::UNKNOWN_REGION) { $secondNumberWithFirstNumberRegion = $this->parse($secondNumberIn, $firstNumberRegion); $match = $this->isNumberMatch($firstNumberIn, $secondNumberWithFirstNumberRegion); if ($match === MatchType::EXACT_MATCH) { return MatchType::NSN_MATCH; } return $match; } else { // If the first number didn't have a valid country calling code, then we parse the // second number without one as well. $secondNumberProto = new PhoneNumber(); $this->parseHelper($secondNumberIn, null, false, false, $secondNumberProto); return $this->isNumberMatch($firstNumberIn, $secondNumberProto); } } catch (NumberParseException $e2) { // Fall-through to return NOT_A_NUMBER. } } } } if ($firstNumberIn instanceof PhoneNumber && $secondNumberIn instanceof PhoneNumber) { // Make copies of the phone number so that the numbers passed in are not edited. $firstNumber = new PhoneNumber(); $firstNumber->mergeFrom($firstNumberIn); $secondNumber = new PhoneNumber(); $secondNumber->mergeFrom($secondNumberIn); // First clear raw_input, country_code_source and preferred_domestic_carrier_code fields and any // empty-string extensions so that we can use the proto-buffer equality method. $firstNumber->clearRawInput(); $firstNumber->clearCountryCodeSource(); $firstNumber->clearPreferredDomesticCarrierCode(); $secondNumber->clearRawInput(); $secondNumber->clearCountryCodeSource(); $secondNumber->clearPreferredDomesticCarrierCode(); if ($firstNumber->hasExtension() && mb_strlen($firstNumber->getExtension()) === 0) { $firstNumber->clearExtension(); } if ($secondNumber->hasExtension() && mb_strlen($secondNumber->getExtension()) === 0) { $secondNumber->clearExtension(); } // Early exit if both had extensions and these are different. if ($firstNumber->hasExtension() && $secondNumber->hasExtension() && $firstNumber->getExtension() != $secondNumber->getExtension()) { return MatchType::NO_MATCH; } $firstNumberCountryCode = $firstNumber->getCountryCode(); $secondNumberCountryCode = $secondNumber->getCountryCode(); // Both had country_code specified. if ($firstNumberCountryCode != 0 && $secondNumberCountryCode != 0) { if ($firstNumber->equals($secondNumber)) { return MatchType::EXACT_MATCH; } elseif ($firstNumberCountryCode == $secondNumberCountryCode && $this->isNationalNumberSuffixOfTheOther($firstNumber, $secondNumber)) { // A SHORT_NSN_MATCH occurs if there is a difference because of the presence or absence of // an 'Italian leading zero', the presence or absence of an extension, or one NSN being a // shorter variant of the other. return MatchType::SHORT_NSN_MATCH; } // This is not a match. return MatchType::NO_MATCH; } // Checks cases where one or both country_code fields were not specified. To make equality // checks easier, we first set the country_code fields to be equal. $firstNumber->setCountryCode($secondNumberCountryCode); // If all else was the same, then this is an NSN_MATCH. if ($firstNumber->equals($secondNumber)) { return MatchType::NSN_MATCH; } if ($this->isNationalNumberSuffixOfTheOther($firstNumber, $secondNumber)) { return MatchType::SHORT_NSN_MATCH; } return MatchType::NO_MATCH; } return MatchType::NOT_A_NUMBER; }
/** * Returns the country code of this PhoneNumber. * * The country code is a series of 1 to 3 digits, as defined per the E.164 recommendation. * * @return string */ public function getCountryCode() { return (string) $this->phoneNumber->getCountryCode(); }