1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\currency; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class DefaultCalculator |
13
|
|
|
* does not require any special server extensions loaded so will work anywhere |
14
|
|
|
* |
15
|
|
|
* @package Event Espresso |
16
|
|
|
* @author Brent Christensen |
17
|
|
|
* @since $VID:$ |
18
|
|
|
*/ |
19
|
|
|
class DefaultCalculator extends CalculatorBase |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* returns true if a calculator is supported as determined by loaded server extensions |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function isSupported() |
30
|
|
|
{ |
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* adds the supplied Money amount to this Money amount |
38
|
|
|
* and returns a new Money object |
39
|
|
|
* |
40
|
|
|
* @param float|int|string $amount |
41
|
|
|
* @param float|int|string $amount_to_add |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function add($amount, $amount_to_add) |
45
|
|
|
{ |
46
|
|
|
return (string) ($amount + $amount_to_add); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* subtracts the supplied Money amount from this Money amount |
53
|
|
|
* and returns a new Money object |
54
|
|
|
* |
55
|
|
|
* @param float|int|string $amount |
56
|
|
|
* @param float|int|string $amount_to_subtract |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function subtract($amount, $amount_to_subtract) |
60
|
|
|
{ |
61
|
|
|
return (string) ($amount - $amount_to_subtract); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* multiplies this Money amount by the supplied $multiplier |
68
|
|
|
* and returns a new Money object |
69
|
|
|
* |
70
|
|
|
* @param float|int|string $amount |
71
|
|
|
* @param float|int|string $multiplier |
72
|
|
|
* @param int $precision |
73
|
|
|
* @param int $rounding_mode |
74
|
|
|
* @return string |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
public function multiply($amount, $multiplier, $precision = 3, $rounding_mode = Calculator::ROUND_HALF_UP) |
78
|
|
|
{ |
79
|
|
|
return $this->round($amount * $multiplier, $precision, $rounding_mode); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* divides this Money amount by the supplied $divisor |
86
|
|
|
* and returns a new Money object |
87
|
|
|
* |
88
|
|
|
* @param float|int|string $amount |
89
|
|
|
* @param float|int|string $divisor |
90
|
|
|
* @param int $precision |
91
|
|
|
* @param int $rounding_mode |
92
|
|
|
* @return string |
93
|
|
|
* @throws InvalidArgumentException |
94
|
|
|
*/ |
95
|
|
|
public function divide($amount, $divisor, $precision = 3, $rounding_mode = Calculator::ROUND_HALF_UP) |
96
|
|
|
{ |
97
|
|
|
$this->validateDivisor($divisor); |
98
|
|
|
return $this->round($amount / $divisor, $precision, $rounding_mode); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
|
103
|
|
|
} |
104
|
|
|
|