sprain/currency-converter
Composer 安装命令:
composer require sprain/currency-converter
包简介
A php library to handle currency conversion
关键字:
README 文档
README
SprainCurrencyConverter is a php library to convert currencies. It allows great flexibility by use of built-in or custom providers.
Installation
Add SprainCurrencyConverter to your composer.json:
{
"require": {
"sprain/currency-converter": "~0.1"
}
}
Now tell composer to download the bundle by running the command:
$ php composer.phar update sprain/currency-converter
Usage
<?php include 'vendor/autoload.php'; // Create an instance $converter = new \Sprain\CurrencyConverter\CurrencyConverter(); // Optional, but recommended: add a cache which will be used by some of the providers. // You can use any cache system supported by Doctrine Cache. $cache = new \Doctrine\Common\Cache\FilesystemCache(sys_get_temp_dir()); $converter->setCache($cache); // Add at least one provider with its dependencies $browser = new Buzz\Browser(); $converter->addProvider(new \Sprain\CurrencyConverter\Provider\FixerIoProvider($browser)); // Optional: Add more providers. The converter will then loop over the providers until // a result is found. Use the second parameter to define the provider's priority. // Higher values mean higher priority. The highest priority provider will be checked before all others. $converter->addProvider(new \Sprain\CurrencyConverter\Provider\SwissGovernmentProvider($browser), 255); // Do a quick conversion $convertedAmount = $converter->convert(100)->from('USD')->to('EUR')->quick(); // Or alternatively, get a conversion object which contains more data $conversion = $converter->convert(100)->from('USD')->to('EUR')->getConversion(); $convertedAmount = $conversion->getAmountInTargetCurrency(); $exchangeRate = $conversion->getExchangeRate(); $successfulProvider = $conversion->getProvider();
Providers
Included providers
-
FixerIoProvider
Exchange rates provided by fixer.io, which gets its data from the European Central Bank.
Supported currencies: No restrictions defined -
SwissGovernmentProvider
Exchange rates provided by the Swiss government. See www.estv.admin.ch.
Supported currencies: CHF -
YahooProvider
Exchange rates provided by finance.yahoo.com.
Supported currencies: No restrictions defined
Create your own custom provider
SprainCurrencyConverter easily allows you to add your own custom provider. This is helpful if for instance you keep exchange rates locally in a database.
It's simple:
<?php namespace Acme\Your\Project; use Sprain\CurrencyConverter\Provider\Abstracts\AbstractProvider; // Simply extend the AbstractProvider which comes with SprainCurrencyConverter class MyCustomProvider extends AbstractProvider { public function doGetExchangeRate() { $baseCurrency = $this->getBaseCurrency(); $targetCurrency = $this->getTargetCurrency(); // do your thing and return the exchange rate which represents the // value of 1 unit of the base currency in units of the target currency. // ... return $exchangeRate; } // Return your provider's name. public function getName() { return 'My custom provider'; } // Optional: // Add this method and return an array of supported currencies. // The provider will only be used if either base currency _or_ target currency // is a member of this array. public function getSupportedCurrencies() { return array('USD', 'EUR'); } }
Caching in custom providers
If your provider uses a remote api or any other method which takes some time to get data, you might want to cache the exchange rate.
To achieve this, simply extend AbstractCacheableProvider instead of AbstractProvider:
<?php namespace Acme\Your\Project; use Sprain\CurrencyConverter\Provider\Abstracts\AbstractCacheableProvider; class MyCacheableCustomProvider extends AbstractCacheableProvider { ... same, same as above }
If a cache was injected into CurrencyConverter, every exchange rate fetched by your provider will now automatically be cached for 24 hours.
License
This library is under the MIT license. See the complete license in:
Resources/meta/LICENSE
sprain/currency-converter 适用场景与选型建议
sprain/currency-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 2, 最近一次更新时间为 2015 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「money」 「currency」 「conversion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sprain/currency-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sprain/currency-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sprain/currency-converter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
Fixer.io data provider for Peso
TCMB Currenct Converter
Color conversion library
Convert and serve jpeg/png to webp with PHP (if at all possible)
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-03-15