pavlepredic/currency-converter
Composer 安装命令:
composer require pavlepredic/currency-converter
包简介
Tools for performing currency conversion
README 文档
README
Tools for performing currency conversion. This library might be used with Symfony2 framework, but this is not a requirement. You can use the basic exchange rate conversion utility with vanilla PHP. In order to use the storage layer, you will need Doctrine. This library is a work in progress.
Installing
composer require pavlepredic/currency-converter
Basic usage example
use PavlePredic\CurrencyConverter\Service\CurrencyConverter;
$converter = new CurrencyConverter();
$converted = $converter->convert(100, 'USD', 'EUR');
Storing exchange rates to DB using Doctrine
-
Create an ORM Entity that implements PavlePredic\CurrencyConverter\Entity\ExchangeRateInterface
-
Implement PavlePredic\CurrencyConverter\Repository\ExchangeRateRepositoryInterface. The repository might look something like this:
<?php
namespace AppBundle\Repository;
use AppBundle\Entity\ExchangeRate;
use Doctrine\ORM\EntityRepository;
use PavlePredic\CurrencyConverter\Entity\ExchangeRateInterface;
use PavlePredic\CurrencyConverter\Repository\ExchangeRateRepositoryInterface;
class ExchangeRateRepository extends EntityRepository implements ExchangeRateRepositoryInterface
{
/**
* @param string $sourceCurrency
* @param array $destinationCurrencies
* @return ExchangeRateInterface[]
*/
public function findAllBySourceCurrencyAndDestinationCurrencies($sourceCurrency, array $destinationCurrencies)
{
return $this->findBy([
'sourceCurrency' => $sourceCurrency,
'destinationCurrency' => $destinationCurrencies,
]);
}
/**
* @param string $sourceCurrency
* @param string $destinationCurrency
* @return ExchangeRateInterface
*/
public function findOneBySourceCurrencyAndDestinationCurrency($sourceCurrency, $destinationCurrency)
{
return $this->findOneBy([
'sourceCurrency' => $sourceCurrency,
'destinationCurrency' => $destinationCurrency,
]);
}
/**
* @param string $sourceCurrency
* @param string $destinationCurrency
* @param float $rate
* @return ExchangeRateInterface
*/
public function update($sourceCurrency, $destinationCurrency, $rate)
{
$exchangeRate = $this->findOneBySourceCurrencyAndDestinationCurrency($sourceCurrency, $destinationCurrency);
if (!$exchangeRate) {
$exchangeRate = ExchangeRate::create($sourceCurrency, $destinationCurrency, $rate);
$this->getEntityManager()->persist($exchangeRate);
}
$exchangeRate->setRate($rate);
return $exchangeRate;
}
}
- Update the exchange rates using CurrencyUpdater service:
//the following might be executed in a symfony command, but is not restricted to symfony in any way
//you only need to use Doctrine EntityManager and EntityRepository
$em = $this->getContainer()->get('doctrine.orm.default_entity_manager');
$repo = $em->getRepository('AppBundle:ExchangeRate');
$supportedCurrencies = ['USD','EUR','GBP'];
$updater = new CurrencyUpdater($repo, new FixerExchangeRatesProvider(), $supportedCurrencies);
$updater->update();
$em->flush();
- Now you can use the CurrencyConverter service using DbExchangeRatesProvider
use PavlePredic\CurrencyConverter\Provider\DbExchangeRatesProvider;
use PavlePredic\CurrencyConverter\Service\CurrencyConverter;
$em = $this->getContainer()->get('doctrine.orm.default_entity_manager');
$repo = $em->getRepository('AppBundle:ExchangeRate');
$provider = new DbExchangeRatesProvider($repo);
$converter = new CurrencyConverter($provider);
$converted = $converter->convert(100, 'USD', 'EUR');
pavlepredic/currency-converter 适用场景与选型建议
pavlepredic/currency-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 76 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 pavlepredic/currency-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pavlepredic/currency-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 76
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2015-10-13