florianv/swap
PHP 8.2+ currency exchange rate library with a single API over 30+ providers. Supports conversion, historical rates, PSR-16 caching, and provider fallback. Works with PSR-18 HTTP clients and PSR-17 factories for flexible integrations.
symfony/http-client) and caching systems (e.g., Illuminate\Cache) are fully compatible, requiring minimal abstraction.Exchanger\Contract\ExchangeRateService, enabling integration with internal APIs or legacy systems.Builder pattern simplifies configuration (e.g., fallback chains).Symfony\Component\Cache\Adapter\AdapterInterface), which Laravel’s Cache facade can wrap seamlessly.ChainException for provider fallbacks, which can be mapped to Laravel’s exception handling (e.g., render() in controllers).fastforex) may introduce rate limits or cost over time. Public providers (e.g., ECB) are free but limited in scope.coin_layer; may not meet all crypto-native applications’ needs.Swap to the container).fastforex or other commercial providers? What’s the cost/risk tolerance?Exchanger directly)?florianv/swap, symfony/http-client, nyholm/psr7 (or alternatives like Guzzle).moneyphp/money for advanced currency arithmetic (if using SwapExchange).european_central_bank for free EUR-based rates).fastforex) for broader coverage.Monolog channel).Swap to the container in AppServiceProvider:
$this->app->singleton(Swap::class, function ($app) {
return (new Builder())
->add('fastforex', ['api_key' => config('services.fastforex.key')])
->add('european_central_bank')
->useCache($app->make(\Illuminate\Contracts\Cache\Store::class))
->build();
});
Swap into controllers/services via constructor..env (e.g., FASTFOREX_API_KEY).config/services.php for provider settings:
'swap' => [
'providers' => [
'primary' => 'fastforex',
'fallback' => ['european_central_bank', 'central_bank_of_republic_turkey'],
],
],
fastforex free tier may have limits).Cache::forget() or scheduled jobs).florianv/swap and its dependencies (e.g., symfony/http-client) regularly.ChainException to identify which provider failed in the chain.swap.provider.response channel).400 error or fallback to a default rate).coin_layer requires crypto pairs to be lowercase).fastforex free tier allows 1,000 requests/month).Swap::latest() for single pairs or implement batch requests for multiple pairs (e.g., via Exchanger).swap:refresh command).fastforex paid plans).USD/JPY).central_bank_of_republic_turkey for TRY).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Primary provider outage | No rates available | Fallback chain to secondary providers. |
| Rate limit exceeded | 429 errors |
Implement exponential backoff or switch providers. |
| Unsupported currency pair | InvalidArgumentException |
Return a default rate or notify the user. |
| Cache corruption | Stale rates | Use short TTLs or cache invalidation hooks. |
| Provider API changes | Broken integrations | Test against provider sandboxes; monitor changelogs. |
| High latency | Poor UX (e.g., slow checkout) | Cache aggressively; use async rate fetching. |
Builder pattern and provider configuration.How can I help you explore Laravel packages today?