Gets the type of a phone number.
public getNumberType ( |
||
$number | the number the phone number that we want to know the type | |
return | integer | PhoneNumberType the type of the phone number |
public function testKWMobileNumber() { $number = "51440519"; $phoneNumber = $this->phoneUtil->parse($number, "KW"); $this->assertTrue($this->phoneUtil->isValidNumber($phoneNumber)); $this->assertEquals(PhoneNumberType::MOBILE, $this->phoneUtil->getNumberType($phoneNumber)); }
/** * Gets the name of the carrier for the given phone number, in the language provided. As per * {@link #getNameForValidNumber(PhoneNumber, Locale)} but explicitly checks the validity of * the number passed in. * * @param PhoneNumber $number The phone number for which we want to get a carrier name * @param string $languageCode Language code for which the description should be written * @return string a carrier name for the given phone number, or empty string if the number passed in is * invalid */ public function getNameForNumber(PhoneNumber $number, $languageCode) { $numberType = $this->phoneUtil->getNumberType($number); if ($this->isMobile($numberType)) { return $this->getNameForValidNumber($number, $languageCode); } return ""; }
/** * As per {@link #getTimeZonesForGeographicalNumber(PhoneNumber)} but explicitly checks * the validity of the number passed in. * * @param $number PhoneNumber the phone number for which we want to get the time zones to which it belongs * @return array a list of the corresponding time zones or a single element list with the default * unknown time zone if no other time zone was found or if the number was invalid */ public function getTimeZonesForNumber(PhoneNumber $number) { $numberType = $this->phoneUtil->getNumberType($number); if ($numberType === PhoneNumberType::UNKNOWN) { return $this->unknownTimeZoneList; } elseif (!!PhoneNumberUtil::getInstance()->isNumberGeographical($numberType, $number->getCountryCode())) { return $this->getCountryLevelTimeZonesforNumber($number); } return $this->getTimeZonesForGeographicalNumber($number); }
/** * As per getDescriptionForValidNumber, but explicitly checks the validity of the number * passed in. * * * @see getDescriptionForValidNumber * @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, or empty * string if the number passed in is invalid */ public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null) { $numberType = $this->phoneUtil->getNumberType($number); if ($numberType === PhoneNumberType::UNKNOWN) { return ""; } elseif (!$this->phoneUtil->isNumberGeographical($numberType, $number->getCountryCode())) { return $this->getCountryNameForNumber($number, $locale); } return $this->getDescriptionForValidNumber($number, $locale, $userRegion); }
private function checkNumbersValidAndCorrectType($exampleNumberRequestedType, $possibleExpectedTypes, $regionCode) { $exampleNumber = $this->phoneNumberUtil->getExampleNumberForType($regionCode, $exampleNumberRequestedType); if ($exampleNumber !== null) { $this->assertTrue($this->phoneNumberUtil->isValidNumber($exampleNumber), "Failed validation for {$exampleNumber}"); // We know the number is valid, now we check the type. $exampleNumberType = $this->phoneNumberUtil->getNumberType($exampleNumber); $this->assertContains($exampleNumberType, $possibleExpectedTypes, "Wrong type for {$exampleNumber}"); } }
/** * As per {@link #getTimeZonesForGeographicalNumber(PhoneNumber)} but explicitly checks * the validity of the number passed in. * * @param $number PhoneNumber the phone number for which we want to get the time zones to which it belongs * @return array a list of the corresponding time zones or a single element list with the default * unknown time zone if no other time zone was found or if the number was invalid */ public function getTimeZonesForNumber(PhoneNumber $number) { $numberType = $this->phoneUtil->getNumberType($number); if ($numberType === PhoneNumberType::UNKNOWN) { return $this->unknownTimeZoneList; } elseif (!$this->canBeGeocoded($numberType)) { return $this->getCountryLevelTimeZonesforNumber($number); } return $this->getTimeZonesForGeographicalNumber($number); }
/** * As per getDescriptionForValidNumber, but explicitly checks the validity of the number * passed in. * * * @see getDescriptionForValidNumber * @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, or empty * string if the number passed in is invalid */ public function getDescriptionForNumber(PhoneNumber $number, $locale, $userRegion = null) { /** @var PhoneNumberType $numberType */ $numberType = $this->phoneUtil->getNumberType($number); if ($numberType === PhoneNumberType::UNKNOWN) { return ""; } elseif (!$this->canBeGeocoded($numberType)) { return $this->getCountryNameForNumber($number, $locale); } return $this->getDescriptionForValidNumber($number, $locale, $userRegion); }
public function testIsFixedLineAndMobile() { $this->assertEquals(PhoneNumberType::FIXED_LINE_OR_MOBILE, $this->phoneUtil->getNumberType(self::$usNumber)); $fixedLineAndMobileNumber = new PhoneNumber(); $fixedLineAndMobileNumber->setCountryCode(54)->setNationalNumber(1987654321); $this->assertEquals(PhoneNumberType::FIXED_LINE_OR_MOBILE, $this->phoneUtil->getNumberType($fixedLineAndMobileNumber)); }
/** * Performs the actual validation of the phone number. * * @param mixed $number * @param null $country * @return bool */ protected function isValidNumber($number, $country = null) { try { // Throws NumberParseException if not parsed correctly against the supplied country. // If no country was given, tries to discover the country code from the number itself. $phoneNumber = $this->lib->parse($number, $country); // Lenient validation; doesn't need a country code. if ($this->lenient) { return $this->lib->isPossibleNumber($phoneNumber); } // For automatic detection, the number should have a country code. // Check if type is allowed. if ($phoneNumber->hasCountryCode() && (empty($this->types) || in_array($this->lib->getNumberType($phoneNumber), $this->types))) { // Automatic detection: if ($this->autodetect) { // Validate if the international phone number is valid for its contained country. return $this->lib->isValidNumber($phoneNumber); } // Validate number against the specified country. return $this->lib->isValidNumberForRegion($phoneNumber, $country); } } catch (NumberParseException $e) { // Proceed to default validation error. } return false; }
public function testCountryWithNoNumberDesc() { // Andorra is a country where we don't have PhoneNumberDesc info in the metadata. $adNumber = new PhoneNumber(); $adNumber->setCountryCode(376)->setNationalNumber(12345); $this->assertEquals("+376 12345", $this->phoneUtil->format($adNumber, PhoneNumberFormat::INTERNATIONAL)); $this->assertEquals("+37612345", $this->phoneUtil->format($adNumber, PhoneNumberFormat::E164)); $this->assertEquals("12345", $this->phoneUtil->format($adNumber, PhoneNumberFormat::NATIONAL)); $this->assertEquals(PhoneNumberType::UNKNOWN, $this->phoneUtil->getNumberType($adNumber)); $this->assertTrue($this->phoneUtil->isValidNumber($adNumber)); // Test dialing a US number from within Andorra. $this->assertEquals("00 1 650 253 0000", $this->phoneUtil->formatOutOfCountryCallingNumber(self::$usNumber, RegionCode::AD)); }
public function testChildLine() { $number = '08001111'; $phoneObject = $this->phoneUtil->parse($number, 'GB'); $valid = $this->phoneUtil->isValidNumber($phoneObject); $this->assertTrue($valid, "Checking phone number is valid"); $type = $this->phoneUtil->getNumberType($phoneObject); $this->assertEquals(PhoneNumberType::TOLL_FREE, $type, "Checking phone number is detected as TOLL FREE"); $formattedE164 = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::E164); $this->assertEquals("+448001111", $formattedE164, "Checking E164 format is correct"); $formattedNational = $this->phoneUtil->format($phoneObject, PhoneNumberFormat::NATIONAL); $this->assertEquals("0800 1111", $formattedNational, "Checking National format is correct"); }
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); } }
/** * @param string|Entities\IPhone $number * @param string $country * @param string|NULL $type * * @return bool * * @throws Exceptions\NoValidCountryException * @throws Exceptions\NoValidTypeException */ public function isValid($number, $country = 'AUTO', $type = NULL) { // Check if country is valid $country = $this->validateCountry($country); // Check if phone type is valid $type = $type !== NULL ? $this->validateType($type) : NULL; try { // Parse string into phone number $phoneNumber = $this->phoneNumberUtil->parse((string) $number, $country); if ($type !== NULL && $this->phoneNumberUtil->getNumberType($phoneNumber) !== $type) { return FALSE; } // Automatic detection: if ($country == 'AUTO') { // Validate if the international phone number is valid for its contained country return (bool) $this->phoneNumberUtil->isValidNumber($phoneNumber); } // Validate number against the specified country return (bool) $this->phoneNumberUtil->isValidNumberForRegion($phoneNumber, $country); } catch (libphonenumber\NumberParseException $ex) { return FALSE; } }
/** * @return string */ public function getType() : string { return $this->util->getNumberType($this->number); }