Installation
composer require baks-dev/reference-currency
Ensure your composer.json specifies PHP 8.4+ and Laravel 10+ (or equivalent framework).
Publish Config (if needed)
php artisan vendor:publish --provider="BaksDev\ReferenceCurrency\CurrencyServiceProvider" --tag="config"
Check config/currency.php for default settings (e.g., API endpoints, caching).
First Use Case Fetch the latest exchange rates for a currency (e.g., USD to RUB):
use BaksDev\ReferenceCurrency\Facades\Currency;
$rates = Currency::getRates('USD', ['RUB', 'EUR']);
// Returns: ['RUB' => 90.5, 'EUR' => 0.92]
Key Classes/Facades
Currency facade (primary entry point).CurrencyService (service container binding).RateRepository (handles data fetching/storage).Fetching Rates
$rate = Currency::rate('USD', 'RUB'); // Returns 90.5
$rates = Currency::getRates('USD', ['EUR', 'GBP']); // Array of rates
$historical = Currency::historical('USD', 'RUB', '2024-01-01');
Caching
config/currency.php:
'cache_ttl' => 3600, // 1 hour
'cache_driver' => 'redis', // or 'file', 'database'
Currency::clearCache();
Custom Sources
RateRepository to support custom APIs:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(\BaksDev\ReferenceCurrency\Contracts\RateRepository::class, CustomRateRepository::class);
}
Laravel Integration
// config/app.php
'providers' => [
BaksDev\ReferenceCurrency\CurrencyServiceProvider::class,
],
Route::get('/rates/{base}', [CurrencyController::class, 'index']);
Validation
use BaksDev\ReferenceCurrency\Rules\ValidCurrency;
$request->validate([
'base_currency' => ['required', new ValidCurrency],
'target_currencies' => ['array', 'required', new ValidCurrency],
]);
API Rate Limits
429 errors.RateRepository.Timezone Mismatches
$localTime = now()->timezone('Europe/Moscow')->format('Y-m-d');
Fallback Logic
config/currency.php:
'sources' => [
'primary' => 'central_bank',
'fallback' => 'exchange_rate_api',
],
Caching Issues
php artisan cache:clear.Cache::tags() to invalidate specific currency caches:
Cache::tags(['currency-rates'])->flush();
Currency Code Sensitivity
USD, not usd). Use strtoupper() if accepting user input.Enable Logging:
'debug' => env('CURRENCY_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Check API Responses:
$response = Currency::getRawResponse('USD', 'RUB');
// Inspect $response->body for raw API data.
Custom Rate Sources
BaksDev\ReferenceCurrency\Contracts\RateSource:
class CustomSource implements RateSource {
public function fetch(string $base, array $targets): array {
// Your logic here
}
}
config/currency.php:
'sources' => [
'custom' => \App\Services\CustomSource::class,
],
Event Listeners
// app/Listeners/LogCurrencyUpdate.php
public function handle(CurrencyUpdated $event) {
Log::info('Updated rate:', ['base' => $event->base, 'rates' => $event->rates]);
}
Testing
RateRepository in tests:
$this->app->instance(
\BaksDev\ReferenceCurrency\Contracts\RateRepository::class,
MockRateRepository::class
);
Currency::lazyRate('USD', 'RUB') to defer rate fetching until needed.currency_rates table and sync periodically:
Currency::syncWithDatabase();
How can I help you explore Laravel packages today?