PHP code example of elegantly / laravel-forex

1. Go to this page and download the library: Download elegantly/laravel-forex 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/ */

    

elegantly / laravel-forex example snippets


use Brick\Math\RoundingMode;
use Elegantly\Forex\Integrations\ExchangeRateApiFree\ExchangeRateApiFreeConnector;

return [

    /**
     * Rounding mode used when converting money.
     */
    'rounding_mode' => RoundingMode::HalfUp,

    'cache' => [
        'enabled' => true,
        'driver' => env('FOREX_CACHE_DRIVER', env('CACHE_STORE', env('CACHE_DRIVER', 'file'))),
        'expiry_seconds' => 86_400, // 1 day
    ],

    'rate_limit' => [
        'enabled' => false,
        'driver' => env('FOREX_RATE_LIMIT_DRIVER', env('CACHE_STORE', env('CACHE_DRIVER', 'file'))),
        'every_seconds' => 3_600, // 1 hour
    ],

    'client' => ExchangeRateApiFreeConnector::class,

    'clients' => [
        'exchange-rate-api' => [
            'token' => env('EXCHANGE_RATE_API_TOKEN'),
        ],
    ],

];

use Elegantly\Forex\Facades\Forex;

$rates = Forex::latest('USD');

$usdToEur = $rates['EUR'];

use Carbon\Carbon;

$rates = Forex::rates(Carbon::create(2022, 4, 25), 'USD');

$usdToEur = $rates['EUR'];

use Brick\Math\RoundingMode;
use Brick\Money\Money;
use Elegantly\Forex\Facades\Forex;

$convertedMoney = Forex::convert(
    money: Money::of(100, 'USD'),
    currency: 'EUR',
);

$convertedMoney->__toString(); // (EUR) 88.84

use Carbon\Carbon;

$convertedMoney = Forex::convert(
    money: Money::of(100, 'USD'),
    currency: 'EUR',
    roundingMode: RoundingMode::Down,
    date: Carbon::create(2022, 4, 25),
);

use Brick\Money\Currency;
use Brick\Money\Money;
use Elegantly\Forex\Rules\ValidMoney;

$validated = $request->validate([
    'amount' => [
        '      ),
    ],
]);

$validated = $request->validate([
    'currency' => [' 'currency')],
]);

// Refresh latest rates
Forex::refreshLatest('USD');

// Refresh historical rates
Forex::refreshRates(Carbon::create(2022, 4, 25), 'USD');

use Elegantly\Forex\Integrations\ExchangeRateApi\ExchangeRateApiConnector;

'client' => ExchangeRateApiConnector::class,

use Carbon\CarbonInterface;
use Elegantly\Forex\ForexClient;

class MyCustomForexClient implements ForexClient
{
    public function latest(string $currency): array
    {
        // Return an associative array of currency code => rate
    }

    public function rates(CarbonInterface $date, string $currency): array
    {
        // Return historical rates for the given date
    }
}

'client' => \App\Services\MyCustomForexClient::class,
bash
php artisan vendor:publish --tag="forex-config"
bash
composer analyse