https://github.com/osoianmarcel/bnm-rates
PHP library used to get official exchange rates of National bank of Moldova
https://github.com/osoianmarcel/bnm-rates
bank bnm composer currency curs exchange-rates moldova national-bank php valutar
Last synced: 6 months ago
JSON representation
PHP library used to get official exchange rates of National bank of Moldova
- Host: GitHub
- URL: https://github.com/osoianmarcel/bnm-rates
- Owner: OsoianMarcel
- License: mit
- Created: 2017-05-01T08:11:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-25T14:07:04.000Z (over 3 years ago)
- Last Synced: 2025-04-12T10:07:15.121Z (6 months ago)
- Topics: bank, bnm, composer, currency, curs, exchange-rates, moldova, national-bank, php, valutar
- Language: PHP
- Size: 12.7 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# bnm-rates
PHP library used to get official exchange rates of National bank of Moldova.[](https://app.travis-ci.com/OsoianMarcel/bnm-rates)
[](https://packagist.org/packages/osoian/bnm-rates)
[](https://php.net/)
[](https://github.com/OsoianMarcel/bnm-rates/blob/master/LICENSE)## Installation
Use [Composer] to install the package:
```
$ composer require osoian/bnm-rates
```## Examples
Here an example of how to use the library:
```php
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):
```yaml
osoian.bnm_rates:
class: Osoian\BnmRates\BnmRates
calls:
- [ setLocale, ['ro'] ] # Optional config
```Now you can call it:
```php
$this->get('osoian.bnm_rates')
->setDate(new \DateTime('yesterday'))
->getOne('EUR', true); // float(20.9869)
```## Testing
``` bash
$ composer test
```## Contribute
Contributions to the package are always welcome!
* Report any bugs or issues you find on the [issue tracker].
* You can grab the source code at the package's [Git repository].## License
All contents of this package are licensed under the [MIT license].
[Composer]: https://getcomposer.org
[issue tracker]: https://github.com/OsoianMarcel/bnm-rates/issues
[Git repository]: https://github.com/OsoianMarcel/bnm-rates
[MIT license]: LICENSE