Skip to content

Commit 8c7c2e6

Browse files
authored
Merge pull request #49 from InteractionDesignFoundation/worktree-replace-currencies-with-league-iso3166
Replace static Currencies.php with league/iso3166
2 parents 0416047 + 85b446e commit 8c7c2e6

3 files changed

Lines changed: 15 additions & 270 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"ext-curl": "*",
1717
"illuminate/cache": "^12.0 || ^13.0",
1818
"illuminate/console": "^12.0 || ^13.0",
19-
"illuminate/support": "^12.0 || ^13.0"
19+
"illuminate/support": "^12.0 || ^13.0",
20+
"league/iso3166": "^4.0"
2021
},
2122
"require-dev": {
2223
"friendsofphp/php-cs-fixer": "^3.50",

src/GeoIP.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Illuminate\Support\Arr;
88
use Illuminate\Cache\CacheManager;
9+
use League\ISO3166\Exception\OutOfBoundsException;
10+
use League\ISO3166\ISO3166;
911

1012
/**
1113
* @psalm-import-type LocationArray from \InteractionDesignFoundation\GeoIP\Location
@@ -19,12 +21,7 @@ class GeoIP
1921
*/
2022
protected $location = null;
2123

22-
/**
23-
* Currency data.
24-
*
25-
* @var array<string, string>|null
26-
*/
27-
protected $currencies = null;
24+
protected ?ISO3166 $iso3166 = null;
2825

2926
/**
3027
* GeoIP service instance.
@@ -162,11 +159,18 @@ private function find(?string $ip = null): Location
162159
*/
163160
public function getCurrency($iso)
164161
{
165-
if ($this->currencies === null && $this->config('include_currency', false)) {
166-
$this->currencies = include(__DIR__ . '/Support/Currencies.php');
162+
if (! $this->config('include_currency', false)) {
163+
return null;
167164
}
168165

169-
return Arr::get($this->currencies, $iso);
166+
try {
167+
$this->iso3166 ??= new ISO3166();
168+
$country = $this->iso3166->alpha2($iso);
169+
170+
return $country['currency'][0] ?? null;
171+
} catch (OutOfBoundsException) {
172+
return null;
173+
}
170174
}
171175

172176
/**

src/Support/Currencies.php

Lines changed: 0 additions & 260 deletions
This file was deleted.

0 commit comments

Comments
 (0)