PHP code example of hennest / exchange-rate

1. Go to this page and download the library: Download hennest/exchange-rate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hennest / exchange-rate example snippets


'providers' => [
    // Other Service Providers
    Hennest\ExchangeRate\Providers\ExchangeRateServiceProvider::class,
],

use Hennest\ExchangeRate\Contracts\ExchangeRateInterface;

class ExampleController
{
    public function __construct(
        private ExchangeRateInterface $exchangeRate
    ) {}

    public function example()
    {
        // Get exchange rates for specific currencies
        $rates = $this->exchangeRate->rates(['EUR', 'GBP', 'JPY']);

        // Get a single exchange rate
        $rate = $this->exchangeRate->getRate('EUR');

        // Convert currency
        $convertedAmount = $this->exchangeRate->convert(100, 'USD', 'EUR');
    }
    
    public function anotherExample()
    {
        // Get exchange rates for specific currencies
        $rates = app(ExchangeRateInterface::class)->rates(['EUR', 'GBP', 'JPY']);

        // Get a single exchange rate
        $rate = app(ExchangeRateInterface::class)->getRate('EUR');

        // Convert currency
        $convertedAmount = app(ExchangeRateInterface::class)->convert(100, 'USD', 'EUR');
    }
}

return [
    // Base currency for the exchange rate
    'base_currency' => env('EXCHANGE_RATE_BASE_CURRENCY', 'USD'),

    // API key for the exchange rate service
    'api_key' => env('EXCHANGE_RATE_API_KEY', ''),

    // Arbitrary Precision Calculator settings
    'math' => [
        'scale' => env('EXCHANGE_RATE_SCALE', 10),
    ],

    // Cache configuration
    'cache' => [
        'prefix' => env('EXCHANGE_RATE_CACHE_PREFIX', 'exchange_rate'),
        'driver' => env('EXCHANGE_RATE_CACHE_DRIVER', env('CACHE_STORE', 'file')),
        'ttl' => env('EXCHANGE_RATE_CACHE_TTL', 6 * 3600),
    ],

    // Builder classes for creating DTOs
    'assemblers' => [
        'response' => Hennest\ExchangeRate\Assembler\ResponseAssembler::class,
    ],

    // Customizable service classes
    'services' => [
        'api' => Hennest\ExchangeRate\Drivers\CurrencyApiService::class,
        'cache' => Hennest\ExchangeRate\Services\CacheService::class,
        'parser' => Hennest\ExchangeRate\Services\ParserService::class,
        'exchange_rate' => Hennest\ExchangeRate\Services\ExchangeRateService::class,
    ],

    // Default driver
    'default_driver' => 'currency-api',

    // Available API drivers
    'drivers' => [
        'currency-api' => [
            'api' => Hennest\ExchangeRate\Drivers\CurrencyApiService::class,
        ],
        'currency-beacon' => [
            'api' => Hennest\ExchangeRate\Drivers\CurrencyBeaconApiService::class,
        ],
    ],
];

'services' => [
    'api' => \App\Services\MyCustomApiService::class,
    'cache' => \App\Services\MyCustomCacheService::class,
    'parser' => \App\Services\MyCustomParserService::class,
    'exchange_rate' => \App\Services\MyCustomExchangeRateService::class,
],

'drivers' => [
    'currency-api' => [
        'api' => Hennest\ExchangeRate\Drivers\CurrencyApiService::class,
    ],
    'currency-beacon' => [
        'api' => Hennest\ExchangeRate\Drivers\CurrencyBeaconApiService::class,
    ],
    'my-custom-driver' => [
        'api' => \App\Services\MyCustomApiService::class,
    ],
],

'default_driver' => 'my-custom-driver',
bash
php artisan vendor:publish --tag=exchange-rate-config