rebilly / money
货币值对象的实现
v2.1.0
2024-03-01 08:47 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- ext-mbstring: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.16.1
- vimeo/psalm: ^4.20
README
Money
值对象,用于表示使用货币的最小单位表示的货币值。此库最初基于 Sebastian Bergmann 的已停用的库。它已更新,并添加了对货币兑换的支持。
安装
如果您使用 Composer 来管理项目的依赖项,只需在项目的 composer.json
文件中添加对 rebilly/money
的依赖即可。
使用示例
创建一个 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
compareTo()
方法返回一个整数,小于、等于或大于零,如果认为一个 Money
对象的值分别小于、等于或大于另一个 Money
对象的值。
您可以使用 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 对象的货币转换为另一个货币对象
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 许可证 开源。