Search by

adachsoft / money

Immutable money value objects for PHP with exact arithmetic and currency-safe operations.

Maintainers

Package info

gitlab.com/a.adach/money

Issues

pkg:composer/adachsoft/money

Transparency log

Statistics

Installs: 9

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.3 2026-08-31 20:51 UTC

This package is not auto-updated.

Last update: 2026-09-01 16:58:06 UTC


README

Immutable money value objects for PHP with exact arithmetic, currency-safe operations, allocation, and range support.

Requirements

  • PHP 8.3 or newer
  • BCMath extension

Installation

composer require adachsoft/money

Usage

Amounts are stored as integer minor units to avoid floating-point rounding errors:

use AdachSoft\Money\ValueObject\CurrencyVo;
use AdachSoft\Money\ValueObject\MoneyVo;

$price = MoneyVo::fromMajorUnits('19.99', CurrencyVo::EUR());
$tax = MoneyVo::fromMajorUnits('2.50', CurrencyVo::EUR());
$total = $price->add($tax);

echo $total->toMajorUnitsString(); // 22.49
echo $total;                       // 22.49 EUR

Create values directly from minor units when the source system already uses the smallest currency unit:

$amount = MoneyVo::fromMinorUnits(1999, CurrencyVo::EUR());

Arithmetic operations return new immutable values. Operations between different currencies throw CurrencyMismatchException.

$remaining = $total->subtract($price);
$discounted = $price->multiply('0.9');
$shares = $total->allocate([1, 2, 1]);

Supported currencies

CurrencyVo includes factory methods for common currencies and digital assets, including PLN(), EUR(), USD(), GBP(), JPY(), BTC(), ETH(), USDT(), USDC(), and SOL().

Custom currencies can define their minor-unit precision:

$customCurrency = new CurrencyVo('ABC', 4);
$amount = MoneyVo::fromMajorUnits('12.3456', $customCurrency);

Development

Install development dependencies and run the test suite:

composer install
vendor/bin/phpunit

License

This library is released under the MIT License. See LICENSE for details.