sebastian/money
最新稳定版本:v1.6.2
Composer 安装命令:
composer require sebastian/money
包简介
Value Object that represents a monetary value (using a currency's smallest unit)
关键字:
README 文档
README
This project has been abandoned. It was only ever intended to be used as an example for PHPUnit features etc. and not for usage in production. I am sorry that I failed to make that clear. Please have a look at moneyphp/money instead.
Money
Value Object that represents a monetary value using a currency's smallest unit.
Installation
Simply add a dependency on sebastian/money to your project's composer.json file if you use Composer to manage the dependencies of your project.
Here is a minimal example of a composer.json file that just defines a dependency on Money:
{
"require": {
"sebastian/money": "^1.6"
}
}
Usage Examples
Creating a Money object and accessing its monetary value
use SebastianBergmann\Money\Currency; use SebastianBergmann\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();
The code above produces the output shown below:
100
1.00
Creating a Money object from a string value
use SebastianBergmann\Money\Currency; use SebastianBergmann\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();
The code above produces the output shown below:
1234
Using a Currency-specific subclass of Money
use SebastianBergmann\Money\EUR; // Create Money object that represents 1 EUR $m = new EUR(100); // Access the Money object's monetary value print $m->getAmount();
The code above produces the output shown below:
100
Please note that there is no subclass of Money that is specific to Turkish Lira as TRY is not a valid class name in PHP.
Formatting a Money object using PHP's built-in NumberFormatter
use SebastianBergmann\Money\Currency; use SebastianBergmann\Money\Money; use SebastianBergmann\Money\IntlFormatter; // Create Money object that represents 1 EUR $m = new Money(100, new Currency('EUR')); // Format a Money object using PHP's built-in NumberFormatter (German locale) $f = new IntlFormatter('de_DE'); print $f->format($m);
The code above produces the output shown below:
1,00 €
Basic arithmetic using Money objects
use SebastianBergmann\Money\Currency; use SebastianBergmann\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();
The code above produces the output shown below:
-100
300
100
200
Comparing Money objects
use SebastianBergmann\Money\Currency; use SebastianBergmann\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')); var_dump($a->lessThan($b)); var_dump($a->greaterThan($b)); var_dump($b->lessThan($a)); var_dump($b->greaterThan($a)); var_dump($a->compareTo($b)); var_dump($a->compareTo($a)); var_dump($b->compareTo($a));
The code above produces the output shown below:
bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(0)
int(1)
The compareTo() method returns an integer less than, equal to, or greater than
zero if the value of one Money object is considered to be respectively less
than, equal to, or greater than that of another Money object.
You can use the compareTo() method to sort an array of Money objects using
PHP's built-in sorting functions:
use SebastianBergmann\Money\Currency; use SebastianBergmann\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"; }
The code above produces the output shown below:
100
200
300
Allocate the monetary value represented by a Money object among N targets
use SebastianBergmann\Money\Currency; use SebastianBergmann\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"; }
The code above produces the output shown below:
10
10
10
10
10
10
10
10
10
9
Allocate the monetary value represented by a Money object using a list of ratios
use SebastianBergmann\Money\Currency; use SebastianBergmann\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"; }
The code above produces the output shown below:
2
3
Extract a percentage (and a subtotal) from the monetary value represented by a Money object
use SebastianBergmann\Money\Currency; use SebastianBergmann\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() );
The code above produces the output shown below:
10000 = 8265 + 1735
Please note that this extracts the percentage out of a monetary value where the
percentage is already included. If you want to get the percentage of the
monetary value you should use multiplication (multiply(0.21), for instance,
to calculate 21% of a monetary value represented by a Money object) instead.
sebastian/money 适用场景与选型建议
sebastian/money 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.22M 次下载、GitHub Stars 达 739, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「money」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sebastian/money 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sebastian/money 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sebastian/money 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
The Payum Yandex Money gateway
Handles currency for Laravel 5.8
Yii 2 package for sprintpay gateway
Money lib for RUR currency only
Provides an easy-to-use class for communicating with the exchange rate web service of Banco de Guatemala.
统计信息
- 总下载量: 1.22M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 741
- 点击次数: 33
- 依赖项目数: 16
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 未知