Exchange is PHP script works with currencies. You can convert price, format add VAT or only render exchange rates.
Here is changelog.
$ composer require h4kuna/exchange
Init object Exchange and Cache. Default Driver for read is Cnb, here are others.
For example define own exchange rates:
- 25 CZK = 1 EUR
- 20 CZK = 1 USD
use h4kuna\Exchange;
$cache = new Caching\Cache('/temp/dir');
$exchange = new Exchange\Exchange($cache);
$exchange->setDefault('eur');
$exchange->setOutput('usd');
echo $exchange->change(100); // EUR -> USD = 125.0
echo $exchange->change(100, 'czk'); // CZK -> USD = 5.0
echo $exchange->change(100, NULL, 'czk'); // EUR -> CZK = 2500.0
echo $exchange->change(100, 'usd', 'czk'); // USD -> CZK = 2000.0
Download history exchange rates.
$exchange->setDriver(new Exchange\Driver\Cnb\Day, new \Datetime('2000-12-30'));
If we need read data from database, it is not problem write own Driver.
class MyDatabaseDriver extend \h4kuna\Exchange\Driver\ADriver
{
/**
* Load data for iterator.
* @param DateTime|NULL $date
* @return array
*/
protected function loadFromSource(DateTime $date = NULL)
{
// your implementation
$this->setDate('Y-m-d', (string) $database->selectDate());
return $database->fetchMyCurrencies();
}
/**
* Modify data before save to cache.
* @return Exchange\Currency\Property|NULL
*/
protected function createProperty($row)
{
return new \h4kuna\Exchange\Currency\Property([
'code' => $row['currency'],
'home' => $row['rate'],
'foreign' => 1
]);
}
}
$exchange->setDriver(new \MyDatabaseDriver);
Define output formats, for more information read this documentation h4kuna/number-format.
$formats = new Exchange\Currency\Formats(new \h4kuna\Number\NumberFormatFactory());
$formats->addFormat('EUR', ['decimalPoint' => '.', 'unit' => '€']);
Create Filters for format API.
$filters = new Exchange\Filters($exchange, $formats);
You can define VAT
// VAT 21%
$filters->setVat(new \h4kuna\Number\Tax(21));
Output
// format 100 EUR like default to USD is set above
$filters->format(100); // '125,00 USD'
// count with VAT
$filters->formatVat(100, 'usd', 'eur'); // '96.80 €'
$filters->formatVatTo(100); // '151,25 USD'
$filters->formatTo(100, 'CZK'); // '2 000,00 CZK'
// Other options
$filters->change(100, 'usd', 'eur'); // 80.0
$filters->changeTo(100, 'usd'); // 125.0
$filters->vat(100); // 121.0
$exchange->addRate('usd', 23.0);
$exchange->change(1, 'usd', 'czk'); // 23.0
$exchange->removeRate('usd');
$exchange->change(1, 'usd', 'czk'); // 20.0
/* @var $property Exchange\Currenry\Property */
$property = $exchange['eur'];
var_dump($property);
foreach ($exchange as $code => $property) {
/* @var $property Exchange\Currenry\Property */
var_dump($property);
}
$cache = new Caching\Cache('/temp/dir');
$cache->setAllowedCurrencies(['CZK', 'USD', 'EUR']);
$exchange = new Exchange\Exchange($cache);
// in cache are only this three currencies
foreach ($exchange as $code => $property) {
/* @var $property Exchange\Currenry\Property */
var_dump($property);
}
Here is prepared object whose keep selected currency.
Run on startup of you project.
$cookieManager = Exchange\Http\CookieManager($exchange);
Set new option
$cookieManager->setCurrency($_GET['currency']);
Default Cache save data on filesystem you can implement ICache interface and change it.