macmotp/money
Composer 安装命令:
composer require macmotp/money
包简介
Light PHP Library to handle Money and Currencies
关键字:
README 文档
README
Library to Handle Money and Currencies in PHP, simple and light
Useful for any ecommerce, fintech, or any app that requires 💰.
Requirements
- PHP >= 8.2
Installation
Install the package via composer:
composer require macmotp/money
Usage
Create Money objects
use Macmotp\Money; use Macmotp\Support\CurrencyCode; // Amounts are always in cents (or lowest minor unit)! $amount = 100; // All Currencies adopt ISO 4217 standard $currency = CurrencyCode::USD; // Create the object $money = new Money($amount, $currency); // Alternative methods $money = new Money($amount, 'USD'); $money = Money::make($amount, $currency); echo $money->print(); // (string) '1.00$' // Another example with Vietnamese Dong $money = new Money(10000, CurrencyCode::VND); echo $money->print(); // (string) '10 000₫'
List of basic methods
getAmount(): return the amount in cents;getAmountForHumans(): return the amount in decimals;getCurrencyCode(): return the currency code;getCurrencySymbol(): return the currency symbol;getCurrency(): return the currency object (as Currency implementation, not as string);clone(): clone the entire object;zero(): set the amount to zero;
If you want to print any of the currency properties, check the Format class.
For example: if you want to get the currency code, you can run $money->getCurrencyCode()
Print Money (on the screen, not for real)
use Macmotp\Money; use Macmotp\Support\CurrencyCode; $money = new Money(12345, CurrencyCode::USD); // Default format based on each currency echo $money->print(); // (string) '123.45$' // Override the default echo $money->withCode() ->withSpace() ->withoutDecimals() ->print(); // (string) '123 USD'
List of methods for printing
toArray(): convert the object into array;getCurrency()->toArray(): convert the currency object into array;withSymbol(): print the symbol and not the currency code;withoutSymbol(): do not print neither symbol nor currency code;withCode(): print the currency code and not the code;withoutCode(): do not print neither symbol nor currency code;withSymbolOnLeft(): print symbol on the left;withSymbolOnRight(): print symbol on the right;withCodeOnLeft(): print currency code on the left;withCodeOnRight(): print currency code on the right;withSpace(): print a space between the amount and the currency;withoutSpace(): do not print a space between the amount and the currency;withoutDecimals(): do not print decimals of the amount;withDecimalSeparator(string $decimalSeparator): override default decimal separator character;withThousandsSeparator(string $thousandsSeparator): override default thousands separator character;withNumberOfDecimals(int $numberOfDecimals): set the precision of the decimals;
Calculate with Money
use Macmotp\Money; use Macmotp\Support\CurrencyCode; $moneyOne = new Money(200, CurrencyCode::USD); $moneyTwo = new Money(300, CurrencyCode::USD); echo $moneyOne->add($moneyTwo); // new Money(500, CurrencyCode::USD)
List of calculation methods
add(Money $money);subtract(Money $money);multiply(float $factor, string $flat = '');divide(float $factor, string $flat = '');inPercentage(float $percentage, string $flat = '');absolute();
Aggregate Money
use Macmotp\Money; use Macmotp\Support\CurrencyCode; $moneyOne = new Money(200, CurrencyCode::USD); $moneyTwo = new Money(300, CurrencyCode::USD); $moneyThree = new Money(400, CurrencyCode::USD); echo Money::max($moneyOne, $moneyTwo, $moneyThree); // new Money(400, CurrencyCode::USD) echo Money::avg($moneyOne, $moneyTwo, $moneyThree); // new Money(300, CurrencyCode::USD)
List of aggregation methods
min(Money ...$money): return the minimum amount;max(Money ...$money): return the maximum amount;avg(Money ...$money): return the average amount;sum(Money ...$money): return the sum of all amounts;
Compare Money
use Macmotp\Money; use Macmotp\Support\CurrencyCode; $moneyOne = new Money(200, CurrencyCode::USD); $moneyTwo = new Money(300, CurrencyCode::USD); echo $moneyOne->isLessThan($moneyTwo); // (bool) true
List of comparison methods
isSameCurrency(Money ...$money);isEqualTo(Money $money);isGreaterThan(Money $money);isGreaterThanOrEqualTo(Money $money);isLessThan(Money $money);isLessThanOrEqualTo(Money $money);isPositive();isNegative();isZero();isZeroOrPositive();isZeroOrNegative();
List of Currencies in the package
use Macmotp\Money; $list = Money::getAllCurrencies(); // This will return a Illuminate\Support\Collection
These are the implemented currencies:
AED: United Arab Emirates DirhamAFN: Afghan AfghaniALL: Albanian LekAMD: Armenian DramANG: Netherlands Antillean GuilderAOA: Angolan KwanzaARS: Argentine PesoAUD: Australian DollarAWG: Aruban FlorinAZN: Azerbaijani ManatBAM: Bosnia and Herzegovina Convertible MarkBBD: Barbados DollarBDT: Bangladeshi TakaBGN: Bulgarian LevBHD: Bahraini DinarBIF: Burundian FrancBMD: Bermudian DollarBND: Brunei DollarBOB: Bolivian BolivianoBRL: Brazilian RealBSD: Bahamian DollarBTC: BitcoinBTN: Bhutanese NgultrumBWP: Botswana PulaBYN: Belarusian RubleBZD: Belize DollarCAD: Canadian DollarCDF: Congolese FrancCHF: Swiss FrancCLP: Chilean PesoCNY: Chinese Yuan - RenminbiCOP: Colombian PesoCRC: Costa Rican ColónCUP: Cuban PesoCVE: Cape Verdean EscudoCZK: Czech KorunaDJF: Djiboutian FrancDKK: Danish KroneDOP: Dominican PesoDZD: Algerian DinarEGP: Egyptian PoundERN: Eritrean NakfaETB: Ethiopian BirrEUR: EuroFJD: Fijian DollarFKP: Falkland Islands PoundGBP: British PoundGEL: Georgian LariGHS: Ghanaian CediGIP: Gibraltar PoundGMD: Gambian DalasiGNF: Guinean FrancGTQ: Guatemalan QuetzalGYD: Guyanese DollarHKD: Hong Kong DollarHNL: Honduran LempiraHTG: Haitian GourdeHUF: Hungarian ForintIDR: Indonesian RupiahILS: Israeli New ShekelINR: Indian RupeeIQD: Iraqi DinarIRR: Iranian RialISK: Icelandic KrónaJMD: Jamaican DollarJOD: Jordanian DinarJPY: Japanese YenKES: Kenyan ShillingKGS: Kyrgyz SomKHR: Cambodian RielKMF: Comorian FrancKPW: North Korean WonKRW: South Korean WonKWD: Kuwaiti DinarKYD: Cayman Islands DollarKZT: Kazakhstani TengeLAK: Lao KipLBP: Lebanese PoundLKR: Sri Lankan RupeeLRD: Liberian DollarLSL: Lesotho LotiLYD: Libyan DinarMAD: Moroccan DirhamMDL: Moldovan LeuMGA: Malagasy AriaryMKD: Macedonian DenarMMK: Burmese KyatMNT: Mongolian TögrögMOP: Macanese PatacaMRU: Mauritanian OuguiyaMUR: Mauritian RupeeMVR: Maldivian RufiyaaMWK: Malawian KwachaMXN: Mexican PesoMYR: Malaysian RinggitMZN: Mozambican MeticalNAD: Namibian DollarNGN: Nigerian NairaNIO: Nicaraguan CórdobaNOK: Norwegian KroneNPR: Nepalese RupeeNZD: New Zealand DollarOMR: Omani RialPAB: Panamanian BalboaPEN: Peruvian SolPGK: Papua New Guinean KinaPHP: Philippine PesoPKR: Pakistani RupeePLN: Polish ZłotyPYG: Paraguayan GuaraníQAR: Qatari RiyalRON: Romanian LeuRSD: Serbian DinarRUB: Russian RubleRWF: Rwandan FrancSAR: Saudi RiyalSBD: Solomon Islands DollarSCR: Seychellois RupeeSDG: Sudanese PoundSEK: Swedish KronaSGD: Singapore DollarSHP: Saint Helena PoundSLE: Sierra Leonean LeoneSOS: Somali ShillingSRD: Surinamese DollarSSP: South Sudanese PoundSTN: São Tomé and Príncipe DobraSYP: Syrian PoundSZL: Swazi LilangeniTHB: Thai BahtTJS: Tajikistani SomoniTMT: Turkmenistani ManatTND: Tunisian DinarTOP: Tongan PaʻangaTRY: Turkish LiraTTD: Trinidad and Tobago DollarTWD: New Taiwan DollarTZS: Tanzanian ShillingUAH: Ukrainian HryvniaUGX: Ugandan ShillingUSD: United States DollarUYU: Uruguayan PesoUZS: Uzbekistani SumVES: Venezuelan Sovereign BolívarVND: Vietnamese ĐồngVUV: Vanuatu VatuWST: Samoan TālāXAF: Central African CFA FrancXCD: Eastern Caribbean DollarXOF: West African CFA FrancXPF: CFP FrancYER: Yemeni RialZAR: South African RandZMW: Zambian KwachaZWG: Zimbabwe Gold
If you want to add another currency, please open an issue.
Testing
composer test
Changelog
Please see changelog for more information on what has changed recently.
Contributing
Please see contributing for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
macmotp/money 适用场景与选型建议
macmotp/money 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.23k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「money」 「currencies」 「macmotp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 macmotp/money 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 macmotp/money 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 macmotp/money 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Beyonic PHP Library
ExchangeRatesCBRF Class to get exchange rates of the Central Bank of Russia
PHP Countries and Currencies
The Payum Yandex Money gateway
A database with ISO country information in all configured translations
Laravel countries, states, cities and currencies
统计信息
- 总下载量: 1.23k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-13