adachsoft / money
Immutable money value objects for PHP with exact arithmetic and currency-safe operations.
Requires
- php: ^8.3
- ext-bcmath: *
- adachsoft/collection: ^3.0
Requires (Dev)
- adachsoft/php-code-style: ^0.6.0
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^12.0
- rector/rector: ^2.6
- symplify/phpstan-rules: ^14.9
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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.