Statistics
4167
17
1
11d
Badges
Tags
api

Exchange

Build Status Latest stable Downloads this Month Scrutinizer Code Quality Coverage Status

Exchange is PHP script works with currencies. You can convert price, format add VAT or only render exchange rates.

Changelog

v5.0

Dependency on Nette framework was removed, If you want, follow this extension. Minimal php is 5.5+. Api is changed, not compatible with older version.

v4.0

Here is older version.

Extension for framework

Installation via composer

$ composer require h4kuna/exchange

How to use

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

Change driver and date

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);

Format output

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

Temporary rate

$exchange->addRate('usd', 23.0);
$exchange->change(1, 'usd', 'czk'); // 23.0

$exchange->removeRate('usd');
$exchange->change(1, 'usd', 'czk'); // 20.0

Access and iterator

/* @var $property Exchange\Currenry\Property */
$property = $exchange['eur'];
var_dump($property);


foreach ($exchange as $code => $property) {
    /* @var $property Exchange\Currenry\Property */
    var_dump($property);
}

Limit for currencies

$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);
}

Save state to cookie

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']);

Cache

Default Cache save data on filesystem you can implement ICache interface and change it.

No release at this moment. Try to create first one.

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com