Laravel Exchange Rates
repository·master·Indexed 19 days ago
https://github.com/ash-jc-allen/laravel-exchange-ratesA Laravel package for interacting with multiple exchange rate APIs to fetch latest and historical rates and perform currency conversions. It supports drivers for Exchange Rates API IO, Exchange Rates Data API, Exchange Rate Host, and CurrencyBeacon. Features include a Laravel Facade, a ValidCurrency validation rule, and built-in caching management with options to bust or disable the cache.
What's inside laravel-exchange-rates
- Laravel Exchange Rates is a package designed for interacting with various exchange rate APIs. It provides functionality to retrieve the latest or historical exchange rates and perform monetary conversions between different currencies within a Laravel application.
Manage exchange rate caching
masterBy default, all API responses are cached to improve performance and reduce bandwidth. You can control this behavior using the following methods:
- Bust the cache: Use
shouldBustCache()to ignore existing cached values and force a fresh API request. - Disable caching: Use
shouldCache(false)to prevent the result of a specific call from being stored in the cache.
use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate; $exchangeRates = app(ExchangeRate::class); // Force a fresh API request (ignore cache) $exchangeRates->shouldBustCache()->convert(100, 'GBP', 'EUR', Carbon::now()); // Perform request without caching the result $exchangeRates->shouldCache(false)->convert(100, 'GBP', 'EUR', Carbon::now());- Bust the cache: Use
Upgrade from 1.* to 2.0.0: Update ExchangeRate namespace
masterThe
ExchangeRateclass namespace has changed to be consistent with other library classes. You must update your import statements.Old namespace:
AshAllenDesign\LaravelExchangeRates\ExchangeRate
New namespace:AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate// Change from this: use AshAllenDesign\LaravelExchangeRates\ExchangeRate; // To this: use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate;Run tests for the package
masterIf you are contributing to the package, you can execute the test suite from the package's root directory using Composer.
composer testUpgrade the library
masterTo update this library to newer versions, refer to the dedicatedUPGRADE.mdguide for specific instructions and breaking changes.Upgrade from 6.* to 7.0.0: Update RequestSender interface
masterIf you are implementing the
AshAllenDesign\LaravelExchangeRates\Interfaces\RequestSenderinterface in your own code, you must update themakeRequestmethod signature. It now returns aResponseContractinstead ofmixed.New signature:
public function makeRequest(string $path, array $queryParams = []): ResponseContract;Upgrade from 4.* to 5.0.0: Minimum PHP and Dependency requirements
masterUpgrading to v5.0.0 introduces the following requirements:
PHP Version:
- Requires PHP 8.0 or higher (PHP 7.2, 7.3, and 7.4 are no longer supported).
Laravel & Dependencies:
- Laravel: Minimum version 8.
- guzzlehttp/guzzle: Minimum version 7.0.
- orchestra/testbench: Minimum version 5.0.
- phpunit/phpunit: Minimum version 9.0.
Install Laravel Exchange Rates via Composer
masterTo install the package, run the following Composer command in your terminal:
composer require ashallendesign/laravel-exchange-ratesMinimum Requirements:
- PHP 8.2
- Laravel 9
Upgrade from 3.* to 4.0.0: Configure exchangeratesapi.io API Key
masterAs of April 2021,
exchangeratesapi.iorequires an API key. Add your credentials to your.envfile:EXCHANGE_RATES_API_URL=https://api.exchangeratesapi.io/v1/ EXCHANGE_RATES_API_KEY={Your-API-Key-Here}Plan Requirements:
- HTTPS: Requires at least the 'Basic' plan.
convertBetweenDateRange()andexchangeRateBetweenDateRange(): Requires at least the 'Professional' plan.exchangeRate()andconvert()(with custom base currency): Requires at least the 'Basic' plan.
Configure Laravel Exchange Rates
masterTo customize the package behavior, you must first publish the configuration file to your
configdirectory using the Artisan command below.After publishing, if your chosen API requires an API key, add it to your
.envfile using theEXCHANGE_RATES_API_KEYvariable.php artisan vendor:publish --provider="AshAllenDesign\LaravelExchangeRates\Providers\ExchangeRatesProvider"Upgrade from 5.* to 6.0.0: Resolve ExchangeRate from the container
masterAs of v6.0.0, the
ExchangeRateclass uses drivers and should no longer be instantiated directly withnew. Instead, resolve it from the Laravel service container using theapp()helper to ensure the correct driver is used.Old way (v5.x and below):
use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate; $exchangeRate = (new ExchangeRate())->exchangeRate(...);New way (v6.0.0+):
use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate; $exchangeRate = app(ExchangeRate::class)->exchangeRate(...);use AshAllenDesign\LaravelExchangeRates\Classes\ExchangeRate; $exchangeRate = app(ExchangeRate::class)->exchangeRate(...);Configure and switch exchange rate drivers
masterThe package supports multiple API drivers. You can set a global default in
config/laravel-exchange-rates.phpusing thedriverkey.To switch drivers fluently for a specific call, use the
driver()method on either the class instance or the Facade.// Set default in config/laravel-exchange-rates.php: // 'driver' => 'exchange-rates-api-io' // Switching driver fluently (Class instance): $exchangeRates = app(ExchangeRate::class); $result = $exchangeRates->driver('exchange-rates-data-api')->exchangeRate('GBP', 'EUR'); // Switching driver fluently (Facade): use AshAllenDesign\LaravelExchangeRates\Facades\ExchangeRate; $result = ExchangeRate::driver('exchange-rates-data-api')->exchangeRate('GBP', 'EUR');