h4kuna / exchange
Exchange between currencies.
Requires
- php: >=8.2
- h4kuna/critical-cache: ^v1.0.4
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0 || ^2.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.5
- guzzlehttp/psr7: ^2.4
- h4kuna/dir: ^0.1.2
- malkusch/lock: ^3.0
- mockery/mockery: ^1.6
- nette/caching: ^3.2
- nette/tester: ^2.5
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- tracy/tracy: ^2.10
Suggests
- ext-simplexml: If you want to use h4kuna\Exchange\Driver\Ecb or h4kuna\Exchange\Driver\RB.
- guzzlehttp/guzzle: As default implementation for PSR standards.
- guzzlehttp/psr7: Minimum ^2.4 for guzzle.
- h4kuna/dir: If you want to use build-in factory.
- malkusch/lock: As default lock system.
- nette/caching: If you have not own PSR-6 implementation.
Provides
None
Conflicts
None
Replaces
None
- dev-master
- dev-main
- v7.1.5
- v7.1.4
- v7.1.3
- v7.1.2
- v7.1.1
- v7.1.0
- v7.0.10
- v7.0.9
- v7.0.8
- v7.0.7
- v7.0.6
- v7.0.5
- v7.0.4
- v7.0.3
- v7.0.2
- v7.0.1
- v7.0.0
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.0.2
- v5.0.1
- v5.0.0
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v3.3.4
- v3.3.3
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.0
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
This package is auto-updated.
Last update: 2026-09-25 11:05:37 UTC
README
Part of the h4kuna PHP libraries, see the overview of all packages.
Exchange is a PHP library for working with currencies: it downloads exchange rates and converts prices between currencies.
Here is the changelog.
Framework extensions
Installation via composer
Requires PHP 8.2 or newer.
$ composer require h4kuna/exchange
Optional packages used by the default setup of ExchangeFactory:
$ composer require guzzlehttp/guzzle guzzlehttp/psr7 h4kuna/dir malkusch/lock nette/caching
guzzlehttp/guzzleandguzzlehttp/psr7are the default PSR-18 HTTP client and PSR-17 request factory; you can pass your own implementation instead.h4kuna/dir,nette/cachingandmalkusch/lockare used to build the default cache with locking; you can pass your ownRatingListCacheorCacheLockingFactoryInterfaceinstead.
The cache is PSR-16 based, provided by h4kuna/critical-cache.
How to use
Create the Exchange object by ExchangeFactory. The default driver for downloading rates is Cnb (Czech National Bank), here are the others: Ecb\Day (European Central Bank, current rates only) and RB\DayBuy, RB\DaySell, RB\DayCenter (Raiffeisenbank).
Or create Exchange with your own exchange rates, for example:
- 25 CZK = 1 EUR
- 20 CZK = 1 USD
use h4kuna\Exchange\Currency\Property; use h4kuna\Exchange\Exchange; use h4kuna\Exchange\ExchangeFactory; use h4kuna\Exchange\RatingList\RatingList; { # by factory $exchangeFactory = new ExchangeFactory( from: 'eur', to: 'usd', allowedCurrencies: [ 'CZK', 'USD', 'eur', // lower case will be changed to upper case ], ); $exchange = $exchangeFactory->create(); } { # or custom RatingList, the results below are for these rates $ratingList = new RatingList(new DateTimeImmutable(), new DateTimeImmutable(), null, [ 'EUR' => new Property(1, 25.0, 'EUR'), 'USD' => new Property(1, 20.0, 'USD'), 'CZK' => new Property(1, 1.0, 'CZK'), ]); $exchange = new Exchange('EUR', $ratingList, 'USD'); } echo $exchange->change(100) . PHP_EOL; // EUR -> USD = 125.0 // currency codes passed to change() must be upper case echo $exchange->change(100, 'CZK') . PHP_EOL; // CZK -> USD = 5.0 echo $exchange->change(100, null, 'CZK') . PHP_EOL; // EUR -> CZK = 2500.0 echo $exchange->change(100, 'USD', 'CZK') . PHP_EOL; // USD -> CZK = 2000.0
Change driver and date
Download historical exchange rates. This creates a new instance of Exchange with the historical rates.
use h4kuna\Exchange\Driver\Cnb\Day; use h4kuna\Exchange\RatingList\CacheEntity; $exchangePast = $exchangeFactory->create(cacheEntity: new CacheEntity(new DateTime('2000-12-30'), new Day)); echo $exchangePast->change(100) . PHP_EOL;
The second argument of CacheEntity is the driver, e.g. new CacheEntity(null, new \h4kuna\Exchange\Driver\Ecb\Day) for the current ECB rates.
Array access and iteration
use h4kuna\Exchange\Currency\Property; /* @var $property Property */ $property = $exchange['EUR']; var_dump($property); echo PHP_EOL; foreach ($exchange as $code => $property) { /* @var $property Property */ var_dump($code, $property); }
Caching
The cache of current rates expires automatically 30 minutes after the refresh time of the driver, the $refresh argument of the driver constructor (e.g. today 14:30:00 for Cnb\Day). It is a little better to rebuild the cache by cron, because then no request on the server has to wait for the download. Run the cron within the last 29 minutes before the cache expires.
use h4kuna\Exchange\RatingList\RatingListCache; use h4kuna\Exchange\RatingList\CacheEntity; use h4kuna\Exchange\Driver\Cnb\Day; /** @var RatingListCache $ratingListCache */ $ratingListCache->rebuild(new CacheEntity(null, new Day));
In the example, h4kuna\Exchange\Driver\Cnb\Day has the refresh time 14:30, so the cache expires at 15:00. Run the cron every day at 14:32.