rogervila / moneyphp-operations
Helpers for manipulating money with MoneyPHP
Installs: 1 277
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 2
Requires
- moneyphp/money: ^4.1
Requires (Dev)
- ext-intl: *
- phpstan/phpstan: ^1.9.14
- phpunit/phpunit: ^9.5.28
- vimeo/psalm: ^5.6
This package is auto-updated.
Last update: 2024-12-12 07:07:18 UTC
README
MoneyPHP Operations
About
MoneyPHP Operations brings a set of helpers to manipulate money with MoneyPHP.
Install
composer require rogervila/moneyphp-operations
Usage
Note: Pull requests with new helpers are welcome!
Percentage increase
use Money\Money; use MoneyOperation\Operation; $money = Money::EUR('100'); // 1€ $increasedMoney = Operation::of($money)->percentageIncrease('20') // 1.20€
Percentage decrease
use Money\Money; use MoneyOperation\Operation; $money = Money::EUR('288'); // 2.88€ // percentageDecrease accepts positive and negative numeric strings $decreasedMoney = Operation::of($money)->percentageDecrease('2.99') // 2.79€ $decreasedMoney = Operation::of($money)->percentageDecrease('-2.99') // 2.79€
Percentage difference
use Money\Money; use MoneyOperation\Operation; $moneyA = Money::EUR('100'); // 1€ $moneyB = Money::EUR('120'); // 1.20€ // Returns a float. Use number_format to format the result $percentage = Operation::of($moneyA)->percentageDifference($moneyB) // 20.0
Split
use Money\Money; use MoneyOperation\Operation; $money = Money::EUR('1000'); // 10€ /** * Will try to increase the first part when cannot be split equally * Throws \MoneyOperation\Exceptions\InvalidOperationException when cannot be split at all (for very low values mainly) */ $parts = Operation::of($money)->split(3) // [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]
Join
use Money\Money; use MoneyOperation\Operation; $parts = [Money::EUR('334'), Money::EUR('333'), Money::EUR('333')]; $money = Operation::join($parts) // 10€
Average
use Money\Money; use MoneyOperation\Operation; $parts = [Money::EUR('100'), Money::EUR('200'), Money::EUR('300'), Money::EUR('400')]; $money = Operation::average($parts) // 2,50€
Format
use Money\Money; use MoneyOperation\Operation; /** * Uses \Money\Formatter\IntlMoneyFormatter * Throws \MoneyOperation\Exceptions\InvalidOperationException when intl extension is not available */ $money = Operation::of(Money::USD('100'))->format('en_US') // $1.00
Parse
use Money\Money; use MoneyOperation\Operation; /** * Uses \Money\Parser\IntlMoneyParser * Throws \MoneyOperation\Exceptions\InvalidOperationException when intl extension is not available */ $money = Operation::parse('$1.00', 'en_US') // Money::USD('100')
To Decimal
use Money\Money; use MoneyOperation\Operation; /** * Uses \Money\Parser\DecimalMoneyFormatter */ $money = Operation::of(Money::EUR(54321))->toDecimal() // double(543.21)
Factory
use Money\Money; use MoneyOperation\Operation; /** * @param int|numeric-string $amount * @param Currency|non-empty-string $currency */ $money = Operation::factory(100, 'EUR') // Money::EUR('100')
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