Laravel Exchange Rates

repository·master·Indexed 19 days ago

https://github.com/ash-jc-allen/laravel-exchange-rates

A 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.

Tokens
7.2K
Snippets
30
Records
36
Agent score
66%

What's inside laravel-exchange-rates

  1. Manage exchange rate caching

    master

    By 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());
  2. Upgrade from 1.* to 2.0.0: Update ExchangeRate namespace

    master

    The ExchangeRate class 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;
  3. Upgrade from 6.* to 7.0.0: Update RequestSender interface

    master

    If you are implementing the AshAllenDesign\LaravelExchangeRates\Interfaces\RequestSender interface in your own code, you must update the makeRequest method signature. It now returns a ResponseContract instead of mixed.

    New signature:

    public function makeRequest(string $path, array $queryParams = []): ResponseContract;
  4. Upgrade from 4.* to 5.0.0: Minimum PHP and Dependency requirements

    master

    Upgrading 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.
  5. Upgrade from 3.* to 4.0.0: Configure exchangeratesapi.io API Key

    master

    As of April 2021, exchangeratesapi.io requires an API key. Add your credentials to your .env file:

    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() and exchangeRateBetweenDateRange(): Requires at least the 'Professional' plan.
    • exchangeRate() and convert() (with custom base currency): Requires at least the 'Basic' plan.
  6. Configure Laravel Exchange Rates

    master

    To customize the package behavior, you must first publish the configuration file to your config directory using the Artisan command below.

    After publishing, if your chosen API requires an API key, add it to your .env file using the EXCHANGE_RATES_API_KEY variable.

    php artisan vendor:publish --provider="AshAllenDesign\LaravelExchangeRates\Providers\ExchangeRatesProvider"
  7. Upgrade from 5.* to 6.0.0: Resolve ExchangeRate from the container

    master

    As of v6.0.0, the ExchangeRate class uses drivers and should no longer be instantiated directly with new. Instead, resolve it from the Laravel service container using the app() 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(...);
  8. Configure and switch exchange rate drivers

    master

    The package supports multiple API drivers. You can set a global default in config/laravel-exchange-rates.php using the driver key.

    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');