rogervila/moneyphp-operations
Composer 安装命令:
composer require rogervila/moneyphp-operations
包简介
Helpers for manipulating money with MoneyPHP
README 文档
README
MoneyPHP Operations
MoneyPHP Operations provides a set of powerful helpers to manipulate and format money using MoneyPHP.
Installation
composer require rogervila/moneyphp-operations
Usage
Initialization
You can initialize an Operation instance using an existing Money object or directly from values.
use Money\Money; use MoneyOperation\Operation; // From a Money object $operation = Operation::of(Money::EUR(1000)); // From values (amount and currency) $operation = Operation::ofValues(1000, 'EUR');
Percentage Operations
Increase
Increase the amount by a given percentage.
$money = Money::EUR('100'); // 1.00€ $increased = Operation::of($money)->percentageIncrease('20'); // 1.20€ // Custom rounding mode $increased = Operation::of($money)->percentageIncrease('20', Money::ROUND_HALF_DOWN);
Decrease
Decrease the amount by a given percentage. Supports both positive and negative percentage strings.
$money = Money::EUR('288'); // 2.88€ $decreased = Operation::of($money)->percentageDecrease('2.99'); // 2.79€
Difference
Calculate the percentage difference between two Money objects.
$moneyA = Money::EUR('100'); $moneyB = Money::EUR('120'); $diff = Operation::of($moneyA)->percentageDifference($moneyB); // 20.0
Collection Operations
Split
Split a Money object into multiple parts. It ensures the sum of parts equals the original amount by adding the remainder to the first part.
$money = Money::EUR('1000'); // 10.00€ $parts = Operation::of($money)->split(3); // [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]
Join
Combine an array of Money objects back into a single Money object.
$parts = [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]; $sum = Operation::join($parts); // 10.00€
Assert Split
Verify if an array of Money objects correctly totals the original instance.
$parts = [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]; $isValid = Operation::of(Money::EUR(1000))->assertSplit($parts); // true
Average
Calculate the average value of a collection of Money objects.
$parts = [Money::EUR('100'), Money::EUR('200'), Money::EUR('300'), Money::EUR('400')]; $avg = Operation::average($parts); // 2.50€
Formatting and Parsing
Formatting
Format a Money object to a localized string. Requires the intl extension.
$money = Money::USD('100'); $formatted = Operation::of($money)->format('en_US'); // "$1.00" // Custom currencies implementation $formatted = Operation::of($money)->format('en_US', new MyCustomCurrencies());
Parsing
Parse a localized currency string into a Money object.
$money = Operation::parse('$1.00', 'en_US'); // Money::USD('100')
To Decimal
Convert a Money object to a float representation.
$decimal = Operation::of(Money::EUR(54321))->toDecimal(); // 543.21
Factory
Create Money instances easily.
$money = Operation::factory(100, 'EUR'); // Money::EUR('100') $money = Operation::factory('500', new \Money\Currency('USD')); // Money::USD('500')
Rounding Modes
Most operations accept an optional $roundingMode parameter. By default, it uses Money::ROUND_HALF_UP.
Available modes (from MoneyPHP):
Money::ROUND_HALF_UPMoney::ROUND_HALF_DOWNMoney::ROUND_HALF_EVENMoney::ROUND_HALF_ODDMoney::ROUND_UPMoney::ROUND_DOWNMoney::ROUND_CEILINGMoney::ROUND_FLOOR
Exceptions
Methods may throw \MoneyOperation\Exceptions\InvalidOperationException in cases like:
- Invalid number of parts for
split. - Indivisible amounts.
- Missing
intlextension for formatting/parsing. - Empty arrays for
joinoraverage.
Author
Created by Roger Vilà
License
MoneyPHP Operations is open-sourced software licensed under the MIT license.
Icons made by Prosymbols Premium from www.flaticon.es
rogervila/moneyphp-operations 适用场景与选型建议
rogervila/moneyphp-operations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.28k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「money operations」 「moneyphp helpers」 「money percentage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rogervila/moneyphp-operations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rogervila/moneyphp-operations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rogervila/moneyphp-operations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Performing any actions during the deployment process
Bundle integrating the Money for PHP library with Symfony
Beyonic PHP Library
Useful class for binary operations
This bundle provides integration for Money library in your Symfony project.
Integrates moneyphp/money into your Symfony application
统计信息
- 总下载量: 1.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-14