Open Journal Systems  3.3.0
PhpCalculator.php
1 <?php
2 
3 namespace Money\Calculator;
4 
6 use Money\Money;
7 use Money\Number;
8 
12 final class PhpCalculator implements Calculator
13 {
17  public static function supported()
18  {
19  return true;
20  }
21 
25  public function compare($a, $b)
26  {
27  return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
28  }
29 
33  public function add($amount, $addend)
34  {
35  $result = $amount + $addend;
36 
37  $this->assertInteger($result);
38 
39  return (string) $result;
40  }
41 
45  public function subtract($amount, $subtrahend)
46  {
47  $result = $amount - $subtrahend;
48 
49  $this->assertInteger($result);
50 
51  return (string) $result;
52  }
53 
57  public function multiply($amount, $multiplier)
58  {
59  $result = $amount * $multiplier;
60 
61  $this->assertIntegerBounds($result);
62 
63  return (string) Number::fromNumber($result);
64  }
65 
69  public function divide($amount, $divisor)
70  {
71  $result = $amount / $divisor;
72 
73  $this->assertIntegerBounds($result);
74 
75  return (string) Number::fromNumber($result);
76  }
77 
81  public function ceil($number)
82  {
83  return $this->castInteger(ceil($number));
84  }
85 
89  public function floor($number)
90  {
91  return $this->castInteger(floor($number));
92  }
93 
97  public function absolute($number)
98  {
99  $result = ltrim($number, '-');
100 
101  $this->assertIntegerBounds($result);
102 
103  return (string) $result;
104  }
105 
109  public function round($number, $roundingMode)
110  {
111  if (Money::ROUND_HALF_POSITIVE_INFINITY === $roundingMode) {
112  $number = Number::fromNumber($number);
113 
114  if ($number->isHalf()) {
115  return $this->castInteger(ceil((string) $number));
116  }
117 
118  return $this->castInteger(round((string) $number, 0, Money::ROUND_HALF_UP));
119  }
120 
121  if (Money::ROUND_HALF_NEGATIVE_INFINITY === $roundingMode) {
122  $number = Number::fromNumber($number);
123 
124  if ($number->isHalf()) {
125  return $this->castInteger(floor((string) $number));
126  }
127 
128  return $this->castInteger(round((string) $number, 0, Money::ROUND_HALF_DOWN));
129  }
130 
131  return $this->castInteger(round($number, 0, $roundingMode));
132  }
133 
137  public function share($amount, $ratio, $total)
138  {
139  return $this->castInteger(floor($amount * $ratio / $total));
140  }
141 
145  public function mod($amount, $divisor)
146  {
147  $result = $amount % $divisor;
148 
149  $this->assertIntegerBounds($result);
150 
151  return (string) $result;
152  }
153 
163  private function assertIntegerBounds($amount)
164  {
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)');
169  }
170  }
171 
179  private function castInteger($amount)
180  {
181  $this->assertIntegerBounds($amount);
182 
183  return (string) intval($amount);
184  }
185 
191  private function assertInteger($amount)
192  {
193  if (filter_var($amount, FILTER_VALIDATE_INT) === false) {
194  throw new \UnexpectedValueException('The result of arithmetic operation is not an integer');
195  }
196  }
197 }
Money\Money\ROUND_HALF_DOWN
const ROUND_HALF_DOWN
Definition: Money.php:22
Money\Money
Definition: Money.php:16
Money\Money\ROUND_HALF_NEGATIVE_INFINITY
const ROUND_HALF_NEGATIVE_INFINITY
Definition: Money.php:34
Money\Calculator\PhpCalculator\round
round($number, $roundingMode)
Definition: PhpCalculator.php:109
Money\Calculator\PhpCalculator\compare
compare($a, $b)
Definition: PhpCalculator.php:25
Money\Calculator
Definition: BcMathCalculator.php:3
Money\Calculator\PhpCalculator\divide
divide($amount, $divisor)
Definition: PhpCalculator.php:69
Money\Calculator
Definition: Calculator.php:10
Money\Number
Definition: paymethod/paypal/vendor/moneyphp/money/src/Number.php:10
Money\Number\fromNumber
static fromNumber($number)
Definition: paymethod/paypal/vendor/moneyphp/money/src/Number.php:84
Money\Calculator\PhpCalculator\subtract
subtract($amount, $subtrahend)
Definition: PhpCalculator.php:45
Money\Calculator\PhpCalculator\add
add($amount, $addend)
Definition: PhpCalculator.php:33
Money\Calculator\PhpCalculator\multiply
multiply($amount, $multiplier)
Definition: PhpCalculator.php:57
Money\Calculator\PhpCalculator\ceil
ceil($number)
Definition: PhpCalculator.php:81
Money\Calculator\PhpCalculator\absolute
absolute($number)
Definition: PhpCalculator.php:97
Money\Calculator\PhpCalculator\floor
floor($number)
Definition: PhpCalculator.php:89
Money\Calculator\PhpCalculator
Definition: PhpCalculator.php:12
Money\Calculator\PhpCalculator\share
share($amount, $ratio, $total)
Definition: PhpCalculator.php:137
Money\Money\ROUND_HALF_POSITIVE_INFINITY
const ROUND_HALF_POSITIVE_INFINITY
Definition: Money.php:32
Money\Money\ROUND_HALF_UP
const ROUND_HALF_UP
Definition: Money.php:20
Money\Calculator\PhpCalculator\supported
static supported()
Definition: PhpCalculator.php:17
Money\Calculator\PhpCalculator\mod
mod($amount, $divisor)
Definition: PhpCalculator.php:145