plugowski/currency_converter

There is no license information available for the latest version (1.1) of this package.

1.1 2016-08-13 19:41 UTC

This package is not auto-updated.

Last update: 2024-04-27 23:08:15 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