/** * @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 $regionCode * @return $this */ public function setRegionCode($regionCode) { $regionCode = (string) $regionCode; if (!in_array($regionCode, $this->libPhoneNumber->getSupportedRegions())) { throw new InvalidArgumentException('Region code "' . $regionCode . '" is not currently supported by libPhoneNumber.'); } $this->regionCode = $regionCode; return $this; }
/** * @param string $country * * @return string * * @throws Exceptions\NoValidCountryException */ protected function validateCountry($country) { // Country code have to be upper-cased $country = strtoupper($country); // Correct auto or null value if ($country === 'AUTO' || $country === NULL) { return 'AUTO'; } else { if (strlen($country) === 2 && ctype_alpha($country) && in_array($country, $this->phoneNumberUtil->getSupportedRegions())) { return $country; } else { throw new Exceptions\NoValidCountryException('Provided country code "' . $country . '" is not valid. Provide valid country code or AUTO for automatic detection.'); } } }
public function testGetSupportedRegions() { $this->assertGreaterThan(0, count($this->phoneUtil->getSupportedRegions())); }
public function isValid($value) { if (!is_scalar($value)) { $this->error(self::INVALID); return false; } $country = $this->getCountry(); $supportedCountries = $this->libPhoneNumber->getSupportedRegions(); if (!in_array($country, $supportedCountries)) { $this->error(self::UNSUPPORTED); return false; } try { $numberProto = $this->libPhoneNumber->parse($value, $country); } catch (NumberParseException $e) { $this->error(self::INVALID_NUMBER); return false; } if (!$this->libPhoneNumber->isValidNumber($numberProto)) { $this->error(self::INVALID_NUMBER); return false; } $region = $this->libPhoneNumber->getRegionCodeForNumber($numberProto); if ($this->libPhoneNumber->isValidNumberForRegion($numberProto, $region)) { return true; } $this->error(self::NO_MATCH); return false; }