skeeks / cms-money
金钱和货币处理模块
2.0.0
2023-06-05 11:23 UTC
README
信息
金钱和货币处理模块。
主要工作是实现金钱的透明处理。例如,在这个库中我们透明地解决了以下问题:(10美元 + 154卢布 + 12美元)= 结果需要显示为GBP,针对de_DE地区。
示例
use \skeeks\cms\money\Money;
$result = new Money('0', "RUB");
$money1 = new Money('10', "USD");
$money2 = new Money('154', "EUR");
$money3 = new Money('12', "RUB");
$result->add($money1)->add($money2)->add($money3);
$result->convertToCurrency('GBP');
echo (string) $result; //
echo $result->format();
$formatter = new IntlFormatter('de_DE');
安装
推荐通过composer安装此扩展。
运行以下命令
php composer.phar require --prefer-dist skeeks/cms-money "*"
或添加
"skeeks/cms-money": "*"
安装迁移
php yii migrate --migrationPath=vendor/skeeks/cms-money/migrations
配置
'components' => [ 'money' => [ 'class' => 'skeeks\cms\money\MoneyComponent', ], 'i18n' => [ 'translations' => [ 'skeeks/money' => [ 'class' => 'yii\i18n\PhpMessageSource', 'basePath' => '@skeeks/cms/money/messages', 'fileMap' => [ 'skeeks/money' => 'main.php', ], ] ] ], ], 'modules' => [ 'money' => [ 'class' => 'skeeks\cms\money\Module', ] ]
示例和用法
原始示例
创建一个金钱对象并访问其货币值
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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();
上面的代码会产生以下输出
100
从字符串值创建金钱对象
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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
使用PHP内置的NumberFormatter格式化金钱对象
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\money\Money; use skeeks\cms\modules\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);
上面的代码会产生以下输出
1,00 €
使用金钱对象进行基本算术运算
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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
比较金钱对象
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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));
上面的代码会产生以下输出
bool(true)
bool(false)
bool(false)
bool(true)
int(-1)
int(0)
int(1)
compareTo()
方法返回一个整数,如果认为一个Money
对象的值分别小于、等于或大于另一个Money
对象的值,则返回一个小于、等于或大于零的整数。
您可以使用compareTo()
方法,配合PHP内置的排序函数对Money
对象数组进行排序
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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
将金钱对象表示的货币值分配给N个目标
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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
使用比率列表将金钱对象表示的货币值进行分配
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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
从金钱对象表示的货币值中提取百分比(和子总金额)
use skeeks\cms\modules\money\Currency; use skeeks\cms\modules\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)
计算金钱对象表示的货币值的21%)。
链接
SkeekS CMS (Yii2) — 快速、简单、高效!
skeeks.com | cms.skeeks.com