/** * Allocates the money among a given number of targets * * @param int $num The number of targets * * @return Money[] */ public function split($num) { assert(Test::naturalNumber($num), sprintf('%s expects $num to be greater than zero', __METHOD__)); $num = (int) $num; $shares = []; $amount = $this->castToInteger($this->amount / $num); $remainder = $this->amount % $num; for ($i = 0; $i < $num; $i++) { $shares[] = $this->withAmount($amount); } for ($i = 0; $i < $remainder; $i++) { $shares[$i] = $this->withAmount($shares[$i]->amount + 1); } return $shares; }