amrshawky/currency
A Laravel/PHP package for currency exchange rates and conversion, with a clean API for fetching rates from multiple providers, converting amounts between currencies, and caching results for fast, reliable performance in your applications.
composer require amrshawky/currency
src/Currency.php. The public surface is small and fluent; confirm the exact method signatures there before spreading usage through the app.use AmrShawky\Currency\Currency;
$converted = (new Currency())
->source('USD')
->target('EUR')
->amount(100)
->get();
namespace App\Services;
use AmrShawky\Currency\Currency;
class CurrencyConverter
{
public function convert(string $from, string $to, float $amount): float
{
return (new Currency())
->source(strtoupper($from))
->target(strtoupper($to))
->amount($amount)
->get();
}
}
$key = "fx:USD:EUR:2024-01-15";
$rate = cache()->remember($key, now()->addDay(), function () {
return (new Currency())
->source('USD')
->target('EUR')
->amount(1)
->date('2024-01-15')
->get();
});
$this->app->bind(CurrencyConverter::class, function () {
return new CurrencyConverter();
});
Do not bind the package’s Currency object as a long-lived singleton unless you verify it is stateless; the fluent API may retain source/target/date state between calls.
bcmath or a money library for financial calculations.Y-m-d values and handled in UTC. Be aware that weekends/holidays may return the nearest available rate or no rate depending on the API.Currency per operation. State leakage is an easy bug with fluent clients.How can I help you explore Laravel packages today?