mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-09 19:51:42 +09:00
Add functions for converting between country codes and calling codes
This commit is contained in:
parent
3e0181a3c5
commit
342c011a6c
2 changed files with 137 additions and 0 deletions
|
|
@ -18,6 +18,11 @@ class i18n
|
|||
const SORT_NAME_KOREAN = 7;
|
||||
const SORT_NAME_NATIVE = 8;
|
||||
|
||||
/**
|
||||
* Local cache.
|
||||
*/
|
||||
protected static $_countries = array();
|
||||
|
||||
/**
|
||||
* Get the list of all countries.
|
||||
*
|
||||
|
|
@ -26,6 +31,11 @@ class i18n
|
|||
*/
|
||||
public static function listCountries($sort_by = self::SORT_NAME_ENGLISH)
|
||||
{
|
||||
if (isset(self::$_countries[$sort_by]))
|
||||
{
|
||||
return self::$_countries[$sort_by];
|
||||
}
|
||||
|
||||
$countries = (include \RX_BASEDIR . 'common/defaults/countries.php');
|
||||
$result = array();
|
||||
|
||||
|
|
@ -73,6 +83,101 @@ class i18n
|
|||
break;
|
||||
}
|
||||
|
||||
self::$_countries[$sort_by] = $result;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the calling code from a country code (either ISO-3166-1 alpha2 or alpha3).
|
||||
*
|
||||
* This function returns null if a matching country is not found.
|
||||
*
|
||||
* @param $code Country code
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getCallingCodeByCountryCode($code)
|
||||
{
|
||||
$countries = self::listCountries();
|
||||
if (strlen($code) === 3)
|
||||
{
|
||||
return (isset($countries[$code]) && $countries[$code]->calling_code) ? $countries[$code]->calling_code : null;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($countries as $country)
|
||||
{
|
||||
if ($country->iso_3166_1_alpha2 === $code)
|
||||
{
|
||||
return $country->calling_code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the country code (either ISO-3166-1 alpha2 or alpha3) from a calling code.
|
||||
*
|
||||
* This function may return the wrong country if two or more countries share a calling code.
|
||||
* This function returns null if a matching country is not found.
|
||||
*
|
||||
* @param $code Calling code
|
||||
* @return string|null
|
||||
*/
|
||||
public static function getCountryCodeByCallingCode($code, $type = 3)
|
||||
{
|
||||
$countries = self::listCountries();
|
||||
$code = preg_replace('/[^0-9]/', '', $code);
|
||||
foreach ($countries as $country)
|
||||
{
|
||||
if (preg_replace('/[^0-9]/', '', $country->calling_code) === $code)
|
||||
{
|
||||
return $type == 3 ? $country->iso_3166_1_alpha3 : $country->iso_3166_1_alpha2;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* Format a phone number with country code.
|
||||
*
|
||||
* @param string $phone_number
|
||||
* @param string $phone_country
|
||||
* @param bool $pretty (optional)
|
||||
* @return string
|
||||
*/
|
||||
public static function formatPhoneNumber($phone_number, $phone_country, $pretty = true)
|
||||
{
|
||||
if (!is_numeric($phone_country))
|
||||
{
|
||||
$phone_country = self::getCallingCodeByCountryCode($phone_country);
|
||||
}
|
||||
|
||||
if ($pretty)
|
||||
{
|
||||
if ($phone_country == 82)
|
||||
{
|
||||
$pretty_phone_number = Korea::formatPhoneNumber($phone_number);
|
||||
}
|
||||
elseif ($phone_country == 1)
|
||||
{
|
||||
$digits = preg_replace('/[^0-9]/', '', $phone_number);
|
||||
$pretty_phone_number = substr($digits, 0, 3) . '-' . substr($digits, 3, 3) . '-' . substr($digits, 6);
|
||||
}
|
||||
else
|
||||
{
|
||||
$pretty_phone_number = preg_replace('/[^0-9-]/', '', $phone_number);
|
||||
}
|
||||
return sprintf('(+%s) %s', $phone_country, $pretty_phone_number);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!in_array(strval($phone_country), array('39', '378', '379')))
|
||||
{
|
||||
$phone_number = preg_replace('/^0/', '', $phone_number);
|
||||
}
|
||||
return sprintf('+%s', preg_replace('/[^0-9]/', '', $phone_country . $phone_number));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue