adsmurai / currency
一个小型库,用于处理货币和货币值
3.3.3
2024-02-12 08:12 UTC
Requires
- litipk/php-bignumbers: ^0.8
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^8.0
- dev-master
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.12
- 3.2.11
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.9.x-dev
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.1.1
- 2.1
- 2.0
- 1.0
- dev-CUSHC-9426_fix_SAR_currency_symbol_equal_iso_code
- dev-CUSHC-9426_fix_currency_symbol_equal_iso_code
- dev-remove_obsolete_currencies
- dev-CUSHC-8517_Add_PAB_currency
- dev-FHC-1823_change_symbol_currency_BGN
- dev-FHC-1823_change_to_2_decimals_price
- dev-FHC-1710_Add_manat_currency
- dev-FC-4211_Fix_CLP_currency
- dev-FC-4116_Add_ALL_currency
- dev-FC-3907_add_BAM_currency_support
- dev-FC-3901_add_MKD_currency_support
- dev-FC-3892_add_AMD_currency_support
- dev-FC-3845_Fix_build_PEN_money_from_string_with_symbol
- dev-FC-3797_Add_new_currencies_support
- dev-FC-3758_Add_TND_currency_support
- dev-FC-3095_add_grivna_from_ucrania
- dev-FC-2781_add_bulgarian_currency
- dev-FC-1816_error_on_currency_jod
- dev-FC-1756_add_oman_currency
- dev-FC-1477_unify_fractional_digits
- dev-FC-1475_add_digits_to_cop
- dev-FC-1456_problems_mango_with_price_on_hungria
- dev-FC-1312_add_currencies
This package is auto-updated.
Last update: 2024-09-12 09:34:04 UTC
README
简介
Adsmurai Currency 库的开发是为了解决一些货币和架构相关的问题
- 货币数据集中化
- 值封装
- 表示
- 为了表示非常小的货币金额,提供了额外的精度。
- 转换
设置
通过 composer 安装它,就这样
composer require adsmurai/currency
代码示例
<?php use Adsmurai\Currency\Contracts\Currency; use Adsmurai\Currency\CurrencyFactory; use Adsmurai\Currency\MoneyFactoriesLocator; use Adsmurai\Currency\MoneyFactory; // This factory will create Currency objects given the currency ISO code. // By default, it will load the currency data from a library's internal data // source, but we can use alternative data sources. $currencyFactory = CurrencyFactory::fromDataPath(); $currencyFactory = CurrencyFactory::fromDataArray([ 'EUR' => [ 'numFractionalDigits' => 2, 'symbol' => '€', 'symbolPlacement' => Currency::AFTER_PLACEMENT, ], 'USD' => [ 'numFractionalDigits' => 2, 'symbol' => '$', 'symbolPlacement' => Currency::BEFORE_PLACEMENT, ] ]); // This factory will create Money objects with the same currency type. // The rationale behind this class is that almost always we'll work with the // same currency type. $moneyFactory = new MoneyFactory( $currencyFactory->buildFromISOCode('EUR') ); // In fact, to avoid introducing hard dependencies on the factory implementation // through the instantiation (via the `new` operator), we recommend to obtain // the `MoneyFactory` instances through the `MoneyFactoriesLocator`. // // This will have some advantages: // * We can inject instances of the `MoneyFactoriesLocator` contract in our // domain logic without having to rely on specific implementations. // * We can avoid `MoneyFactory` instances proliferation, since this // factories locator keeps one single instances per currency ISO code. $moneyFactoriesLocator = new MoneyFactoriesLocator($currencyFactory); $moneyFactory = $moneyFactoriesLocator->getMoneyFactory('EUR'); // We have many ways to construct Money objects, depending on the data we // have at the time. $money = $moneyFactory->buildFromString('10.57'); $money = $moneyFactory->buildFromString('10.57€'); // Look! it will validate the symbol too :) $money = $moneyFactory->buildFromString('10.57 €'); $money = $moneyFactory->buildFromString('10.57EUR'); // Look! it will validate the ISO code too :) $money = $moneyFactory->buildFromString('10.57 EUR'); $money = $moneyFactory->buildFromFloat(10.57); $money = $moneyFactory->buildFromFractionalUnits(1057); // If we want to format a currency value, we can use a specific method, that // will take into account the number of digits, symbol, symbol placement... echo $money->format();
故障排除
我们没有遇到任何与这个库相关有趣的问题,如果你在使它工作时有困难,请在问题跟踪器上打开一个问题(我们将更新这一节)。