viaaurea/currency 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

viaaurea/currency

Composer 安装命令:

composer require viaaurea/currency

包简介

Cross-currency money comparison and exchange tool.

README 文档

README

Test Suite PHP from Packagist Powered by

Cross-currency money comparison and exchange tool. Framework agnostic.

💿 composer require viaaurea/currency

📖 Czech/Slovak readme version.

Use this package to do comparison arithmetic or aggregations on values in different currencies or exchange money in one currency to another.

// resolve the service / get an instance
$cs = $container->get(VA\Currency\CurrencyService::class);

// create a money value object (3 equivalent ways)
$cs->create(100, 'USD');
new Money(100, 'USD');
Money::USD(100);

// exchange / conversion
$valueInUsd = Money::USD(100);
$valueInEur = $cs->exchange($valueInUsd, 'EUR');
$valueInEur->amount();

// comparison arithmetic
// < <= > >= == !=
$cs->greaterThan($valueInUsd, $valueInForeignCurrency); // or $cs->gt( ... )

// diff mixed currencies
$diff = $cs->diff($valueInEur, $valueInForeignCurrency);

// aggregate an array of Money objects with unknown or mixed currencies:
// sum, average, max or min value
$money = [ $valueInEur, $valueInUsd, $valueInForeignCurrency, ... ];
$sum = $cs->sum($money);
$max = $cs->max($money);
$min = $cs->min($money);
$avg = $cs->avg($money);

💡
This package is meant as a lightweight tool to exchange, compare and aggregate money value objects.
For a fully fledged robust solution consider using Money for PHP.

Currency Service

CurrencyService is the main service class used for conversions and arithmetics. Its main dependency is an implementation of an exchange rate provider.
During comparison operations or aggregations between money objects with different currencies exchange to one of the currencies must happen.
The provider provides exchange rates for these conversions as well as for explicit conversions using CurrencyService::exchange method.

Currently only one implementation is delivered with this package - the StaticExchange, however, it is trivial to provide your own implementation according to your needs (database, third party API fetches etc.).

Configuration

The exchange rates are defined and passed to the StaticExchange class, which is then used by the CurrencyService. You can also provide your own exchange rate provider simply by implementing the ExchangeRateProviderInterface interface.

The rates are defined in relation to a reference/base/local currency.

For directly quoted reference currencies (USD, CAD, JPY, CZK, ...):

$container->register(CurrencyService::class, function(){
    $rates = [
        'EUR' => 0.9,
        'JPY' => 100,
        'AUD' => 1.5,
    ];
    $provider = new StaticExchange('USD', $rates, StaticExchange::RATE_DIRECT);
    return new CurrencyService($provider);
});

For indirectly quoted reference currencies (EUR, GBP, AUD, ...):

$container->register(CurrencyService::class, function(){
    $rates = [
        'USD' => 1.1,
        'JPY' => 120,
        'AUD' => 1.6,
    ];
    $provider = new StaticExchange('EUR', $rates, StaticExchange::RATE_INDIRECT);
    return new CurrencyService($provider);
});

💡 You are not required to use a service container, in which case just assign the instance to a variable...

It is possible to specify amount of units with the rate. It can be used for currencies where the difference to the base currency is very high (certain African currencies, Bitcoin, etc.). To do that, pass in an array with the rate and amount:

$rates = [
    // rate for Bitcoin may be specified in micro-Bitcoins:
    'XBT' => [0.5, 0.001],
];

Usage

See the examples at the beginning of this file above and see the source code of CurrencyService class for full list of available methods.

Fine-grain exchanges

The exchange method, as well as every comparison or aggregation method of CurrencyService accepts a variadic list of arguments that is passed down to the calls to underlying exchange rate provider. This can be leveraged to enable calculation of historic currency values, buy/sell/middle rates and so on.

Examples:

$cs->exchange(
    $cs->create(100, 'EUR'), 
    'USD', 
    Carbon::parse('last saturday')
);

$cs->diff(
    $cs->create(100, 'EUR'),
    $cs->create(100, 'USD'),
    Carbon::parse('2017-11-23'),
    'buy'
);

To use this functionality, a custom provider needs to be implemented, example:

class DatabaseRateProvider implements ExchangeRateProviderInterface {

    function getExchangeRate(
        Currency $target, 
        Currency $from, 
        DateTimeInterface $at, 
        string $direction = 'middle'
    ){
        // your logic here
        // return exchange rate at a given point in time and for the specified direction
    }

}

Tip: use the ExchangeHelper class to easily implement a custom provider.

Nette DI extension

Contains an extension for the Nette framework. See the Czech/Slovak readme version.

Contributions

... are welcome.

viaaurea/currency 适用场景与选型建议

viaaurea/currency 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.18k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 viaaurea/currency 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 viaaurea/currency 我们能提供哪些服务?
定制开发 / 二次开发

基于 viaaurea/currency 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-01-02