grynchuk/currency

获取货币汇率并执行货币转换

v1.0.8 2024-04-07 13:15 UTC

This package is auto-updated.

Last update: 2024-09-07 14:15:43 UTC


README

提供获取汇率和执行货币金额转换的最小功能(使用php数学计算)。

安装

要安装,请运行

composer require grynchuk/currency 

配置

您可以为获取汇率的可用货币提供配置。为此,您需要添加yaml配置并将其传递给Currency\DataProvider\CurrencyConfigDataProvider。此yaml配置应如下所示

currency:
  items:
    - { code: !php/enum Currency\Enum\Code::USD, precision: 2, description: 'United States Dollar'}

默认情况下,此配置仅包含三种货币 USD EUR PLN

目前没有提供到Symfony的桥梁(可能以后会实现),所以您需要自己实现,更多细节请参阅测试。从Monobank获取汇率的基本Symfony DI配置(symfony/dependency-injection)如下所示

services:
  app.currency.remote.service:
    class: Currency\Service\CurrencyService
    arguments:
      - '@app.currency.repository'
      - '@app.currency.rate.repository'

  app.currency.rate.repository:
    class: Currency\Repository\EntityRepository
    arguments:
      - '@app.currency.rate.provider'


  app.currency.rate.provider:
    class: Currency\DataProvider\Rate\MonobankRateDataProvider
    arguments:
      - '@app.currency.rate.client.monobank'
      - !php/enum Currency\Enum\Code::UAH
      - '@app.currency.rate.dta_mapper.monobank'

  app.currency.rate.dta_mapper.monobank:
    class: Currency\Mapper\Rate\RateDataMapper

  app.currency.rate.client.monobank:
    class: Monobank\MonobankClient
    arguments:
      - '@app.currency.rate.client.guzzle'
      - '@app.currency.rate.client.monobank.empty_token'

  app.currency.rate.client.monobank.empty_token:
    class: Monobank\ValueObject\Token
    arguments:
      - 'empty_token'

  app.currency.rate.client.guzzle:
    class: GuzzleHttp\Client

  app.currency.repository:
    class: Currency\Repository\EntityRepository
    arguments:
      - '@app.currency.data_provider'

  app.currency.data_provider:
    class: Currency\DataProvider\CurrencyConfigDataProvider
    arguments:
      - '@app.currency.config.processor'
      - '@app.currency.config.definition'
      - '@app.currency.mapper'

  app.currency.config.processor:
    class: Symfony\Component\Config\Definition\Processor

  app.currency.config.definition:
    class: Currency\Configuration\CurrencyConfiguration

  app.currency.mapper:
    class: Currency\Mapper\CurrencyMapper

如何使用

要使用核心功能,您需要初始化Currency\Service命名空间中的其中一个服务,然后根据提供的接口使用它。