brick/money
Brick\Money is a PHP library for precise, immutable money and currency values. It provides exact arithmetic (no float errors), explicit rounding control, and supports large amounts via brick/math, with optional GMP/BCMath acceleration.
brick/math) align perfectly with Laravel’s need for reliable financial operations.Money facade (if used) or custom financial logic can leverage this package for consistency. The package’s context-based rounding (e.g., CashContext, AutoContext) addresses real-world currency constraints (e.g., Swiss Franc cash rounding).split(), allocate()) and comparison methods (isEqualTo(), isSameValueAs()) support DDD patterns like Value Objects and Domain Events (e.g., for order processing or tax calculations).composer require brick/money with no Laravel-specific dependencies.0.10 (PHP 8.1) or 0.8 (PHP 8.0).Money::ofMinor() for storage) or custom accessors in Eloquent models. Example:
// Model attribute casting
protected $casts = [
'amount' => Money::class,
];
class OrderTotal {
public function __construct(public Money $amount) {}
}
0.x.* (e.g., 0.13.*) to mitigate risk.RationalMoney chains) may impact latency. Benchmark with GMP/BCMath extensions enabled for optimization.CashContext) require upfront design decisions. Misconfiguration (e.g., wrong step for CHF) could lead to incorrect rounding.RoundingMode::Up vs. Down).CurrencyMismatchException).float/decimal fields in databases/models map to Money objects? Use accessors/mutators or database migrations.RoundingMode::HalfUp (banker’s rounding) or Up (conservative)?IsoCurrencyProvider::getHistoricalCurrenciesForCountry())?RationalMoney could be optimized?Money objects to/from database fields.
// Model
public function getAmountAttribute($value) {
return Money::ofMinor($value, $this->currency);
}
Money to JSON via custom encoders or DTOs.
// DTO
public function toArray(): array {
return ['amount' => $this->money->getAmount(), 'currency' => $this->money->getCurrencyCode()];
}
Money objects directly in Laravel queues (immutability ensures thread safety).Money to gateway-specific formats (e.g., Money::ofMinor() → cents).MoneyBag for multi-currency tax splits.float calculations with Money objects in domain services.OrderService to use Money::of() instead of floatval().amount (minor units) and currency columns to tables (e.g., orders, invoices).Money objects with database fields.amount and currency fields.Money::getCurrencyCode().0.13.* (PHP 8.2+).0.10 (PHP 8.1).DECIMAL(20, 0) (e.g., cents) to avoid floating-point issues.INTEGER for minor units.float logic in adapters to gradually adopt Money:
class FloatAdapter {
public static function toMoney(float $amount, string $currency): Money {
return Money::ofMinor(round($amount * 100), $currency);
}
}
Money to validate the approach.Money::allocate(), MoneyBag).Money-related operations to catch currency mismatches or rounding discrepancies early.0.x.* to avoid breaking changes. Monitor release notes for currency updates.{
"scripts": {
"check-money-version": "php -r \"$version = file_get_contents('composer.lock'); if (strpos($version, 'brick/money': '0.14') !== false) echo 'Warning: Major version bump detected!';\""
}
}
Money object lifecycle (creation, operations, serialization).CurrencyMismatchException with stack traces to identify integration issues.RationalMoney for complex calculations to trace discrepancies.USD) align with business logic (e.g., tax jurisdiction).Currency::getSymbol() to avoid repeated ISO lookups.| Issue Type | Resolution Path |
|---|---|
| Rounding discrepancies | Reproduce with RationalMoney |
| Currency not found | Check ISO 4217 updates |
| Performance bottlenecks | Enable GMP/BCMath, optimize contexts |
Currency objects (e.g., Currency::of('USD')) in a static container.MoneyBag for bulk calculations (e.g., refunds, payouts).currency columns for fast joins.How can I help you explore Laravel packages today?