Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,19 @@ public function allocate(array $ratios): array
$results = [];
$total = array_sum($ratios);

if ($total <= 0) {
throw new InvalidArgumentException('Cannot allocate to none, sum of ratios must be greater than zero');
$nonNegativeRatios = array_filter($ratios, function($ratio) {
return $ratio >= 0;
});

if (count($nonNegativeRatios) === 0) {
throw new \InvalidArgumentException('Cannot allocate to none, ratios must contain at least one non-negative value');
}

foreach ($ratios as $key => $ratio) {
if ($ratio < 0) {
throw new InvalidArgumentException('Cannot allocate to none, ratio must be zero or positive');
}
if ($total == 0) {
throw new \InvalidArgumentException('Cannot allocate to none, sum of ratios must not be zero');
}

foreach ($ratios as $key => $ratio) {
$share = self::$calculator::share($this->amount, (string) $ratio, (string) $total);
$results[$key] = new self($share, $this->currency);
$remainder = self::$calculator::subtract($remainder, $share);
Expand Down