savritsky/currency-rate-bundle

Symfony5+ 货币汇率包,允许通过 cbr 或 ecb 提供商进行货币转换和加载汇率

0.4 2023-01-12 11:09 UTC

This package is not auto-updated.

Last update: 2024-09-16 15:49:13 UTC


README

启动步骤

  1. 将模块安装到您的应用中 从 Packagist
  2. 将包添加到内核
$bundles = array(
    ...
    new \RedCode\CurrencyRateBundle\RedCodeCurrencyRateBundle(),
    ...
);

3. 创建 Currency 和 CurrencyRate 类

/**
* @ORM\Entity
*/
class Currency extends \RedCode\CurrencyRateBundle\Entity\Currency
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var string
     */
    protected $code;
}
/**
 * @ORM\Entity
 */
class CurrencyRate extends \RedCode\CurrencyRateBundle\Entity\CurrencyRate
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @var \DateTime
     */
    protected $date;

    /**
     * @var int
     */
    protected $nominal;

    /**
     * @var float
     */
    protected $rate;

    /**
     * @var \RedCode\Currency\ICurrency
     * @ORM\ManyToOne(targetEntity="Currency")
     * @ORM\JoinColumn(name="currency_id", referencedColumnName="id")
     */
    protected $currency;

    /**
     * @var string
     */
    protected $providerName;
}

4. 在 config.yml 文件中添加部分

redcode_currency_rate:
    currency_rate_class: NameSpasePath\CurrencyRate
    currency_class: NameSpasePath\Currency

5. 只需运行命令

a. 创建基础货币

./app/console redcode:create:base:currencies

b. 创建加载货币汇率

./app/console redcode:currency:rate:load

6. 现在,您可以通过名称调用货币汇率转换器 - redcode.currency.rate.converter

$converter = $container->get('redcode.currency.rate.converter');
$convertedValue = $converter->convert('USD', 'EUR', $value);

贡献

欢迎拉取请求。请参阅我们的 CONTRIBUTING 指南。