Skip to content

gnahotelsolutions/currency-converter

Repository files navigation

Convert price amounts between currencies

Latest Version on Packagist Build status Quality Score Total Downloads

Use this package to convert prices from one currency to another by the exchange rate using a base currency.

Installation

You can install the package via composer:

composer require gnahotelsolutions/currency-converter

Usage

Before anything, you need to create a repository of currencies to be loaded into the Converter.

A currency needs a name, exchange rate and decimals to be shown after the conversion.

$repository = new CurrenciesRepository([
    new Currency('EUR', 1, 2),
    new Currency('USD', 1.1, 2),
    // ...
]);

When declaring a Converter, you'll also have to tell what's your base currency from where the exchange rates are calculated.

$baseCurrency = new Currency('EUR', 1, 2);

$converter = new Converter($baseCurrency, $repository);

Once your Converter is ready, you can transform a Price using the fluent interface. The result will be a new Price instance with the converted amount and the desired currency

$price = new Price(1000, 'EUR');

$converter->from($price)->to('USD')->convert(); // new Price(1100, 'USD')

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.