Skip to content

Currency facade

Fomvasss\Currency\Facades\Currency → the scoped currency binding, an instance of Fomvasss\Currency\Currency (also bound under that class name, so it can be injected). The facade is aliased as Currency by auto-discovery.

Currency codes are case-insensitive everywhere. $rateType is 'buy', 'sell', 'average', or null for config('currency.default_rate_type'); 'all' is accepted only by getRates()/getRatesAt(). An invalid type throws InvalidArgumentException.

MethodReturnsDescription
convert(float $amount, string $from, string $to, ?string $rateType = null)floatConverts through the base currency, rounded to the precision of $to. Throws InvalidArgumentException if a rate is missing
getRate(string $currency, ?string $rateType = null)?floatBase-currency units per 1 unit of $currency; 1.0 for the base currency; null if unknown
getRates(?string $rateType = null)array[code => float], or [code => ['buy' => float, 'sell' => float]] for 'all'. Base currency excluded
isSupported(string $currency)boolThe provider returned a rate for the currency (false for the provider’s base currency)
getSupportedCurrencies()arrayCodes returned by the provider
getSupportedCurrenciesCount()intTheir count

Details — Converting & rates.

All throw LogicException if the provider doesn’t implement HistoricalRateProvider.

MethodReturnsDescription
convertAt(float $amount, string $from, string $to, DateTimeInterface|string $date, ?string $rateType = null)floatconvert() with rates as of $date. Missing rate → InvalidArgumentException with the date in the message
getRateAt(string $currency, DateTimeInterface|string $date, ?string $rateType = null)?floatgetRate() as of $date
getRatesAt(DateTimeInterface|string $date, ?string $rateType = null)arraygetRates() as of $date. In all three *At methods a string date is parsed with Carbon::parse()
supportsHistoricalRates()boolWhether the current provider implements HistoricalRateProvider

Details — Historical rates.

MethodReturnsDescription
getBaseCurrency()stringRuntime override, else config('currency.default'), else the provider’s base
setBaseCurrency(string $currency)CurrencyOverride the base currency on the shared instance

Details — Base currency.

MethodReturnsDescription
useProvider(string $providerName)CurrencySwitch to a provider by config alias
setRateProvider(RateProvider|string $provider)CurrencySwitch by alias, class name or instance
getProvider()RateProviderCurrent provider instance
getRateProvider()RateProviderAlias of getProvider()
getAvailableProviders()arrayconfig('currency.providers')
clearCache()voidForget the current provider’s cached, fallback and historical rates

useProvider(), setRateProvider() and setBaseCurrency() mutate the shared instance — see the warning in Rate providers.

MethodReturnsDescription
format(float $amount, string $currency, bool $includeSymbol = true)stringnumber_format() with the currency’s separators and symbol
getCurrencyConfig(string $currency)arrayconfig('currency.currencies.CODE'), or []
getActiveCurrencies()arrayConfigured currencies without 'active' => false
getActiveCurrencyCodes()arrayTheir codes
getAllCurrencies()arrayAll configured currencies
getPrecision(string $currency)intThe currency’s precision, else default_precision
getDefaultPrecision()intconfig('currency.default_precision'), default 2

Details — Formatting & currencies.

new \Fomvasss\Currency\Currency(
RateProvider $rateProvider,
array $config = [], // the 'currency' config array
?ProviderManager $providerManager = null, // defaults to app('currency.manager')
);

The $config array is read on every call, not merged with defaults — pass config('currency').

Declared in src/helpers.php, each wrapped in function_exists().

FunctionReturnsCalls
currency_convert(float $amount, string $from, string $to, ?string $rateType = null)floatconvert()
currency_convert_at(float $amount, string $from, string $to, DateTimeInterface|string $date, ?string $rateType = null)floatconvertAt(); a string is parsed with Carbon::parse()
currency_format(float $amount, string $currency, bool $includeSymbol = true)stringformat()
currency_rate(string $currency, ?string $rateType = null)?floatgetRate()
currency_symbol(string $currency)stringgetCurrencyConfig()['symbol'], or the code as passed
DirectiveCompiles to
@currency(...)<?php echo app('currency')->convert(...); ?>
@currencyFormat(...)<?php echo app('currency')->format(...); ?>
@currencyRate(...)<?php echo app('currency')->getRate(...); ?>
@currencySymbol(...)<?php echo currency_symbol(...); ?>

See Helpers & Blade.