pavlepredic/currency-converter

货币转换工具

0.0.0 2015-10-13 12:23 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:07:17 UTC


README

货币转换工具。这个库可能与Symfony2框架一起使用,但这不是必需的。你可以使用基本的汇率转换工具配合纯PHP。为了使用存储层,你需要Doctrine。这个库还在开发中。

安装

composer require pavlepredic/currency-converter

基本用法示例

use PavlePredic\CurrencyConverter\Service\CurrencyConverter;

$converter = new CurrencyConverter();
$converted = $converter->convert(100, 'USD', 'EUR');

使用Doctrine将汇率存储到数据库

  • 创建一个实现PavlePredic\CurrencyConverter\Entity\ExchangeRateInterface的ORM实体

  • 实现PavlePredic\CurrencyConverter\Repository\ExchangeRateRepositoryInterface。仓库可能看起来像这样

<?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;
    }

}
  • 使用CurrencyUpdater服务更新汇率
//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();
  • 现在你可以使用DbExchangeRatesProvider使用CurrencyConverter服务
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');