rogervila/moneyphp-operations

Helpers for manipulating money with MoneyPHP

1.5.0 2023-12-02 11:50 UTC

This package is auto-updated.

Last update: 2024-10-19 18:30:41 UTC


README

MoneyPHP Operations

Build Status StyleCI Quality Gate Status

Latest Stable Version Total Downloads License

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