/nbp-currency-converter

Retrieve average currency exchange rates from the NBP website and convert an amount from one currency to another

Primary LanguagePHPMIT LicenseMIT

NBP currency converter

Latest Version on Packagist Software License Build Status Coverage Status Quality Score

Retrieve average currency exchange rates from the NBP website and convert an amount from one currency to another.

Install

Via Composer

$ composer require ksdev/nbp-currency-converter

Usage

If optional table publication date is not passed to convert or averageExchangeRates methods, the last published average exchange rates table will be used, starting from yesterday, so e.g. on 2015-07-10 (friday) a table from 2015-07-09 (thursday) will be used, and on 2015-07-12 (sunday) a table from 2015-07-10 (friday) - unless no table was published on this day, then the table from previous day will be used, etc.

use Ksdev\NBPCurrencyConverter\CurrencyConverter;
use Ksdev\NBPCurrencyConverter\ExRatesTableFinder;
use Ksdev\NBPCurrencyConverter\ExRatesTableFactory;
use GuzzleHttp\Client;

$converter = new CurrencyConverter(
    new ExRatesTableFinder(
        new Client(),
        new ExRatesTableFactory(),
        'path/to/cache/folder'
    )
);
try {
    $result = $converter->convert('123.4567', 'PLN', 'USD');
    $avgExRates = $converter->averageExchangeRates();
}
catch (Exception $e) {
    //
}
$result
array(
    'publication_date' => '2015-07-03',
    'amount'           => '32.7246',
    'currency'         => 'USD'
);
$avgExRates
array(
    'numer_tabeli'    => '127/A/NBP/2015',
    'data_publikacji' => '2015-07-03',
    'waluty'          =>
        array(
            'PLN' =>
                array(
                    'nazwa_waluty' => 'złoty polski',
                    'przelicznik'  => '1',
                    'kurs_sredni'  => '1',
                ),
            'THB' =>
                array(
                    'nazwa_waluty' => 'bat (Tajlandia)',
                    'przelicznik'  => '1',
                    'kurs_sredni'  => '0,1117',
                ),
            'USD' =>
                array(
                    'nazwa_waluty' => 'dolar amerykański',
                    'przelicznik'  => '1',
                    'kurs_sredni'  => '3,7726',
                ),
            'AUD' =>
                array(...)
        )
);

Testing

$ composer test

License

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