orkhanahmadov/cbar-currency

PHP library to work with cbar.az currency rates

1.5.0 2022-06-15 06:43 UTC

This package is auto-updated.

Last update: 2024-11-15 11:55:40 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads GitHub license

Build Status Test Coverage Maintainability Quality Score StyleCI

Requirements

PHP 7.2 or higher, simplexml and bcmath extensions.

Installation

composer require orkhanahmadov/cbar-currency

Usage

Fetching rates from CBAR

Instantiate Orkhanahmadov\CBARCurrency\CBAR with date you want to fetch rates for. If you don't pass a date, current date will be used:

use Orkhanahmadov\CBARCurrency\CBAR;

$cbar = new CBAR(); // this will fetch rates for current date
$cbar = new CBAR('01.05.2019'); // this will fetch rates for 01.05.2019

You can get currency rate by accessing it with with uppercase currency code:

$cbar->EUR; // returns EUR rate

You can change date for a new date by calling for() method without instantiating new class:

$cbar->for('25.04.2019'); // this will fetch rates for 25.04.2019
$cbar->USD; // returns USD rate for 25.04.2019

You can pass dates in any format that acceptable by PHP's strtotime() function. For example, 20.10.2019, 10/20/2019, 2019-10-20, today, yesterday, -1 week, -1 year, 15 December 2015, last Friday.

You can fetch currency rates for multiple dates with same class instance. Class instance fetches rates for each unique date only once and stores results for each date. If you set date to previously fetched date, stored rates will be used.

$cbar = new CBAR();
$cbar->for('20.04.2019'); // this will fetch rates from CBAR API
$cbar->for('23.04.2019'); // this will also fetch rates from CBAR API
$cbar->for('20.04.2019'); // since rates for 20.04.2019 fetched previously stored rates will be used instead of fetching rates for same day again

You can chain methods with fluent API syntax:

$cbar = new CBAR();
$cbar->for('yesterday')->EUR;

All available currencies and currency codes can be found in CBAR website

Converting amount to and from AZN

Library supports converting given amount in foreign currency to AZN with given date's rates:

$cbar = new CBAR();
$cbar->USD(13); // returns AZN equivalent of 13.00 USD with today's rates
$cbar->for('01.05.2019')->USD(57.5); // returns AZN equivalent of 57.50 USD with 01.05.2019 rates

You can also convert given amount in AZN to any available foreign currency:

$cbar = new CBAR();
$cbar->AZN()->USD; // returns USD equivalent of 1.00 AZN with today's rates
$cbar->AZN(55)->USD; // returns USD equivalent of 55.00 AZN with today's rates
$cbar->for('01.05.2019')->AZN(17.3)->USD; // returns USD equivalent of 17.30 AZN with 01.05.2019 rates

Helper function

Library ships with global helper function. You can use it like:

cbar()->USD // returns USD rate for today
cbar('01.05.2019')->USD; // returns USD rate for 01.05.2019
cbar()->for('01.05.2019')->EUR; // same as above
cbar()->USD(27); // returns 27.00 USD to AZN conversion
cbar()->AZN(15.8)->EUR; // returns 15.80 AZN to EUR conversion

Note: Calling cbar() global function always returns new instance of Orkhanahmadov\CBARCurrency\CBAR class.

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 ahmadov90@gmail.com instead of using the issue tracker.

Credits

License

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