src/Money.php
public function allocate(array $ratios, $useGaapPrecision = false)
    {
        $useGaapPrecision ? $precision = self::GAAP_PRECISION : $precision = self::BASIC_PRECISION;

        $remainder = $this->amount;
        $results = [];
        $total = array_sum($ratios);

        foreach ($ratios as $ratio) {
            $share = bcdiv(
                bcmul(
                    $this->amount,
                    (string)$ratio,
                    $precision
                ),
                (string)$total,
                $precision
            );
            $results[] = new Money($share, $this->currency);
            $remainder = bcsub($remainder, $share, $precision);
        }

        $count = count($results) - 1;
        $index = 0;
        $minValue = '0.'.str_repeat('0', $precision - 1).'1';

        while (bccomp($remainder, '0'.str_repeat('0', $precision), $precision) !== 0) {
            $remainder = bcsub($remainder, $minValue, $precision);
            $results[$index] = $results[$index]->add(new Money($minValue, $this->currency));
            if ($index !== $count) {
                $index++;
            } else {
                $index = 0;
            }
        }

        return $results;
    }
public function allocate(array $ratios, $useGaapPrecision = false)
    {
        $useGaapPrecision ? $precision = self::GAAP_PRECISION : $precision = self::BASIC_PRECISION;

        $remainder = $this->amount;
        $results = [];
        $total = array_sum($ratios);

        foreach ($ratios as $ratio) {
            $share = bcdiv(
                bcmul(
                    $this->amount,
                    (string)$ratio,
                    $precision
                ),
                (string)$total,
                $precision
            );
            $results[] = new Money($share, $this->currency);
            $remainder = bcsub($remainder, $share, $precision);
        }

        $count = count($results) - 1;
        $index = 0;
        $minValue = '0.'.str_repeat('0', $precision - 1).'1';

        while (bccomp($remainder, '0'.str_repeat('0', $precision), $precision) !== 0) {
            $remainder = bcsub($remainder, $minValue, $precision);
            $results[$index] = $results[$index]->add(new Money($minValue, $this->currency));
            if ($index !== $count) {
                $index++;
            } else {
                $index = 0;
            }
        }

        return $results;
    }
public function allocate ( array $ ratios , $ useGaapPrecision = false ) { $ useGaapPrecision ? $ precision = self :: GAAP_PRECISION : $ precision = self :: BASIC_PRECISION ; $ remainder = $ this -> amount ; $ results = [ ] ; $ total = array_sum ( $ ratios ) ; foreach ( $ ratios as $ ratio ) { $ share = bcdiv ( bcmul ( $ this -> amount , ( string ) $ ratio , $ precision ) , ( string ) $ total , $ precision ) ; $ results [ ] = new Money ( $ share , $ this -> currency ) ; $ remainder = bcsub ( $ remainder , $ share , $ precision ) ; } $ count = count ( $ results ) - 1 ; $ index = 0 ; $ minValue = '0.' . str_repeat ( '0' , $ precision - 1 ) . '1' ; while ( bccomp ( $ remainder , '0' . str_repeat ( '0' , $ precision ) , $ precision ) !== 0 ) { $ remainder = bcsub ( $ remainder , $ minValue , $ precision ) ; $ results [ $ index ] = $ results [ $ index ] -> add ( new Money ( $ minValue , $ this -> currency ) ) ; if ( $ index !== $ count ) { $ index ++ ; } else { $ index = 0 ; } } return $ results ; }
Allocate the money according to a list of ratio's
Allocation is compatible with Verraes' Money
To use GAAP precision rounding, pass true as second argument

@param array $ratios List of ratio's
@param bool  $useGaapPrecision

@return array
Allocate the money according to a list of ratio s Allocation is compatible with Verraes Money To use GAAP precision rounding pass true as second argument