扭伤 / 货币转换器
一个用于处理货币转换的php库
0.2
2015-04-20 07:15 UTC
Requires
- php: >=5.4.3
- symfony/intl: ~2.6
- symfony/validator: ~2.6
Requires (Dev)
- doctrine/cache: ~1.4
- kriswallsmith/buzz: ~0.13
Suggests
- doctrine/cache: To use caching in some providers
- kriswallsmith/buzz: To use FixerIoProvider or SwissGovernmentProvider
This package is auto-updated.
Last update: 2024-08-29 04:31:07 UTC
README
SprainCurrencyConverter 是一个用于货币转换的php库。它通过内置或自定义提供者实现极大的灵活性。
安装
将 SprainCurrencyConverter 添加到 composer.json
{
"require": {
"sprain/currency-converter": "~0.1"
}
}
现在运行以下命令让 composer 下载程序包
$ php composer.phar update sprain/currency-converter
用法
<?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();
提供者
包含的提供者
-
FixerIoProvider
由 fixer.io 提供的汇率,该数据来源于欧洲中央银行。
支持的货币: 没有定义限制 -
SwissGovernmentProvider
由瑞士政府提供的汇率。请参阅 www.estv.admin.ch。
支持的货币: CHF -
YahooProvider
由 finance.yahoo.com 提供的汇率。
支持的货币: 没有定义限制
创建自定义提供者
SprainCurrencyConverter 允许您轻松添加自己的自定义提供者。如果您例如在数据库中本地保存汇率,这会很有帮助。
很简单
<?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'); } }
自定义提供者中的缓存
如果您的提供者使用远程API或其他需要一些时间来获取数据的方法,您可能想缓存汇率。
为此,只需扩展 AbstractCacheableProvider
而不是 AbstractProvider
<?php namespace Acme\Your\Project; use Sprain\CurrencyConverter\Provider\Abstracts\AbstractCacheableProvider; class MyCacheableCustomProvider extends AbstractCacheableProvider { ... same, same as above }
如果将缓存注入到 CurrencyConverter
,现在您的提供者获取的每个汇率将自动缓存24小时。
许可
此库采用 MIT 许可证。请参阅完整的许可协议
Resources/meta/LICENSE