PHP code example of benjaminmal / exchangeratehost-bundle
1. Go to this page and download the library: Download benjaminmal/exchangeratehost-bundle 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/ */
benjaminmal / exchangeratehost-bundle example snippets
// config/bundles.php
return [
// ...
Benjaminmal\ExchangeRateHostBundle\ExchangeRateHostBundle::class => ['all' => true],
];
namespace App\Service;
use Benjaminmal\ExchangeRateHostBundle\Client\ExchangeRateHostClientInterface;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\ConvertCurrencyOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\EuVatRatesOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\FluctuationDataOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\HistoricalRatesOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\LatestRatesOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\SupportedSymbolsOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Option\TimeSeriesDataOption;
use Benjaminmal\ExchangeRateHostBundle\Model\Output\FluctuationData;
use Benjaminmal\ExchangeRateHostBundle\Model\Output\SymbolData;
use Benjaminmal\ExchangeRateHostBundle\Model\Output\VatRates;
class MyService
{
public function __construct(private readonly ExchangeRateHostClientInterface $client)
{
}
public function getLatestRates()
{
// Get the latest rates
$rates = $this->client->getLatestRates(
options: new LatestRatesOption( // Optional
base: 'EUR',
symbols: ['USD', 'CZK'],
amount: 1200,
places: 2,
source: 'ecb',
callback: 'functionName',
),
);
/**
* @var string $currency
* @var float $rate
*/
foreach ($rates as $currency => $rate) {
// ...
}
// ...
}
public function convertCurrency(): int
{
// Convert price
/** @var int $newAmount */
$newAmount = $this->client->convertCurrency(
fromCurrency: 'EUR', // Required
toCurrency: 'USD', // Required
amount: 1300, // Required
options: new ConvertCurrencyOption(), // Optional
);
// ...
}
public function getHistoricalRates()
{
// Get rates from a specific day
$rates = $this->client->getHistoricalRates(
date: new \DateTimeImmutable('-10days'), // Required, will be converted in the url with the format 'Y-m-d'
options: new HistoricalRatesOption(), // Optional
);
/**
* @var string $currency
* @var float $rate
*/
foreach ($rates as $currency => $rate) {
// ...
}
// ...
}
public function getTimeSeriesRates()
{
// Get the rates between 2 dates
$rates = $this->client->getTimeSeriesRates(
startDate: new \DateTimeImmutable('-89days'), // Required
endDate: new \DateTimeImmutable('-10days'), // Required
options: new TimeSeriesDataOption(), // Optional
);
/**
* @var $date string formatted as 'Y-m-d'
* @var $datum iterable<string, float>
*/
foreach ($rates as $date => $datum) {
foreach ($datum as $currency => $rate) {
// ...
}
}
// ...
}
public function getFluctuationData()
{
// Get the fluctuation data between 2 dates
$data = $this->client->getFluctuationData(
startDate: new \DateTimeImmutable('-89days'), // Required
endDate: new \DateTimeImmutable('-10days'), // Required
options: new FluctuationDataOption(), // Optional
);
/**
* @var FluctuationData $fluctuationData
*/
foreach ($data as $currency => $fluctuationData) {
echo $fluctuationData->startRate;
echo $fluctuationData->endRate;
echo $fluctuationData->change;
echo $fluctuationData->changePct;
}
// ...
}
public function getCurrencies()
{
// Get the supported currencies
$supportedCurrencies = $this->client->getSupportedCurrencies(
options: new SupportedSymbolsOption(), // Optional
);
/**
* @var SymbolData $supportedCurrency
*/
foreach ($supportedCurrencies as $currency => $supportedCurrency) {
echo $supportedCurrency->code;
echo $supportedCurrency->description;
}
// ...
}
public function getEurVatRates()
{
// Get EU VAT rates
$rates = $this->client->getEuVatRates(
options: new EuVatRatesOption(), // Optional
);
/**
* @var VatRates $vatRate
*/
foreach ($rates as $countryCode => $vatRate) {
echo $vatRate->countryName;
echo $vatRate->standardRate;
echo implode(', ', $vatRate->parkingRates);
echo implode(', ', $vatRate->reducedRates);
echo implode(', ', $vatRate->superReducedRates);
}
}
}