Returns the country calling code for a specific region. For example, this would be 1 for the
United States, and 64 for New Zealand. Assumes the region is already valid.
public getCountryCodeForRegion ( string $regionCode ) : integer | ||
$regionCode | string | the region that we want to get the country calling code for |
return | integer | the country calling code for the region denoted by regionCode |
public function testGetCountryCodeForRegion() { $this->assertEquals(1, $this->phoneUtil->getCountryCodeForRegion(RegionCode::US)); $this->assertEquals(64, $this->phoneUtil->getCountryCodeForRegion(RegionCode::NZ)); $this->assertEquals(0, $this->phoneUtil->getCountryCodeForRegion(null)); $this->assertEquals(0, $this->phoneUtil->getCountryCodeForRegion(RegionCode::ZZ)); $this->assertEquals(0, $this->phoneUtil->getCountryCodeForRegion(RegionCode::UN001)); // CS is already deprecated so the library doesn't support it $this->assertEquals(0, $this->phoneUtil->getCountryCodeForRegion(RegionCode::CS)); }
/** * Get dialing country code for provided country * * @param string $country * * @return int * * @throws Exceptions\NoValidCountryException */ public function getCountryCodeForCountry($country) { // Check if country is valid $country = $this->validateCountry($country); // Transform country to country code $code = $this->phoneNumberUtil->getCountryCodeForRegion($country); if ($code !== 0) { return $code; } else { throw new Exceptions\NoValidCountryException('Provided country code "' . $country . '" is not valid. Provide valid country code.'); } }