moneyphp/iso-currencies
Up-to-date ISO 4217 currency list for MoneyPHP, sourced from the official ISO 4217 Maintenance Agency (currency-iso.org). Includes tooling to fetch and update the currency dataset via Composer for use with moneyphp/money.
Install the Package
Add to your composer.json and run:
composer require moneyphp/iso-currencies
Fetch Latest Currencies Update the currency data from the official ISO source:
composer fetch-update
(This populates the data/current.json file with the latest ISO 4217 definitions.)
Basic Usage with MoneyPHP
If using moneyphp/money, integrate the CurrencyRepository:
use Money\Currency\CurrencyRepository;
use Money\Currency\ISOCurrency;
$repository = new CurrencyRepository();
$eur = $repository->getCurrency('EUR'); // Returns ISOCurrency instance
Standalone Validation For non-MoneyPHP use (e.g., form validation), access the raw data:
$currencies = include __DIR__.'/vendor/moneyphp/iso-currencies/data/current.php';
$isValid = isset($currencies['EUR']); // Check if currency exists
Use the package to validate user inputs (e.g., API payloads, forms):
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'currency' => [
'required',
function ($attribute, $value, $fail) {
$currencies = include __DIR__.'/vendor/moneyphp/iso-currencies/data/current.php';
if (!isset($currencies[$value])) {
$fail('Invalid currency code.');
}
},
],
]);
Bind the CurrencyRepository for dependency injection:
// config/app.php
'bindings' => [
Money\Currency\CurrencyRepository::class => function ($app) {
return new Money\Currency\CurrencyRepository();
},
];
Now inject it into controllers/services:
public function __construct(private CurrencyRepository $currencyRepo) {}
Check if a currency is historic (e.g., BGN post-2026):
$currency = $currencyRepo->getCurrency('BGN');
if ($currency->isHistorical()) {
// Handle legacy logic (e.g., log warning, redirect to EUR)
}
Fetch all currencies for dropdowns or reporting:
$allCurrencies = $currencyRepo->getAll();
$currencyCodes = array_keys($allCurrencies);
CurrencyRepository calls.ISOCurrency objects for type safety and metadata (e.g., getSymbol(), getSubUnit()).use Illuminate\Validation\Rule;
$rules = [
'currency' => ['required', Rule::in(array_keys($currencies))],
];
current.php file to avoid repeated file reads:
$currencies = Cache::remember('iso_currencies', now()->addDays(7), function () {
return include __DIR__.'/vendor/moneyphp/iso-currencies/data/current.php';
});
CurrencyRepository in tests:
$this->app->instance(CurrencyRepository::class, $mockRepository);
Static Data Updates
composer fetch-update must be run manually or via CI/CD to sync with ISO changes.composer.json:
"scripts": {
"post-install-cmd": [
"@php -r \"if (!file_exists(__DIR__.'/vendor/moneyphp/iso-currencies/data/current.json')) { shell_exec('composer fetch-update'); }\""
]
}
Historical Currency Logic
if ($currency->isHistorical() && $request->is('admin')) {
throw new \RuntimeException('Currency deprecated; use EUR instead.');
}
File Path Assumptions
data/current.php is in vendor/. Custom paths require manual inclusion.realpath() or environment variables for paths.PHP Version Lock
php-version in composer.json or use a wrapper for legacy apps.Missing Currencies
composer fetch-update to sync with ISO.data/current.json for errors (e.g., malformed YAML).Deprecated Currency Errors
isHistorical() to handle transitions gracefully:
if ($currency->isHistorical()) {
logger()->warning("Using deprecated currency: {$currency->getCode()}");
}
Performance Bottlenecks
current.php on every request. Cache the result or lazy-load:
static $currencies = null;
if (is_null(self::$currencies)) {
self::$currencies = include __DIR__.'/vendor/.../current.php';
}
Custom Currency Metadata
ISOCurrency or wrap the repository to add fields (e.g., getRegion()):
class ExtendedCurrency extends ISOCurrency {
public function getRegion(): string {
return $this->getAttribute('region', 'Global');
}
}
Database Sync
currencies table on app boot:
$currencies = include __DIR__.'/vendor/.../current.php';
foreach ($currencies as $code => $data) {
DB::table('currencies')->updateOrCreate(
['code' => $code],
['name' => $data['name'], 'symbol' => $data['symbol']]
);
}
API Wrapper
Route::get('/api/currencies', function () {
return response()->json(
include __DIR__.'/vendor/moneyphp/iso-currencies/data/current.php'
);
});
Localization
$currencyRepo->setLocale('fr_FR'); // Hypothetical; may require custom logic
Composer Scripts
composer fetch-update has write permissions to vendor/.composer.json:
"scripts": {
"fetch-update": "php -r \"file_put_contents(__DIR__.'/vendor/moneyphp/iso-currencies/data/current.json', file_get_contents('https://raw.githubusercontent.com/moneyphp/iso-currencies/main/data/current.json'));\""
}
Symfony YAML Support
symfony/yaml is installed (v8.0+):
composer require symfony/yaml:^8.0
Historical Data Retention
$oldCurrencies = include __DIR__.'/vendor/.../current.php';
$newCurrencies = include __DIR__.'/vendor/.../current.php';
$changes = array_diff_assoc($oldCurrencies, $newCurrencies);
logger()->info('Currency changes detected:', $changes);
How can I help you explore Laravel packages today?