rebilly/money

货币值对象实现

v2.1.0 2024-03-01 08:47 UTC

This package is auto-updated.

Last update: 2024-08-30 11:22:18 UTC


README

Software License Latest Version on Packagist GitHub Actions status

Money

值对象,表示使用货币的最小单位表示的货币价值。这个库最初是基于 Sebastian Bergmann 的 已废弃的库。它已更新,增加了对货币兑换的支持。

安装

如果您使用 Composer 来管理项目的依赖项,只需将 rebilly/money 添加到项目的 composer.json 文件中即可。

使用示例

创建一个 Money 对象并访问其货币价值

use Money\Currency;
use Money\Money;

// Create Money object that represents 1 EUR
$m = new Money(100, new Currency('EUR'));

// Access the Money object's monetary value
print $m->getAmount();

// Access the Money object's monetary value converted to its base units
print $m->getConvertedAmount();

上面的代码会产生以下输出

100

1.00

从字符串值创建一个 Money 对象

use Money\Currency;
use Money\Money;

// Create Money object that represents 12.34 EUR
$m = Money::fromString('12.34', new Currency('EUR'))

// Access the Money object's monetary value
print $m->getAmount();

上面的代码会产生以下输出

1234

使用 Money 对象进行基本算术运算

use Money\Currency;
use Money\Money;

// Create two Money objects that represent 1 EUR and 2 EUR, respectively
$a = new Money(100, new Currency('EUR'));
$b = new Money(200, new Currency('EUR'));

// Negate a Money object
$c = $a->negate();
print $c->getAmount();

// Calculate the sum of two Money objects
$c = $a->add($b);
print $c->getAmount();

// Calculate the difference of two Money objects
$c = $b->subtract($a);
print $c->getAmount();

// Multiply a Money object with a factor
$c = $a->multiply(2);
print $c->getAmount();

上面的代码会产生以下输出

-100
300
100
200

如果认为一个 Money 对象的值分别小于、等于或大于另一个 Money 对象的值,则 compareTo() 方法返回一个小于、等于或大于零的整数。

您可以使用 compareTo() 方法使用 PHP 内置排序函数对 Money 对象数组进行排序

use Money\Currency;
use Money\Money;

$m = array(
    new Money(300, new Currency('EUR')),
    new Money(100, new Currency('EUR')),
    new Money(200, new Currency('EUR'))
);

usort(
    $m,
    function ($a, $b) { return $a->compareTo($b); }
);

foreach ($m as $_m) {
    print $_m->getAmount() . "\n";
}

上面的代码会产生以下输出

100
200
300

将表示为 Money 对象的货币价值分配给 N 个目标

use Money\Currency;
use Money\Money;

// Create a Money object that represents 0,99 EUR
$a = new Money(99, new Currency('EUR'));

foreach ($a->allocateToTargets(10) as $t) {
    print $t->getAmount() . "\n";
}

上面的代码会产生以下输出

10
10
10
10
10
10
10
10
10
9

使用比率列表将表示为 Money 对象的货币价值进行分配

use Money\Currency;
use Money\Money;

// Create a Money object that represents 0,05 EUR
$a = new Money(5, new Currency('EUR'));

foreach ($a->allocateByRatios(array(3, 7)) as $t) {
    print $t->getAmount() . "\n";
}

上面的代码会产生以下输出

2
3

从表示为 Money 对象的货币价值中提取百分比(和子总账)

use Money\Currency;
use Money\Money;

// Create a Money object that represents 100,00 EUR
$original = new Money(10000, new Currency('EUR'));

// Extract 21% (and the corresponding subtotal)
$extract = $original->extractPercentage(21);

printf(
    "%d = %d + %d\n",
    $original->getAmount(),
    $extract['subtotal']->getAmount(),
    $extract['percentage']->getAmount()
);

上面的代码会产生以下输出

10000 = 8265 + 1735

请注意,这将从包含百分比的货币价值中提取百分比。如果您想获取货币价值的百分比,应使用乘法(例如,使用 multiply(0.21) 来计算表示为 Money 对象的货币价值的 21%)。

使用转换率将 Money 对象的货币转换为另一个 Money 对象

use Money\Currency;
use Money\Money;

// Create a Money object that represents 100,00 EUR
$original = new Money(10000, new Currency('EUR'));

$converted = $original->convert(new Currency('USD'), 1.09, PHP_ROUND_HALF_UP);
$converted->getConvertedAmount(); // 109.00

使用比率对象进行货币转换

$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$rate = new Rate($cp, new DateTime(), 0.92);

$money = new Money(1000, new Currency('USD'));
$markupBips = 500; // 5% markup

$newMoney = $rate->convert($money)->multiply($markupBips / 10000 + 1);

使用比率提供者获取货币汇率

$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$rateProvider = new InMemoryRateProvider(['EUR/USD' => 1.09, 'USD/EUR' => 0.9172], new DateTime());
$rate = $c->getRate($rateProvider);

测试

phpunit

安全性

如果您发现安全漏洞,请向 security at rebilly dot com 报告。

许可

Money 库是在与软件一起分发的 MIT 许可证 下开源的。