2017 © Pedro Peláez
 

library bnm-rates

Library used to get official exchange rates of National bank of Moldova

image

osoian/bnm-rates

Library used to get official exchange rates of National bank of Moldova

  • Sunday, February 25, 2018
  • by osoianmarcel
  • Repository
  • 1 Watchers
  • 2 Stars
  • 84 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 5 % Grown

The README.md

bnm-rates

PHP library used to get official exchange rates of National bank of Moldova., (*1)

Build Status Latest Stable Version Minimum PHP Version license, (*2)

Installation

Use Composer to install the package:, (*3)

$ composer require osoian/bnm-rates

Examples

Here an example of how to use the library:, (*4)

<?php

use Osoian\BnmRates\BnmModel;
use Osoian\BnmRates\BnmRates;

$instance = new BnmRates();

/*
 * Get exchange rate
 */
$rate = $instance->getOne('EUR');
if ($rate) {
    var_dump([
        'value' => $rate->getValue(), // float(20.9869)
        'code' => $rate->getCode(), // string(3) "EUR"
        'name' => $rate->getName() // string(4) "Euro"
    ]);
}

/*
 * What if we need only the value?
 */
$rate = $instance->getOne('EUR', true);
if ($rate) {
    var_dump($rate); // float(20.9869)
}

/*
 * What is we need multiple exchange rates?
 */
$rates = $instance->getMultiple(['EUR', 'USD', 'RON']);
if (!empty($rates)) {
    foreach ($rates as $key => $rate) {
        var_dump([
            'key' => $key, // 0, 1, 2
            'value' => $rate->getValue(), // float(20.9869), float(19.2567), float(4.6349)
            'code' => $rate->getCode(), // string(3) "EUR", string(3) "USD", string(3) "RON"
            'name' => $rate->getName() // string(4) "Euro", string(9) "US Dollar", string(12) "Romanian Leu"
        ]);
    }
}

/*
 * What if we want to use assoc array?
 */
$instance->setAssocResult(true);
$rates = $instance->getMultiple(['EUR', 'USD', 'RON']);
if (!empty($rates)) {
    foreach ($rates as $key => $rate) {
        var_dump($key); // string(3) "EUR", string(3) "USD", string(3) "RON"
    }
}

/*
 * What if we need to use specific array key?
 */
$instance->setAssocClosure(function ($rate) {
    /**
     * @var BnmModel $rate
     */
    return $rate->getNumber() . '-' . $rate->getCode();
});
$rates = $instance->getMultiple(['EUR', 'USD', 'RON']);
if (!empty($rates)) {
    foreach ($rates as $key => $rate) {
        var_dump($key); // string(7) 978-EUR, string(7) "840-USD", string(7) "946-RON"
    }
}

/*
 * What if we call getters multiple times?
 */
$instance->setDate(new \DateTime('yesterday'));
// First call (no cache), request bnm.md web server
$st = microtime(true);
$instance->getOne('EUR');
$elapsed = microtime(true) - $st;
echo $elapsed . ' sec' . PHP_EOL; // 0.065252065658569 sec
// Second call (with cache), use cached results
$st = microtime(true);
$instance->getOne('USD');
$instance->getMultiple(['EUR', 'USD', 'RON']);
$elapsed = microtime(true) - $st;
echo $elapsed . ' sec' . PHP_EOL;// 2.6941299438477E-5 sec
// Yes, results are cached!

/*
 * What if we need results in other languages?
 */
$instance->setLocale('ro'); // Romanian language
$rate = $instance->getOne('RON');
if ($rate) {
    var_dump($rate->getName()); // string(12) "Leu romanesc"
}
$instance->setLocale('ru'); // Russian language
$rate = $instance->getOne('RON');
if ($rate) {
    var_dump($rate->getName()); // string(25) "Румынский Лей"
}

/*
 * What if we need to get all exchange rates?
 */
$rates = $instance->getAll();
if (!empty($rates)) {
    var_dump(count($rates)); // int(42)
    foreach ($rates as $key => $rate) {
        var_dump([
            'key' => $key, // string(7) "978-EUR", string(7) "840-USD", string(7) "643-RUB"
            'toString' => (string)$rate // string(9) "20.99 EUR", string(9) "19.26 USD", string(8) "0.34 RUB"
        ]);
    }
}

For Symfony users

Define the service and set default language (ro):, (*5)

osoian.bnm_rates:
        class: Osoian\BnmRates\BnmRates
        calls:
            - [ setLocale, ['ro'] ] # Optional config

Now you can call it:, (*6)

$this->get('osoian.bnm_rates')
    ->setDate(new \DateTime('yesterday'))
    ->getOne('EUR', true); // float(20.9869)

Testing

bash $ composer test, (*7)

Contribute

Contributions to the package are always welcome!, (*8)

License

All contents of this package are licensed under the MIT license., (*9)

The Versions

25/02 2018

dev-master

9999999-dev https://github.com/OsoianMarcel/bnm-rates

Library used to get official exchange rates of National bank of Moldova

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Osoian Marcel

currency bank rates national bnm moldova

23/08 2017

v1.0.1

1.0.1.0 https://github.com/OsoianMarcel/bnm-rates

Library used to get official exchange rates of National bank of Moldova

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Osoian Marcel

currency bank rates national bnm moldova

01/05 2017

v1.0.0

1.0.0.0 https://github.com/OsoianMarcel/bnm-rates

Library used to get official exchange rates of National bank of Moldova

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Osoian Marcel

currency bank rates national bnm moldova