Skip to content

Contracts, events & manager

Fomvasss\Currency\Contracts\RateProvider — what the Currency service needs from a provider.

MethodReturnsDescription
getRates()array['USD' => ['buy' => float, 'sell' => float], ...] — base-currency units per 1 unit of the currency
getRate(string $currency)?array['buy' => float, 'sell' => float] or null
supports(string $currency)boolWhether getRates() contains the currency
getBaseCurrency()stringThe currency the rates are expressed in
getSupportedCurrencies()arrayCodes from getRates()
getSupportedCurrenciesCount()intTheir count
clearCache()voidForget cached rates (current, fallback and historical)

Fomvasss\Currency\Contracts\HistoricalRateProvider extends RateProvider — opt-in support for the *At methods of Currency.

MethodReturnsDescription
getRatesAt(DateTimeInterface $date)arraySame shape as getRates(), as of $date
getRateAt(string $currency, DateTimeInterface $date)?arrayOne currency as of $date

Currency::supportsHistoricalRates() is an instanceof HistoricalRateProvider check. AbstractRateProvider already has both methods; a subclass only declares implements HistoricalRateProvider and overrides getHistoricalApiUrl() — see Custom providers.

Fomvasss\Currency\Events\CurrencyRateFetchFailed, dispatched by AbstractRateProvider when the API fails.

PropertyTypeDescription
providerClassstringFQCN of the provider
errorMessagestringException message, API returned error status: N, API returned a non-array response, API returned no rates, Using fallback cached rates or No cached rates available, using static fallback
usingFallbackbooltrue when fallback (cached or static) rates are being served
fallbackRates?arrayThe rates served instead; [] when the static fallback is empty
date?DateTimeInterfaceThe requested date for historical fetches, else null

When and how often it fires — Caching & failures.

Fomvasss\Currency\ProviderManager, the scoped currency.manager binding; facade Fomvasss\Currency\Facades\CurrencyProvider (not auto-aliased).

MethodReturnsDescription
resolve(RateProvider|string $provider)RateProviderAn instance as is; a config alias; or a class name. Used by setRateProvider() and for default_provider
createProvider(string $name)RateProviderNew instance by config alias only. Used by useProvider()
provider(?string $name = null)RateProvidercreateProvider($name ?? default_provider) — so default_provider must be an alias here, not a class name
getAvailableProviders()arrayconfig('currency.providers')
getDefaultDriver()stringconfig('currency.default_provider')
any other methodmixedForwarded to provider() — a new instance of the default provider on each call

Instances are created with the container ($container->make($class)); a class that doesn’t exist or doesn’t implement RateProvider throws InvalidArgumentException. Every call creates a new instance.

AbstractConcrete
currency (alias Fomvasss\Currency\Currency)scoped Currency with default_provider (fresh per request/job)
currency.managerscoped ProviderManager

Fomvasss\Currency\ServiceProvider: merges config/currency.php, registers the bindings and the Blade directives; in the console also the currency-config publish tag and the Artisan commands.