public function split(array $percentages, bool $round = true): array
    {
        $totalPercentage = array_sum($percentages);
        if ($totalPercentage > 100) {
            throw new InvalidArgumentException('Only 100% can be allocated');
        }
        $amounts = [];
        $total = 0;
        if (!$round) {
            foreach ($percentages as $percentage) {
                $share = $this->percentage($percentage);
                $total += $share->getAmount();
                $amounts[] = $share;
            }
            if ($totalPercentage != 100) {
                $amounts[] = new static($this->amount - $total, $this->currency);
            }
            return $amounts;
        }

        $count = 0;

        if ($totalPercentage != 100) {
            $percentages[] = 0; //Dummy record to trigger the rest of the amount being assigned to a final pot
        }

        foreach ($percentages as $percentage) {
            ++$count;
            if ($count == count($percentages)) {
                $amounts[] = new static($this->amount - $total, $this->currency);
            } else {
                $share = $this->percentage($percentage)->round();
                $total += $share->getAmount();
                $amounts[] = $share;
            }
        }

        return $amounts;
    }
public function split(array $percentages, bool $round = true): array
    {
        $totalPercentage = array_sum($percentages);
        if ($totalPercentage > 100) {
            throw new InvalidArgumentException('Only 100% can be allocated');
        }
        $amounts = [];
        $total = 0;
        if (!$round) {
            foreach ($percentages as $percentage) {
                $share = $this->percentage($percentage);
                $total += $share->getAmount();
                $amounts[] = $share;
            }
            if ($totalPercentage != 100) {
                $amounts[] = new static($this->amount - $total, $this->currency);
            }
            return $amounts;
        }

        $count = 0;

        if ($totalPercentage != 100) {
            $percentages[] = 0; //Dummy record to trigger the rest of the amount being assigned to a final pot
        }

        foreach ($percentages as $percentage) {
            ++$count;
            if ($count == count($percentages)) {
                $amounts[] = new static($this->amount - $total, $this->currency);
            } else {
                $share = $this->percentage($percentage)->round();
                $total += $share->getAmount();
                $amounts[] = $share;
            }
        }

        return $amounts;
    }
public function split ( array $ percentages , bool $ round = true ) : array { $ totalPercentage = array_sum ( $ percentages ) ; if ( $ totalPercentage > 100 ) { throw new InvalidArgumentException ( 'Only 100% can be allocated' ) ; } $ amounts = [ ] ; $ total = 0 ; if ( ! $ round ) { foreach ( $ percentages as $ percentage ) { $ share = $ this -> percentage ( $ percentage ) ; $ total += $ share -> getAmount ( ) ; $ amounts [ ] = $ share ; } if ( $ totalPercentage != 100 ) { $ amounts [ ] = new static ( $ this -> amount - $ total , $ this -> currency ) ; } return $ amounts ; } $ count = 0 ; if ( $ totalPercentage != 100 ) { $ percentages [ ] = 0 ; //Dummy record to trigger the rest of the amount being assigned to a final pot } foreach ( $ percentages as $ percentage ) { ++ $ count ; if ( $ count == count ( $ percentages ) ) { $ amounts [ ] = new static ( $ this -> amount - $ total , $ this -> currency ) ; } else { $ share = $ this -> percentage ( $ percentage ) -> round ( ) ; $ total += $ share -> getAmount ( ) ; $ amounts [ ] = $ share ; } } return $ amounts ; }
Pass in an array of percentages to allocate Money in those amounts.
Final entry in array gets any remaining units.
If the percentages total less than 100, remaining money is allocated to an extra return value.

By default, the amounts are rounded to the correct number of decimal places for that currency. This can be disabled by passing false as the second argument.

@param float[] $percentages An array of percentages that must total 100 or less
@param bool $round
@return Money[]
Pass in an array of percentages to allocate Money in those amounts . Final entry in array gets any remaining units . If the percentages total less than 100 remaining money is allocated to an extra return value .