承接 osoian/bnm-rates 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

osoian/bnm-rates

最新稳定版本:v1.0.1

Composer 安装命令:

composer require osoian/bnm-rates

包简介

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

README 文档

README

PHP library used to get official exchange rates of National bank of Moldova.

Build Status Latest Stable Version Minimum PHP Version license

Installation

Use Composer to install the package:

$ composer require osoian/bnm-rates

Examples

Here an example of how to use the library:

<?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):

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

Now you can call it:

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

Testing

$ composer test

Contribute

Contributions to the package are always welcome!

License

All contents of this package are licensed under the MIT license.

统计信息

  • 总下载量: 256
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-01