定制 pavlepredic/currency-converter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

pavlepredic/currency-converter

最新稳定版本:0.0.0

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');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2015-10-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固