plugowski / currency_converter
Requires
- php: >=5.6
- plugowski/number_speller: *
Requires (Dev)
- phpunit/phpunit: 4.8.9
This package is not auto-updated.
Last update: 2025-01-05 02:29:59 UTC
README
Currency converter with service to get latest currencies from NBP (Narodowy Bank Polski) and ECB (European Central Bank). Converter has got built-in number speller which convert price into words with specified currency.
Installation
Just clone that repository (remember about NumberSpeller dependency) or just use composer:
composer require plugowski/currency_converter
Usage
Basic usage looks like code below:
<?php require __DIR__ . '/vendor/autoload.php'; $money = new CurrencyConverter\Money\Money(1, new CurrencyConverter\Currency\Currency('EUR')); $rateCollection = new \CurrencyConverter\Exchange\RateCollection(); $rateCollection->add(new \CurrencyConverter\Exchange\Rate('EUR', 4.2636)); $rateCollection->add(new \CurrencyConverter\Exchange\Rate('PLN', 1.0000)); $converter = new CurrencyConverter\Converter($rateCollection); $converted = $converter->exchange($money, new CurrencyConverter\Currency\Currency('PLN')); // how many PLN are in 1 EUR echo $converted->getValue();
Using money factory using magic static methods, just call Money::CURRENCY_CODE(float $value):
$money = Money::EUR(1);
In example above we create exchange rates collection manually, but you can use service to get exchange rates from third party services like NBP or ECB.
Service usage:
<?php $exchangeService = new CurrencyConverter\Exchange\Service(new CurrencyConverter\Exchange\Repository\NBPRatesRepository()); $converted = $exchangeService->convert(CurrencyConverter\Money\Money::EUR(4), new CurrencyConverter\Currency('PLN'));
MoneyFormatter
For some cases we need show money value as spelled string like. For that case you can use built-in formatter.
<?php $formatter = \CurrencyConverter\Money\FormatterFactory::create('pl_PL'); $formatter->setMoney(\CurrencyConverter\Money\Money::PLN(20.99)); echo $formatter->spell(); // will return: dwadzieścia złotych dziewięćdziesiąt dziewięć groszy
Of course you are able to change return format, please chect tests for more examples.
Licence
New BSD Licence: https://opensource.org/licenses/BSD-3-Clause