lexik/currency-bundle

此Symfony2扩展包提供了一种服务和Twig扩展,用于转换和显示货币。

安装次数: 227 173

依赖关系: 3

建议者: 0

安全性: 0

星星: 58

关注者: 10

分支: 39

开放问题: 10

类型:symfony-bundle

v2.1.0 2016-08-19 09:00 UTC

This package is auto-updated.

Last update: 2024-09-06 23:21:07 UTC


README

此Symfony2扩展包提供了一种服务和twig扩展,用于转换和显示货币。

Build Status Latest Stable Version SensioLabsInsight

安装

将扩展包添加到您的 composer.json 文件中

require: {
    // ...
    "lexik/currency-bundle": "~2.0"
    // ...
}

从版本 1.2.0 开始,currency_format 不再进行货币转换,它只根据区域设置格式化给定的值。如果您需要转换和格式化值,请使用 currency_convert_format 过滤器。

然后运行 composer update

composer.phar update
# OR
composer.phar update lexik/currency-bundle # to only update the bundle

在您的内核中注册扩展包

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Lexik\Bundle\CurrencyBundle\LexikCurrencyBundle(),
    // ...
);

配置

最小配置

# app/config/config.yml
lexik_currency:
    currencies:
        default: EUR              # [required] the default currency
        managed: [EUR, USD, ...]  # [required] all currencies used in your app

附加选项(默认值如下所示)

# app/config/config.yml
lexik_currency:
    decimal_part:
        precision:  2                           # number of digits for the decimal part
        round_mode: up                          # round mode to use (up|down|even|odd)
	currency_class: Lexik\Bundle\CurrencyBundle\Entity\Currency  # Use your custom Currency Entity
    default_adapter: doctrine_currency_adapter  # service id OR tag alias, this is adapter used by the conversion service

初始化货币

要初始化数据库中的货币汇率,请运行以下命令

./app/console lexik:currency:import <currency adapter identifier>

示例:使用ECB适配器,从欧洲中央银行获取汇率。在命令行中,ecb 是适配器类 getIdentifier() 方法返回的值。

./app/console lexik:currency:import ecb

用法

货币转换服务

使用 lexik_currency.converter 服务中的 convert() 方法

<?php
// by default the amount will rounded and the amount have to be in the default currency
$convertedAmount = $container->get('lexik_currency.converter')->convert($amount, $targetCurrency);

// here the amount won't be rounded and we specify that $amount currency is 'USD'
$convertedAmount = $container->get('lexik_currency.converter')->convert($amount, $targetCurrency, false, 'USD');
检索管理配置

在控制器中,您可以使用以下行检索所有管理货币的数组

$managedCurrencies = $this->container->getParameter('lexik_currency.currencies.managed');
Twig过滤器

此扩展包提供3个过滤器来转换和格式化值

  • currency_convert:转换值。
  • currency_format:根据当前区域设置格式化值。
  • currency_convert_format:转换和格式化值。

以下是一个使用 currency_convert_format 过滤器的示例。

{% set targetCurrency = 'EUR' %}
{{ amount | currency_convert_format(targetCurrency) }}

您还可以传递更多参数,以显示或隐藏小数和货币符号。如果需要,您还可以指定金额的货币。

{% set targetCurrency = 'EUR' %}
{% set amountCurrency = 'USD' %}
{% set decimal = false %}
{% set symbol = true %}

{{ amount | currency_convert_format(targetCurrency, decimal, symbol, amountCurrency) }}
从其他来源(自定义CurrencyAdatpter)加载转换汇率

如果您需要从其他来源加载转换汇率,您将必须创建一个CurrencyAdatpter并将其设置为默认适配器。

要创建您自己的适配器,您将必须扩展 Lexik\Bundle\CurrencyBundle\Adapter\AbstractCurrencyAdapter,它定义了两个抽象方法

  • getIdentifier():返回适配器的标识符。
  • attachAll():加载带有其汇率的货币(此方法由导入命令调用以获取要保存到数据库中的所有货币)。

以下是一个示例

<?php

namespace MyProject\With\Some\Rainbows;

use Lexik\Bundle\CurrencyBundle\Adapter\AbstractCurrencyAdapter;

class RainbowCurrencyAdapter extends AbstractCurrencyAdapter
{
	/**
     * {@inheritdoc}
     */
    public function attachAll()
    {
    	$defaultRate = 1;

        // Add default currency (euro in this example)
        $euro = new $this->currencyClass;
        $euro->setCode('EUR');
        $euro->setRate($defaultRate);

        $this[$euro->getCode()] = $euro;

        // Get other currencies
        $currencies = // get all currencies with their rate (from a file, an url, etc)

        foreach ($currencies as $code => $rate) {
            if (in_array($code, $this->managedCurrencies)) { // you can check if the currency is in the managed currencies
                $currency = new $this->currencyClass;
                $currency->setCode($code);
                $currency->setRate($rate);

                $this[$currency->getCode()] = $currency;
            }
        }

        // get the default rate from the default currency defined in the configuration
        if (isset($this[$this->defaultCurrency])) {
            $defaultRate = $this[$this->defaultCurrency]->getRate();
        }

        // convert rates according to the default one.
        $this->convertAll($defaultRate);
    }

    /**
     * {@inheritdoc}
     */
    public function getIdentifier()
    {
        return 'rainbow';
    }
}

然后定义适配器作为服务,不要忘记 lexik_currency.adapter 标签

<service id="my_project.rainbow_currency_adapter" class="MyProject\With\Some\Rainbows\RainbowCurrencyAdapter">
    <call method="setDefaultCurrency">
        <argument>%lexik_currency.currencies.default%</argument>
    </call>
    <call method="setManagedCurrencies">
        <argument>%lexik_currency.currencies.managed%</argument>
    </call>
    <call method="setCurrencyClass">
        <argument>%lexik_currency.currency_class%</argument>
    </call>
    <tag name="lexik_currency.adapter" alias="rainbow_currency_adapter" />
</service>

并使用您的适配器导入货币

./app/console lexik:currency:import rainbow