ez-php / exchange
Currency exchange-rate lookup and conversion for Money values
2.4.2
2026-09-16 14:31 UTC
Requires
- php: ^8.5
- ez-php/bignum: ^2.0
- ez-php/http-client: ^2.0
- ez-php/money: ^2.0
Requires (Dev)
- ez-php/cache: ^2.0
- ez-php/docker: ^2.0
- friendsofphp/php-cs-fixer: ^3.94
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^13.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Currency exchange-rate lookup and conversion for Money values
Installation
composer require ez-php/exchange
Usage
Static rate table (tests, or a self-maintained table)
use EzPhp\Exchange\Converter; use EzPhp\Exchange\StaticExchangeRateProvider; use EzPhp\Money\Money; $provider = new StaticExchangeRateProvider([ 'EUR' => ['USD' => '1.08'], ]); $converter = new Converter($provider); $usd = $converter->convert(Money::of('10.00', 'EUR'), 'USD'); // 10.80 USD
HTTP-backed provider
use EzPhp\Exchange\Converter; use EzPhp\Exchange\HttpExchangeRateProvider; use EzPhp\HttpClient\Http; use EzPhp\Money\Money; $provider = new HttpExchangeRateProvider( Http::getClient(), 'https://api.example.com/rate?from={base}&to={quote}', 'rate', // dot-notation path to the rate value in the JSON response ); $converter = new Converter($provider); $jpy = $converter->convert(Money::of('1.00', 'EUR'), 'JPY'); // rounded to JPY's scale (0)
Historical/time-series rates are out of scope.
Caching a provider
use EzPhp\Cache\ArrayDriver; use EzPhp\Exchange\CachingExchangeRateProvider; use EzPhp\Exchange\Converter; use EzPhp\Exchange\HttpExchangeRateProvider; $cached = new CachingExchangeRateProvider( new HttpExchangeRateProvider(/* ... */), new ArrayDriver(), // or any ez-php/cache driver ttl: 3600, ); $converter = new Converter($cached);
Requires ez-php/cache (a soft dependency — declared in require-dev here, install it
separately in applications that want this decorator).
License
MIT