bnet/money-datatype

简单货币数据类型的包

0.2.3 2022-01-25 09:07 UTC

README

简单的货币数据类型实现

主要功能

  • 包含金额和货币作为包
  • 使用金额进行计算
  • 使用货币进行格式化
  • 将字符串解析为货币数据类型

安装

通过 Composer 安装包。编辑项目的 composer.json 文件,添加

"require": {
    "bussnet/money-datatype": "*"
}

then run:  $ composer update

或执行

composer require "bussnet/money-datatype"

使用方法

手动货币属性


    $a = [
        'code' => 'EUR',
        'iso' => 978,
        'name' => 'Euro',
        'symbol_left' => '',
        'symbol_right' => '',
        'decimal_place' => 2,
        'decimal_mark' => ',',
        'thousands_separator' => '.',
        'unit_factor' => 100
    ];
    $c = new \Bnet\Money\Currency('EUR', $a);

使用 ArrayRepository 的货币


    $repository = new ArrayRepository([
        'EUR' => [
            'code' => 'EUR',
            'iso' => 978,
            'name' => 'Euro',
            'symbol_left' => '',
            'symbol_right' => '',
            'decimal_place' => 2,
            'decimal_mark' => ',',
            'thousands_separator' => '.',
            'unit_factor' => 100
        ]
    ]);
    \Bnet\Money\Currency::registerCurrencyRepository($repository);

    $c = new \Bnet\Money\Currency('EUR');

使用 CallbackRepository 的货币


    $repository = new \Bnet\Money\Repositories\CallbackRepository(function($code) {
        // request the data from Database and return it 
    });
    \Bnet\Money\Currency::registerCurrencyRepository($repository);

    $c = new \Bnet\Money\Currency('EUR');

使用 Money 对象


    // assign own currency
    $m = new Money(123456, 'EUR');
    $m = new Money(123456, new Currency('EUR'));

    // use systemwide default currency
    $m = new Money(123456);

    // int 123456
    $m->amount();

    // string "1234.56"
    $m->normalize();

    // 1234,56€
    $m->format();

    // 1.234,56€ (with thousand mark)
    $m->format(true);

    // 1.234,56 EUR (with thousand mark ans code instead of sign)
    $m->format(true, true);

    // EUR 1.234,56 (with thousand mark ans code instead of sign swap before/after part)
    $m->format(true, true, true);

    // 1234,56€ with html spans arround the parts
    $m->html(/* use the same params as format() above */);

    // return [
    //    'amount' => $this->amount(),
    //    'number' => $this->normalize(),
    //    'format' => $this->format(),
    //    'currency' => $this->currency->code,
    // ];
    $m->toArray();

    // toArray() as JsonString
    $m->toJson();

    // alias for format()
    $m->render();

    // parse Moneystrings -> 123456 cents
    $m = Money::parse('1.234,56');
    // +/- sign before is allowed, currency sign is not allowed
    // the first ./, is interpreted as thousand mark, the second as decimal makr - more than two are not allowed

使用 Money 对象进行计算/检查


    // bool - same currency
    $m->isSameCurrency(Money $otherMoney);

    // 0=equal, 1=$m>$o, -1=$m<$o
    $m->compare(Money $o);

    // no explanation needed
    $m->equals(Money $other);
    $m->greaterThan(Money $o);
    $m->greaterThanOrEqual(Money $o);
    $m->lessThan(Money $o);
    $m->lessThanOrEqual(Money $o);
    $m->isZero();
    $m->isPositive();
    $m->isNegative();

    // ALL MATH OPERATIONS ARE ASSERTING THE SAME CURRENCY !!!

    // add amount and return new obj
    $m->add(Money $o);

    // substruct amount and return new obj
    $m->subtract(Money $o);

    // multiply amount with multiplier and return new obj
    $m->multiply($multiplier);

    // divide amount by divisor and return new obj
    $m->divide($divisor, $roundingMode = PHP_ROUND_HALF_UP);

    // allocate the amount in count($ratios) parts with 
    // the weight of the valiue of $ratios and return Money[]
    $m->allocate(array $ratios);

    // has this MoneyObj TaxCalculation (TaxedMoney)
    $m->hasTax();

使用 TaxedMoney 对象

TaxedMoney 类包含一个金额(总额或净额)和一个税率。在创建时,您定义给定的金额是总额还是净额,以及默认返回值(对于 amount() 和计算)是总额还是净额。

对于 TaxedMoney 对象,有 MoneyGross 和 MoneyNet 作为更简单的表示,具有静态方法 ::fromGross() 和 ::fromNet()。

示例


    use Bnet\Money\MoneyNet;
    use Bnet\Money\MoneyGross;

    // 10EUR is the Net and 19% Tax
    $m = MoneyGross::fromNet(1000, 19, 'EUR');
    // return the net: 1000 -> 10EUR
    $m->amountWithoutTax();

    // both return the gross: 1190 -> 11,90EUR
    $m->amount();
    $m->amountWithTax();

    // all calucaltions with this object are with the **gross**,
    // cause this is the default amount
    // so you can replace any Money Obj ex.: in a Cart, with a TaxedMoneyObj
    // and check with the hasTax() method if you can show the Net/Gross

使用 CurrencyUpdater

下载并解析货币文件,并使用闭包保存

示例


        $updater = new \Bnet\Money\Updater\CurrencyUpdater();

        $updater->update_currency_table(function($item) use($db) {
            $db->save($item);
        });

变更日志

**0.1.7

  • FIX: 将 $tax 从 TaxedMoney 公开,以便在 **0.1.5 访问税
  • FIX: 使用 TaxedMoney 的计算现在正确 **0.1.4
  • 添加 CurrencyUpdater 以下载和解析文件,并使用闭包保存数据 **0.1.3
  • 添加 TaxedMoney 以使用带有税的 MoneyObject **0.1.2
  • 向 Money 和 Currency 添加一些数学和比较方法
  • 添加 __toString、toArray、toJson 方法
  • 添加静态调用 Currency **0.1.1
  • 为小型环境添加默认货币存储库以简化使用 **0.1.0
  • 创建基本功能,准备好使用 Array 和 Callback 货币存储库

许可证

MoneyDatatype 包是开源软件,根据 MIT 许可证 许可。

免责声明

本软件按“原样”提供,并明确或暗示放弃任何保证,包括但不限于对适销性和特定用途适用性的暗示保证。在任何情况下,作者或任何贡献者都不应对任何直接、间接、偶然、特殊、示范性或后果性损害(包括但不限于替代货物或服务的采购;使用、数据或利润的损失;或业务中断)负责,无论损害原因如何,也不论责任理论如何,只要因使用本软件而引起的,即使被告知了此类损害的可能性。