Skip to content

Commit

Permalink
Tweak currency loader to support both longlist & shortlist

Browse files Browse the repository at this point in the history
  • Loading branch information
Omranic committed Jan 18, 2020
1 parent d7237e9 commit 553ad84
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/CountryLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class CountryLoader
* @param string $code
* @param bool $hydrate
*
* @throws \Rinvex\Country\CountryLoaderException
*
* @return \Rinvex\Country\Country|array
*/
public static function country($code, $hydrate = true)
Expand All @@ -40,6 +42,8 @@ public static function country($code, $hydrate = true)
* @param bool $longlist
* @param bool $hydrate
*
* @throws \Rinvex\Country\CountryLoaderException
*
* @return array
*/
public static function countries($longlist = false, $hydrate = false)
Expand All @@ -62,6 +66,8 @@ public static function countries($longlist = false, $hydrate = false)
* @param mixed $operator
* @param mixed $value
*
* @throws \Rinvex\Country\CountryLoaderException
*
* @return array
*/
public static function where($key, $operator, $value = null)
Expand Down
23 changes: 14 additions & 9 deletions src/CurrencyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@

namespace Rinvex\Country;

use Rinvex\Country\CountryLoader;

class CurrencyLoader
{
protected static $curriencies = [];

/**
* Retrive all the curriencies of all countries
* @param boolean $longlist states if need all the details of the curriencies or only the keys
*
* @param boolean $longlist states if need all the details of the curriencies or only the keys
*
* @throws \Rinvex\Country\CountryLoaderException
*
* @return array
*/
public static function curriencies($longlist = false): array
{
$list = $longlist ? 'longlist' : 'shortlist';

if(!isset(static::$curriencies[$list])) {

$countries = CountryLoader::countries(true);
if (! isset(static::$curriencies[$list])) {
$countries = CountryLoader::countries($longlist);

foreach ($countries as $country) {
foreach ($country["currency"] as $currency => $details) {
static::$curriencies[$list][$currency] = $longlist? $details: $currency;
if ($longlist) {
foreach ($country['currency'] as $currency => $details) {
static::$curriencies[$list][$currency] = $longlist ? $details : $currency;
}
} else {
static::$curriencies[$list][] = $country['currency'];
}
}
}
}

return static::$curriencies[$list];
}
Expand Down

0 comments on commit 553ad84

Please sign in to comment.