alcohol/iso4217
Lightweight PHP library with ISO 4217 currency data. Look up currencies by alpha-3 (e.g., EUR) or numeric code (978), or retrieve the full list, including name, minor unit exponent, and associated country codes.
Installation:
composer require alcohol/iso4217
Add to composer.json under require or require-dev if only for testing.
First Use:
use Alcohol\ISO4217;
$iso4217 = new ISO4217();
$currency = $iso4217->getByAlpha3('USD');
Verify the response structure:
// Expected output:
// [
// 'name' => 'US Dollar',
// 'alpha3' => 'USD',
// 'numeric' => '840',
// 'exp' => 2,
// 'country' => ['US', 'EC', 'PA', ...]
// ]
Key Methods:
getByAlpha3(string $code): Fetch by 3-letter code (e.g., 'EUR').getByNumeric(string $code): Fetch by numeric code (e.g., '978').getAll(): Return all currencies as an associative array (keyed by alpha3).Currency Validation:
$iso4217 = new ISO4217();
try {
$currency = $iso4217->getByAlpha3('XYZ'); // Throws \InvalidArgumentException
} catch (\InvalidArgumentException $e) {
// Handle invalid currency (e.g., log or show user-friendly message)
}
Country-Currency Mapping:
// Get all countries using a specific currency (e.g., EUR)
$eurCountries = $iso4217->getByAlpha3('EUR')['country'];
Dynamic Formatting:
// Format currency symbol dynamically (e.g., for display)
$currency = $iso4217->getByAlpha3('JPY');
$symbol = $currency['alpha3']; // 'JPY' (use a separate library like `moneyphp/money` for rendering)
Integration with Laravel:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(ISO4217::class);
}
// app/Facades/Iso4217.php
public static function getByAlpha3(string $code) {
return app(ISO4217::class)->getByAlpha3($code);
}
Register in config/app.php under aliases.Caching:
// Cache all currencies for performance (e.g., in a command or service)
$currencies = Cache::remember('iso4217.all', now()->addYear(), function () {
return app(ISO4217::class)->getAll();
});
Testing:
// Example test case
public function testGetByAlpha3()
{
$iso4217 = new ISO4217();
$this->assertEquals('US Dollar', $iso4217->getByAlpha3('USD')['name']);
}
Deprecated Currencies:
VEF for Venezuelan Bolívar Fuerte). Use VES instead.Case Sensitivity:
'USD' works, 'Usd' throws an exception).'840' not 840).PHP Version:
Data Accuracy:
Performance:
getAll() loads ~180 currencies into memory. Cache the result if called frequently:
Cache::rememberForever('iso4217.all', fn() => $iso4217->getAll());
Invalid Codes:
getAll() to inspect available codes during development.error_log(print_r($iso4217->getAll(), true));
Type Hints:
array or throw \InvalidArgumentException. Use PHP 8’s strict typing (declare(strict_types=1)) to catch issues early.Testing Edge Cases:
'XDR' for Special Drawing Rights) to ensure your app handles them gracefully.Custom Data Sources:
ISO4217 class to load data from an external API or database:
class CustomISO4217 extends ISO4217 {
protected function loadData(): array {
return Cache::get('custom_iso4217_data') ?? [];
}
}
Validation Rules:
use Illuminate\Validation\Rule;
$rules = [
'currency' => ['required', Rule::in(array_keys($iso4217->getAll()))],
];
Localization:
'name_es' for Spanish):
$currency = $iso4217->getByAlpha3('EUR');
// Manually merge localized data if needed
$currency['name_es'] = 'Euro';
Event Listeners:
event(new CurrencyFetched($currency));
How can I help you explore Laravel packages today?