Skip to content

Commit

Permalink
Merge 68ad17e into e093186
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 committed Oct 4, 2018
2 parents e093186 + 68ad17e commit 2e28489
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 10 deletions.
48 changes: 48 additions & 0 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,54 @@ public static function getSymbol($currencyCode, $which = '', $locale = '')
return $result;
}

/**
* Returns the ISO 4217 code for a currency given its code.
*
* Historical currencies are not supported.
*
* @param string $currencyCode The currency code
*
* @return string Returns the ISO 427 code, or an empty string if $currencyCode is not valid
*
* @see http://unicode.org/reports/tr35/tr35-info.html#Supplemental_Code_Mapping
*/
public static function getCode($currencyCode)
{
$codeMappings = Data::getGeneric('codeMappings');
$currencies = $codeMappings['currencies'];

if (isset($currencies[$currencyCode]['numeric'])) {
return $currencies[$currencyCode]['numeric'];
}

return '';
}

/**
* Returns the currency code given its ISO 4217 code.
*
* Historical currencies are not supported.
*
* @param string $code The ISO 427 code
*
* @return string Returns the currency code, or an empty string if $code is not valid
*
* @see http://unicode.org/reports/tr35/tr35-info.html#Supplemental_Code_Mapping
*/
public static function getByCode($code)
{
$codeMappings = Data::getGeneric('codeMappings');
$currencies = $codeMappings['currencies'];

foreach ($currencies as $currencyCode => $currency) {
if (isset($currency['numeric']) && $currency['numeric'] == $code) {
return $currencyCode;
}
}

return '';
}

/**
* Return the history for the currencies used in a territory.
*
Expand Down
73 changes: 73 additions & 0 deletions src/Territory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
class Territory
{
private static $CODE_TYPES = array('alpha3', 'numeric', 'fips10', 'internet');

/**
* Retrieve the name of a territory/subdivision (country, continent, ...).
*
Expand Down Expand Up @@ -34,6 +36,77 @@ public static function getName($territoryCode, $locale = '')
return $result;
}

/**
* Retrieve the code of a territory in a different coding system.
*
* @param string $territoryCode The territory code
* @param string $type The type of code to return. "alpha3" for ISO 3166-1 alpha-3 codes, "numeric" for UN M.49, "fips10" for FIPS 10 codes or "internet" for top-level domains
*
* @throws \Punic\Exception\ValueNotInList throws a ValueNotInList exception if $type is not valid
*
* @return string|array returns the code for the specified territory, or an array of codes when $type is "internet", or an empty string if the code is not defined for the territory or the territory is unknown
*
* @see http://unicode.org/reports/tr35/tr35-info.html#Supplemental_Code_Mapping
*/
public static function getCode($territoryCode, $type)
{
$codeMappings = Data::getGeneric('codeMappings');
$territories = $codeMappings['territories'];

if (!in_array($type, static::$CODE_TYPES)) {
throw new Exception\ValueNotInList($type, static::$CODE_TYPES);
}

if (!isset($territories[$territoryCode])) {
$result = '';
} elseif (isset($territories[$territoryCode][$type])) {
$result = $territories[$territoryCode][$type];
} elseif ($type === 'internet') {
$result = array($territoryCode);
} elseif ($type !== 'numeric' && $type !== 'alpha3') {
$result = $territoryCode;
} else {
$result = '';
}

return $result;
}

/**
* Retrieve the territory code given its code in a different coding system.
*
* @param string $code The code
* @param string $type The type of code provided. "alpha3" for ISO 3166-1 alpha-3 codes, "numeric" for UN M.49, "fips10" for FIPS 10 codes or "internet" for top-level domains
*
* @throws \Punic\Exception\ValueNotInList throws a ValueNotInList exception if $type is not valid
*
* @return string returns the code for the specified territory, or null if the code is unknown
*
* @see http://unicode.org/reports/tr35/tr35-info.html#Supplemental_Code_Mapping
*/
public static function getByCode($code, $type)
{
$codeMappings = Data::getGeneric('codeMappings');
$territories = $codeMappings['territories'];

if (!in_array($type, static::$CODE_TYPES)) {
throw new Exception\ValueNotInList($type, static::$CODE_TYPES);
}

foreach ($territories as $territoryCode => $territory) {
$c = isset($territory[$type]) ? $territory[$type] : $territoryCode;
if (is_array($c)) {
if (in_array($code, $c)) {
return $territoryCode;
}
} elseif ($code == $c) {
return $territoryCode;
}
}

return null;
}

/**
* Return the list of continents in the form of an array with key=ID, value=name.
*
Expand Down
29 changes: 19 additions & 10 deletions tests/Currency/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public function testAvailability()
public function providerGetInfo()
{
return array(
array('en', 'USD', null, 'US Dollar', '$', '$', ''),
array('it', 'USD', null, 'dollaro statunitense', 'USD', '$', ''),
array('en', 'Invalid currency code', null, '', '', '', ''),
array('de', 'ARS', null, 'Argentinischer Peso', 'ARS', '$', ''),
array('en', 'USD', 0, 'US dollars', '$', '$', ''),
array('en', 'USD', 1, 'US dollar', '$', '$', ''),
array('en', 'USD', 2, 'US dollars', '$', '$', ''),
array('en', 'USD', 'one', 'US dollar', '$', '$', ''),
array('en', 'USD', 'many', 'US dollars', '$', '$', ''),
array('en', 'USD', null, 'US Dollar', '$', '$', '', 840),
array('it', 'USD', null, 'dollaro statunitense', 'USD', '$', '', 840),
array('en', 'Invalid currency code', null, '', '', '', '', ''),
array('de', 'ARS', null, 'Argentinischer Peso', 'ARS', '$', '', 32),
array('en', 'USD', 0, 'US dollars', '$', '$', '', 840),
array('en', 'USD', 1, 'US dollar', '$', '$', '', 840),
array('en', 'USD', 2, 'US dollars', '$', '$', '', 840),
array('en', 'USD', 'one', 'US dollar', '$', '$', '', 840),
array('en', 'USD', 'many', 'US dollars', '$', '$', '', 840),
);
}

Expand All @@ -45,13 +45,22 @@ public function providerGetInfo()
* @param string $currencySymbol
* @param string $currencySymbolNarrow
* @param string $currencySymbolAlternative
* @param mixed $iso4217
*/
public function testGetInfo($locale, $currencyCode, $quantity, $currencyName, $currencySymbol, $currencySymbolNarrow, $currencySymbolAlternative)
public function testGetInfo($locale, $currencyCode, $quantity, $currencyName, $currencySymbol, $currencySymbolNarrow, $currencySymbolAlternative, $iso4217)
{
$this->assertSame($currencyName, \Punic\Currency::getName($currencyCode, $quantity, $locale), 'Error getting name');
$this->assertSame($currencySymbol, \Punic\Currency::getSymbol($currencyCode, '', $locale), 'Error getting standard symbol');
$this->assertSame($currencySymbolNarrow, \Punic\Currency::getSymbol($currencyCode, 'narrow', $locale), 'Error getting narrow symbol');
$this->assertSame($currencySymbolAlternative, \Punic\Currency::getSymbol($currencyCode, 'alt', $locale), 'Error getting alternative symbol');
$this->assertSame($iso4217, \Punic\Currency::getCode($currencyCode), 'Error getting code');
}

public function testGetByCode()
{
$this->assertSame('DKK', \Punic\Currency::getByCode(208));
$this->assertSame('DKK', \Punic\Currency::getByCode('208'));
$this->assertSame('', \Punic\Currency::getByCode(666));
}

/**
Expand Down
85 changes: 85 additions & 0 deletions tests/Territory/TerritoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,91 @@ public function testGetName($result, $territoryCode, $forLocale)
);
}

/**
* @return array
*/
public function providerGetCode()
{
return array(
array('USA', 'US', 'alpha3'),
array('840', 'US', 'numeric'),
array('US', 'US', 'fips10'),
array('AU', 'AT', 'fips10'),
array('DGA', 'DG', 'alpha3'),
array('', 'EA', 'alpha3'),
array('', 'DG', 'numeric'),
array(array('US'), 'US', 'internet'),
array(array('UK', 'GB'), 'GB', 'internet'),
array('', 'FOO', 'alpha3'),
);
}

/**
* test getCode.
*
* @dataProvider providerGetCode
*
* @param string $result
* @param string $territoryCode
* @param string $type
*/
public function testGetCode($result, $territoryCode, $type)
{
$this->assertSame(
$result,
Territory::getCode($territoryCode, $type)
);
}

public function testGetCodeException()
{
$this->setExpectedException('Punic\\Exception\\ValueNotInList');
Territory::getCode('DE', 'foo');
}

/**
* @return array
*/
public function providerGetByCode()
{
return array(
array('US', 'USA', 'alpha3'),
array('US', '840', 'numeric'),
array('US', 840, 'numeric'),
array('US', 'US', 'fips10'),
array('AT', 'AU', 'fips10'),
array('DG', 'DGA', 'alpha3'),
array('US', 'US', 'internet'),
array('GB', 'UK', 'internet'),
array('GB', 'GB', 'internet'),
array('ZZ', 'COM', 'internet'),
);
}

/**
* test getByCode.
*
* @dataProvider providerGetByCode
*
* @param string $result
* @param string $territoryCode
* @param string $type
* @param mixed $code
*/
public function testGetByCode($result, $code, $type)
{
$this->assertSame(
$result,
Territory::getByCode($code, $type)
);
}

public function testGetByCodeException()
{
$this->setExpectedException('Punic\\Exception\\ValueNotInList');
Territory::getByCode('666', 'foo');
}

public function testCountries()
{
$countries = Territory::getCountries();
Expand Down

0 comments on commit 2e28489

Please sign in to comment.