Implementation for compomo-kit/money-interfaces

Installs: 4

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/compono-kit/money

2.0.0 2025-10-19 16:35 UTC

This package is auto-updated.

Last update: 2025-10-19 16:35:23 UTC


README

Implementation for compomo-kit/money-interfaces

Contents

Requirements

  • PHP >= 8.3
  • compono-kit/money-interfaces

📦 Installation

composer require compono-kit/money

Basics

(new Money( 5000, 'EUR' ))->getAmount(); //5000
(new Money( 5000, 'EUR' ))->add( new Money( 99, 'EUR' ) )->getAmount(); //5099
(new Money( 5000, 'EUR' ))->subtract( new Money( 99, 'EUR' ) )->getAmount(); //4901
(new Money( 5000, 'EUR' ))->multiply( 2 )->getAmount(); //10000
(new Money( 5000, 'EUR' ))->divide( 2 )->getAmount(); //2500

(new Money( 5000, 'EUR' ))->mod( new Money( 4000, 'EUR' ) )->getAmount(); //1000
(new Money( -5000, 'EUR' ))->absolute()->getAmount(); //5000
(new Money( 5000, 'EUR' ))->negate()->getAmount(); //-5000

(new Money( 5000, 'EUR' ))->ratioOf( new Money( 10000, 'EUR' ) ); //0.5
(new Money( 5000, 'EUR' ))->ratioOf( new Money( 2500, 'EUR' ) ); //2

(new Money( 5000, 'EUR' ))->getCurrencyCode() ; //EUR
(new Money( 5000, 'EUR' ))->hasSameCurrency( new Money( 5000, 'USD' ) ) ; //false

Comparisons

(new Money( 1000, 'EUR' ))->greaterThan( new Money( 2000, 'EUR' ) ); //false
(new Money( 1000, 'EUR' ))->lessThan( new Money( 2000, 'EUR' ) ); //true
(new Money( 1000, 'EUR' ))->equals( new Money( 1000, 'EUR' ) ); //true
(new Money( 1000, 'EUR' ))->lessThanOrEqual( new Money( 1000, 'EUR' ) ); //true
(new Money( 1000, 'EUR' ))->lessThanOrEqual( new Money( 1500, 'EUR' ) ); //true
(new Money( 1000, 'EUR' ))->greaterThanOrEqual( new Money( 1000, 'EUR' ) ); //true
(new Money( 1000, 'EUR' ))->greaterThanOrEqual( new Money( 500, 'EUR' ) ); //true

(new Money( 1000, 'EUR' ))->equals( new Money( 4000, 'USD' ) ); //throws CurrencyMismatchException
(new Money( -1000, 'EUR' ))->isNegative(); //true
(new Money( 1000, 'EUR' ))->isPositive(); //true
(new Money( 0, 'EUR' ))->isZero(); //true

Allocation

$allocatedMoney = iterator_to_array((new Money( 99, 'EUR' ))->allocateToTargets( 5 ));
$allocatedMoney[0]->getAmount(); //20
$allocatedMoney[1]->getAmount(); //20
$allocatedMoney[2]->getAmount(); //20
$allocatedMoney[3]->getAmount(); //20
$allocatedMoney[4]->getAmount(); //19

$allocatedMoney = iterator_to_array((new Money( 5000, 'EUR' ))->allocateByRatios( [ 70, 30 ] ));
printf( "70%% of 5000 = %d", $allocatedMoney[0]->getAmount() ); //70% of 5000 = 3500
printf( "30%% of 5000 = %d", $allocatedMoney[1]->getAmount() ); //30% of 5000 = 1500

Value added tax calculation

$extractedPercentage = (new Money( 5000, 'EUR' ))->extractPercentage( 19 ); //19% VAT rate, 5000 = Gross amount
printf(
	"%d + %d = 5000",
	$extractedPercentage->getSubTotal()->getAmount(),
	$extractedPercentage->getPercentage()->getAmount()
); // 4202 + 798 = 5000

$extractedPercentage->getSubTotal(); //Net amount
$extractedPercentage->getPercentage(); //VAT amount

Json

json_encode( (new Money( 5000, new Currency( 'EUR', '€', 100 ) )) ); //{ "amount": "5000", "currency": { "iso-code": "EUR", "symbol": "€", "minor-unit-factor": 100, "minor-unit": 2 } }

Exceptions

To handle all possible exceptions in a unified way, the RepresentsMoneyException interface can be used.

Specific exceptions from Money are:

  • CurrencyMismatchExceptions
  • InvalidCurrencyException
  • InvalidRoundingModeException

Specific exceptions from MoneyAggregator are:

  • CurrencyMismatchExceptions

Specific exceptions from RatioCalculator are:

  • InvalidRatioException

Helpers

MoneyAggregator

MoneyAggregator::sum( new Money( 1000, 'EUR' ), new Money( 2000, 'EUR' ), new Money( 4000, 'EUR' ) )->getAmount(); //7000
MoneyAggregator::avg( new Money( 5000, 'EUR' ), new Money( 2000, 'EUR' ), new Money( 8000, 'EUR' ) )->getAmount(); //3000
MoneyAggregator::min( new Money( 1000, 'EUR' ), new Money( 2000, 'EUR' ), new Money( 4000, 'EUR' ) )->getAmount(); //1000
MoneyAggregator::max( new Money( 1000, 'EUR' ), new Money( 2000, 'EUR' ), new Money( 4000, 'EUR' ) )->getAmount(); //4000