diff --git a/src/Money.php b/src/Money.php index 56c6c66b..250c6326 100644 --- a/src/Money.php +++ b/src/Money.php @@ -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);