/laravel-exchange-rate

Laravel Exchange Rate service.

Primary LanguagePHPMIT LicenseMIT

Laravel Exchange Rate

Packagist Downloads Build Code Coverage Software License Contributor Covenant

Laravel Exchange Rate service.

Requirement

Install

Require this package with composer via command:

composer require yoelpc4/laravel-exchange-rate

Available Service Providers

Available exchange rate service providers:

Environment Variable

If you're planning to use Free Currency Converter Api as a provider. get your api key here. Then add these lines to your .env.

EXCHANGE_RATE_PROVIDER=free_currency_converter_api

FREE_CURRENCY_CONVERTER_API_BASE_URL=https://free.currconv.com/api/v8/
FREE_CURRENCY_CONVERTER_API_KEY=

Package Publication

Publish package configuration via command:

php artisan vendor:publish --provider="Yoelpc4\LaravelExchangeRate\ExchangeRateServiceProvider" --tag=config

Publish package resources via command:

php artisan vendor:publish --provider="Yoelpc4\LaravelExchangeRate\ExchangeRateServiceProvider" --tag=resources

Supported Currencies

Get supported currencies

try {
    $supportedCurrencies = \ExchangeRateService::currencies();
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    throw $e;
}

The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\SupportedCurrenciesInterface object.

Latest Exchange Rate

Get latest exchange rate

try {
    $base = 'IDR';
    
    $targets = [
        'USD', 'DZD',
    ];
    
    $latestExchangeRate = \ExchangeRateService::latest($base, $targets);
} catch (\Illuminate\Validation\ValidationException $e) {
    throw $e;
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    throw $e;
}

The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\LatestExchangeRateInterface object.

Historical Exchange Rate

Get historical exchange rate

try {
    $base = 'IDR';
    
    $targets = [
        'USD', 'DZD',
    ];

    $date = now()->subDays(3)->toDateString();
    
    $historicalExchangeRate = \ExchangeRateService::historical($base, $targets, $date);
} catch (\Illuminate\Validation\ValidationException $e) {
    throw $e;
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    throw $e;
}

The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\HistoricalExchangeRateInterface object.

Time Series Exchange Rate

Get time series exchange rate

try {
    $base = 'IDR';
    
    $targets = [
        'USD', 'DZD',
    ];
    
    $startDate = now()->subDays(8)->toDateString();

    $endDate = now()->toDateString();
    
    $timeSeriesExchangeRate = \ExchangeRateService::timeSeries($base, $targets, $startDate, $endDate);
} catch (\Illuminate\Validation\ValidationException $e) {
    throw $e;
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    throw $e;
}

The return value is an instance of \Yoelpc4\LaravelExchangeRate\Contracts\TimeSeriesExchangeRateInterface object.

Switching Provider

Switch between available providers

$exchangeRateService = \ExchangeRateService::provider('free_currency_converter_api');

Caveat

This package will run validation based on respective provider rules before dispatching some requests, therefore it will throw \Illuminate\Validation\ValidationException for every unmet validation rules.

License

The Laravel Exchange Rate is open-sourced software licensed under the MIT license.