PHPackages                             bentools/currency - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bentools/currency

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bentools/currency
=================

Currency management in PHP.

1.1(6y ago)31.5k1MITPHPPHP &gt;=7.1

Since Apr 10Pushed 5y ago2 watchersCompare

[ Source](https://github.com/bpolaszek/currency)[ Packagist](https://packagist.org/packages/bentools/currency)[ RSS](/packages/bentools-currency/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (18)Versions (4)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/c66935521b07db12c96da20ce8b1ce74bb2e92d736fa66b863348c148ae329da/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f63757272656e63792f762f737461626c65)](https://packagist.org/packages/bentools/currency)[![License](https://camo.githubusercontent.com/35e5ddbd216566bc9701936828eb43c2207413a25046b2d026e215d45d16b35c/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f63757272656e63792f6c6963656e7365)](https://packagist.org/packages/bentools/currency)[![Build Status](https://camo.githubusercontent.com/ba8a24992889983c1066b92d15700f0712d374b766fb6bb6013b11b21dd97d05/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f62706f6c61737a656b2f63757272656e63792f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/bpolaszek/currency)[![Coverage Status](https://camo.githubusercontent.com/7629ec2a0d8a7b78b06ade40cd718002d38976847f841247d6074a97bdc55ed0/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f62706f6c61737a656b2f63757272656e63792f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/bpolaszek/currency?branch=master)[![Quality Score](https://camo.githubusercontent.com/e00a6d0b0a313476d4764f563ac7c4d42275b2e6e9f94e57c05d3179e01f2dcf/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f62706f6c61737a656b2f63757272656e63792e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/bpolaszek/currency)[![Total Downloads](https://camo.githubusercontent.com/711a25278ca04b0e2902e131167d05c04c987675641b97bf7aa91dd96cf9fa48/68747470733a2f2f706f7365722e707567782e6f72672f62656e746f6f6c732f63757272656e63792f646f776e6c6f616473)](https://packagist.org/packages/bentools/currency)

A very simple, framework-agnostic, PHP library to work with currencies.

Install
-------

[](#install)

> composer require bentools/currency:1.0.x-dev

Example use
-----------

[](#example-use)

```
use BenTools\Currency\Converter\CurrencyConverter;
use BenTools\Currency\Model\Currency;
use BenTools\Currency\Provider\EuropeanCentralBankProvider;
use DateTime;

$eur = new Currency('EUR');
$usd = new Currency('USD');

$exchangeRateProvider = new EuropeanCentralBankProvider();
$exchangeRate = $exchangeRateProvider->getExchangeRate($eur, $usd, new DateTime('yesterday'));

$currencyConverter = new CurrencyConverter($exchangeRate);
var_dump($currencyConverter->convert(299, $usd, $eur)); // float(242.67510753997)
var_dump($currencyConverter->convert(10.99, $eur, $usd)); // float(13.540779)
```

`bentools/currency` respects SOLID principles. So feel free to implement your own:

- CurrencyInterface (simple value object with a `getCode()` method)
- ExchangeRateInterface (value object with source currency, target currency and ratio)
- ExchangeRateFactoryInterface (how to instanciate ExchangeRate objects)
- ExchangeRateProviderInterface (provide live or historical rates)
- CurrencyConverterInterface (to convert from one currency to another)

Online rate providers
---------------------

[](#online-rate-providers)

We provide adapters for popular API providers:

- [European Central Bank](https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/index.en.html) - free use without limit, EUR-based, ~32 currencies supported
- [Fixer.io](https://fixer.io/)\* - ~170 currencies supported, including cryptocurrencies, EUR-based in free plan
- [CurrencyLayer.com](https://fixer.io/)\* - ~170 currencies supported, including cryptocurrencies, USD-based in free plan
- [OpenExchangeRates.org](https://fixer.io/)\* - ~200 currencies supported, including cryptocurrencies, USD-based in free plan

*\* Only free plans are currently supported by the built-in adapters. Feel free to submit a PR for supporting paid plans.*

Chained exchange rate provider
------------------------------

[](#chained-exchange-rate-provider)

This provider allow you to chain providers. The 1st one which returns a result wins.

```
use BenTools\Currency\Model\Currency;
use BenTools\Currency\Provider\ChainedExchangeRateProvider;
use BenTools\Currency\Provider\CurrencyLayerProvider;
use BenTools\Currency\Provider\EuropeanCentralBankProvider;
use BenTools\Currency\Provider\FixerIOProvider;
use BenTools\Currency\Provider\OpenExchangeRatesProvider;

$eur = new Currency('EUR');
$usd = new Currency('USD');

$providers = [
    new EuropeanCentralBankProvider(),
    new OpenExchangeRatesProvider($openExchangeApiKey),
    new FixerIOProvider($fixerIoApiKey),
    new CurrencyLayerProvider($currencyLayerApiKey),
];

$exchangeRateProvider = new ChainedExchangeRateProvider($providers);
$exchangeRateProvider->getExchangeRate($eur, $usd);
```

Average exchange rate provider
------------------------------

[](#average-exchange-rate-provider)

Get an average exchange rate from different providers. If you provide a tolerance, an exception will be thrown if the difference between the lowest and the highest rate exceeds it.

```
use BenTools\Currency\Model\Currency;
use BenTools\Currency\Provider\AverageExchangeRateProvider;
use BenTools\Currency\Provider\CurrencyLayerProvider;
use BenTools\Currency\Provider\EuropeanCentralBankProvider;
use BenTools\Currency\Provider\FixerIOProvider;
use BenTools\Currency\Provider\OpenExchangeRatesProvider;
use DateTimeImmutable;

$eur = new Currency('EUR');
$usd = new Currency('USD');

$providers = [
    new EuropeanCentralBankProvider(),
    new OpenExchangeRatesProvider($openExchangeApiKey),
    new FixerIOProvider($fixerIoApiKey),
    new CurrencyLayerProvider($currencyLayerApiKey),
];

$tolerance = 0.005;
$exchangeRateProvider = AverageExchangeRateProvider::create($tolerance)->withProviders(...$providers);

$exchangeRateProvider->getExchangeRate($eur, $usd, new DateTimeImmutable('2018-03-29'))->getRatio();
```

PSR-16 provider
---------------

[](#psr-16-provider)

Cache your exchange rates for the duration of your choice with the PSR-16 decorator:

```
use BenTools\Currency\Model\Currency;
use BenTools\Currency\Provider\EuropeanCentralBankProvider;
use BenTools\Currency\Provider\PSR16CacheProvider;
use Redis;
use Symfony\Component\Cache\Simple\RedisCache;

$eur = new Currency('EUR');
$usd = new Currency('USD');

$redis = new Redis();
$redis->connect('localhost');
$ttl = 3600;
$provider = new PSR16CacheProvider(
    new EuropeanCentralBankProvider(),
    new RedisCache($redis, 'currencies', $ttl)
);

$exchangeRate = $provider->getExchangeRate($eur, $usd);
```

Doctrine ORM provider
---------------------

[](#doctrine-orm-provider)

If you store your own exchange rates with Doctrine ORM, the built-in implementation may help:

```
use App\Entity\ExchangeRate;
use BenTools\Currency\Model\Currency;
use BenTools\Currency\Provider\DoctrineORMProvider;
use DateTimeImmutable;

$eur = new Currency('EUR');
$usd = new Currency('USD');

$exchangeRateProvider = new DoctrineORMProvider($doctrine, ExchangeRate::class);
$exchangeRateProvider->getExchangeRate($eur, $usd, new DateTimeImmutable('2018-03-29'))->getRatio();
```

Framework agnostic
------------------

[](#framework-agnostic)

`bentools/currency` leverages [HttpPlug](http://docs.php-http.org/en/latest/) to connect to the APIs. This means:

- You're free to use any HTTP client (Guzzle 5, Guzzle 6, React, Zend, Buzz, ...) already existing in your project
- By default, `bentools/currency` will automagically discover the client to use
- You can enforce a specific client with its specific configuration in any `ExchangeRateProvider`.

Don't forget that most free plans limit to 1000 calls/month, so you'd better configure your Http Client to cache responses. If you use Guzzle 6+, have a look at [kevinrob/guzzle-cache-middleware](https://github.com/Kevinrob/guzzle-cache-middleware)

Tests
-----

[](#tests)

> ./vendor/bin/phpunit

License
-------

[](#license)

MIT

Disclaimer
----------

[](#disclaimer)

This library is intended to be easy to use, and aims at covering most common needs. To achieve this, it uses `floats` as type hints (but you're free to store as integers and convert back).

Keep in mind that working with floats can lead to inaccurate results when dealing with a big number of digits. [Read this](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) for further information.

```
$float = 0.1234567890123456789;
$factorA = 1000;
$factorB = 10000;

var_dump(($float * $factorA / $factorA) === $float); // true
var_dump(($float * $factorB / $factorB) === $float); // false

$integer = 1000000000000;
$factorA = 1000000;
$factorB = 10000000;
var_dump($integer * $factorA === (int) (float) ($integer * $factorA)); // true
var_dump($integer * $factorB === (int) (float) ($integer * $factorB)); // false
```

In short, avoid dealing with big integers or a high number of decimals when using this library if precision matters.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 95.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~464 days

Total

2

Last Release

2492d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/acdd1a8ee0e657ddd06cf11f98a32100ef7121afb8aa270a5b295f5c29c038b3?d=identicon)[bpolaszek](/maintainers/bpolaszek)

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (20 commits)")[![misaert](https://avatars.githubusercontent.com/u/12974251?v=4)](https://github.com/misaert "misaert (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/bentools-currency/health.svg)

```
[![Health](https://phpackages.com/badges/bentools-currency/health.svg)](https://phpackages.com/packages/bentools-currency)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.6M651](/packages/sylius-sylius)[illuminate/contracts

The Illuminate Contracts package.

704122.9M10.1k](/packages/illuminate-contracts)[wallabag/wallabag

open source self hostable read-it-later web application

12.6k2.2k](/packages/wallabag-wallabag)[florianv/exchanger

Currency exchange rates framework for PHP

1874.7M15](/packages/florianv-exchanger)[razorpay/ifsc

Razorpay IFSC Codes Library

382201.9k](/packages/razorpay-ifsc)[phiki/phiki

Syntax highlighting using TextMate grammars in PHP.

3573.0M23](/packages/phiki-phiki)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
