<?php
namespace App\Support;
use Brick\Math\BigDecimal; use Brick\Math\RoundingMode; use InvalidArgumentException;
final class Money { public static function toMinor(string $amount,int $scale=2): int { if(!preg_match('/^(0|[1-9]\d*)(\.\d+)?$/',$amount)) throw new InvalidArgumentException('Invalid decimal amount.'); $minor=BigDecimal::of($amount)->withPointMovedRight($scale); if(!$minor->isEqualTo($minor->toScale(0,RoundingMode::UNNECESSARY))) throw new InvalidArgumentException('Amount has too many decimal places.'); return $minor->toInt(); } }
