Componette

Componette

h4kuna

h4kuna / exchange v7.0.8

ČNB, ECB exchange rates, built-in cache, expandable for other banks, PSR-7, 17, 18

download-cloud-line composer require h4kuna/exchange

Exchange

Downloads this Month Latest Stable Version Coverage Status Total Downloads License

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

Here is changelog.

Extension for framework

Installation via composer

$ composer require h4kuna/exchange

How to use

Init object Exchange by ExchangeFactory. 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\CriticalCache;use h4kuna\Exchange;

$cacheFactory = new CriticalCache\CacheFactory('exchange');

$exchangeFactory = new Exchange\ExchangeFactory(
    from: 'eur', 
    to: 'usd', 
    allowedCurrencies: [
		'CZK',
		'USD',
		'eur', // lower case will be changed to upper case
	],
	cacheFactory: $cacheFactory
);

$exchange = $exchangeFactory->create();

echo $exchange->change(100); // EUR -> USD = 125.0

// use only upper case
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. Make new instance of Exchange with history rate.

use h4kuna\Exchange\RatingList;
use h4kuna\Exchange;

$exchangePast = $exchange->modify(cacheEntity: new RatingList\CacheEntity(new \Datetime('2000-12-30'), Exchange\Driver\Cnb\Day::class));
$exchangePast->change(100);

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

Caching

The cache invalid automatic at some time, defined by property Driver::$refresh. From this property is counted time to live. Little better is invalid cache by cron. Because one request on server does not lock other requests. Let's run cron max. 15 minutes before invalidate cache.

use h4kuna\Exchange\RatingList\RatingListCache;
use h4kuna\Exchange\RatingList\CacheEntity;
use h4kuna\Exchange\Driver\Cnb\Day;

/** @var RatingListCache $ratingListCache */
$ratingListCache->rebuild(new CacheEntity(null, Day::class));

In example, is used h4kuna\Exchange\Driver\Cnb\Day::$refresh is defined at 15:00. Run cron 14:55 every day.

  • v7.0.8

    fix cache validation

  • v7.0.5

    • fix cache validation
    • add new rebuild method for cache
    • allowed psr-message 2.0
  • v7.0.4

    • add right dependencies
    • Exchange: keep internal object instead of string like currency code
  • v7.0.2

    • Driver: add timezone
    • Driver: add lazy mode via DriverAccessor
  • v7.0.1 v7.0.1

    • remove dependency on h4kuna/number-format
    • Formats.php and Filters.php moved this extension
  • v7.0.0 v7.0.0

    • your temporary rate implement by own RatingList
    • custom driver must be to use in Builder, see to ExchangeFactory
    • cache is implemented by PSR-6
    • remove dependency on Guzzle, PSR-7, PRS-17 and PSR-18 ready
    • remove dependency on h4kuna/data-type, nette/safe-stream, nette/utils
    • access for currency use RatingList instead of Exchange, Exchange::getRatingList()['EUR']
    • support php 8.0+
    • Exchange methods that are preserved are the same prototype and behavior
    • CookieManager moved this extension
bar-chart-fill

Statistics

download-cloud-fill
32369
star-fill
23
bug-fill
1
flashlight-fill
27d
price-tag-2-line

Badges

Componette Componette felix@nette.org