| 1 |
<?php |
| 2 |
|
| 3 |
namespace Money\Calculator; |
| 4 |
|
| 5 |
use Money\Calculator; |
| 6 |
use Money\Money; |
| 7 |
use Money\Number; |
| 8 |
|
| 9 |
/** |
| 10 |
* @author Frederik Bosch <[email protected]> |
| 11 |
*/ |
| 12 |
final class BcMathCalculator implements Calculator |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $scale; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param int $scale |
| 21 |
*/ |
| 22 |
public function __construct($scale = 14) |
| 23 |
{ |
| 24 |
$this->scale = $scale; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* {@inheritdoc} |
| 29 |
*/ |
| 30 |
public static function supported() |
| 31 |
{ |
| 32 |
return extension_loaded('bcmath'); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* {@inheritdoc} |
| 37 |
*/ |
| 38 |
public function compare($a, $b) |
| 39 |
{ |
| 40 |
return bccomp($a, $b, $this->scale); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* {@inheritdoc} |
| 45 |
*/ |
| 46 |
public function add($amount, $addend) |
| 47 |
{ |
| 48 |
return (string) Number::fromString(bcadd($amount, $addend, $this->scale)); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritdoc} |
| 53 |
* |
| 54 |
* @param $amount |
| 55 |
* @param $subtrahend |
| 56 |
* |
| 57 |
* @return string |
| 58 |
*/ |
| 59 |
public function subtract($amount, $subtrahend) |
| 60 |
{ |
| 61 |
return (string) Number::fromString(bcsub($amount, $subtrahend, $this->scale)); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* {@inheritdoc} |
| 66 |
*/ |
| 67 |
public function multiply($amount, $multiplier) |
| 68 |
{ |
| 69 |
$multiplier = Number::fromNumber($multiplier); |
| 70 |
|
| 71 |
return bcmul($amount, (string) $multiplier, $this->scale); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* {@inheritdoc} |
| 76 |
*/ |
| 77 |
public function divide($amount, $divisor) |
| 78 |
{ |
| 79 |
$divisor = Number::fromNumber($divisor); |
| 80 |
|
| 81 |
return bcdiv($amount, (string) $divisor, $this->scale); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* {@inheritdoc} |
| 86 |
*/ |
| 87 |
public function ceil($number) |
| 88 |
{ |
| 89 |
$number = Number::fromNumber($number); |
| 90 |
|
| 91 |
if ($number->isInteger()) { |
| 92 |
return (string) $number; |
| 93 |
} |
| 94 |
|
| 95 |
if ($number->isNegative()) { |
| 96 |
return bcadd((string) $number, '0', 0); |
| 97 |
} |
| 98 |
|
| 99 |
return bcadd((string) $number, '1', 0); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* {@inheritdoc} |
| 104 |
*/ |
| 105 |
public function floor($number) |
| 106 |
{ |
| 107 |
$number = Number::fromNumber($number); |
| 108 |
|
| 109 |
if ($number->isInteger()) { |
| 110 |
return (string) $number; |
| 111 |
} |
| 112 |
|
| 113 |
if ($number->isNegative()) { |
| 114 |
return bcadd((string) $number, '-1', 0); |
| 115 |
} |
| 116 |
|
| 117 |
return bcadd($number, '0', 0); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* {@inheritdoc} |
| 122 |
*/ |
| 123 |
public function absolute($number) |
| 124 |
{ |
| 125 |
return ltrim($number, '-'); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* {@inheritdoc} |
| 130 |
*/ |
| 131 |
public function round($number, $roundingMode) |
| 132 |
{ |
| 133 |
$number = Number::fromNumber($number); |
| 134 |
|
| 135 |
if ($number->isInteger()) { |
| 136 |
return (string) $number; |
| 137 |
} |
| 138 |
|
| 139 |
if ($number->isHalf() === false) { |
| 140 |
return $this->roundDigit($number); |
| 141 |
} |
| 142 |
|
| 143 |
if (Money::ROUND_HALF_UP === $roundingMode) { |
| 144 |
return bcadd( |
| 145 |
(string) $number, |
| 146 |
$number->getIntegerRoundingMultiplier(), |
| 147 |
0 |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
if (Money::ROUND_HALF_DOWN === $roundingMode) { |
| 152 |
return bcadd((string) $number, '0', 0); |
| 153 |
} |
| 154 |
|
| 155 |
if (Money::ROUND_HALF_EVEN === $roundingMode) { |
| 156 |
if ($number->isCurrentEven()) { |
| 157 |
return bcadd((string) $number, '0', 0); |
| 158 |
} |
| 159 |
|
| 160 |
return bcadd( |
| 161 |
(string) $number, |
| 162 |
$number->getIntegerRoundingMultiplier(), |
| 163 |
0 |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
if (Money::ROUND_HALF_ODD === $roundingMode) { |
| 168 |
if ($number->isCurrentEven()) { |
| 169 |
return bcadd( |
| 170 |
(string) $number, |
| 171 |
$number->getIntegerRoundingMultiplier(), |
| 172 |
0 |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
return bcadd((string) $number, '0', 0); |
| 177 |
} |
| 178 |
|
| 179 |
if (Money::ROUND_HALF_POSITIVE_INFINITY === $roundingMode) { |
| 180 |
if ($number->isNegative()) { |
| 181 |
return bcadd((string) $number, '0', 0); |
| 182 |
} |
| 183 |
|
| 184 |
return bcadd( |
| 185 |
(string) $number, |
| 186 |
$number->getIntegerRoundingMultiplier(), |
| 187 |
0 |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
if (Money::ROUND_HALF_NEGATIVE_INFINITY === $roundingMode) { |
| 192 |
if ($number->isNegative()) { |
| 193 |
return bcadd( |
| 194 |
(string) $number, |
| 195 |
$number->getIntegerRoundingMultiplier(), |
| 196 |
0 |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
return bcadd( |
| 201 |
(string) $number, |
| 202 |
'0', |
| 203 |
0 |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
throw new \InvalidArgumentException('Unknown rounding mode'); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
private function roundDigit(Number $number) |
| 214 |
{ |
| 215 |
if ($number->isCloserToNext()) { |
| 216 |
return bcadd( |
| 217 |
(string) $number, |
| 218 |
$number->getIntegerRoundingMultiplier(), |
| 219 |
0 |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
return bcadd((string) $number, '0', 0); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* {@inheritdoc} |
| 228 |
*/ |
| 229 |
public function share($amount, $ratio, $total) |
| 230 |
{ |
| 231 |
return $this->floor(bcdiv(bcmul($amount, $ratio, $this->scale), $total, $this->scale)); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* {@inheritdoc} |
| 236 |
*/ |
| 237 |
public function mod($amount, $divisor) |
| 238 |
{ |
| 239 |
return bcmod($amount, $divisor); |
| 240 |
} |
| 241 |
} |
| 242 |
|