27 return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
33 public function add($amount, $addend)
35 $result = $amount + $addend;
37 $this->assertInteger($result);
39 return (
string) $result;
47 $result = $amount - $subtrahend;
49 $this->assertInteger($result);
51 return (
string) $result;
59 $result = $amount * $multiplier;
61 $this->assertIntegerBounds($result);
69 public function divide($amount, $divisor)
71 $result = $amount / $divisor;
73 $this->assertIntegerBounds($result);
81 public function ceil($number)
83 return $this->castInteger(
ceil($number));
91 return $this->castInteger(
floor($number));
99 $result = ltrim($number,
'-');
101 $this->assertIntegerBounds($result);
103 return (
string) $result;
109 public function round($number, $roundingMode)
114 if ($number->isHalf()) {
115 return $this->castInteger(
ceil((
string) $number));
124 if ($number->isHalf()) {
125 return $this->castInteger(
floor((
string) $number));
131 return $this->castInteger(
round($number, 0, $roundingMode));
137 public function share($amount, $ratio, $total)
139 return $this->castInteger(
floor($amount * $ratio / $total));
145 public function mod($amount, $divisor)
147 $result = $amount % $divisor;
149 $this->assertIntegerBounds($result);
151 return (
string) $result;
163 private function assertIntegerBounds($amount)
165 if ($amount > PHP_INT_MAX) {
166 throw new \OverflowException(
'You overflowed the maximum allowed integer (PHP_INT_MAX)');
167 } elseif ($amount < ~PHP_INT_MAX) {
168 throw new \UnderflowException(
'You underflowed the minimum allowed integer (PHP_INT_MAX)');
179 private function castInteger($amount)
181 $this->assertIntegerBounds($amount);
183 return (
string) intval($amount);
191 private function assertInteger($amount)
193 if (filter_var($amount, FILTER_VALIDATE_INT) ===
false) {
194 throw new \UnexpectedValueException(
'The result of arithmetic operation is not an integer');