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 CalculatorBase |
13
|
|
|
* abstract parent class for Money Calculators |
14
|
|
|
* |
15
|
|
|
* @package Event Espresso |
16
|
|
|
* @author Brent Christensen |
17
|
|
|
* @since $VID:$ |
18
|
|
|
*/ |
19
|
|
|
abstract class CalculatorBase implements Calculator |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int|float $amount |
24
|
|
|
* @param int $precision |
25
|
|
|
* @param int $rounding_mode |
26
|
|
|
* @return string |
27
|
|
|
* @throws InvalidArgumentException |
28
|
|
|
*/ |
29
|
|
|
public function round($amount, $precision, $rounding_mode) |
30
|
|
|
{ |
31
|
|
|
$this->verifyRoundingMode($rounding_mode); |
32
|
|
|
if ($rounding_mode === Calculator::ROUND_UP) { |
33
|
|
|
return (string) ceil($amount); |
34
|
|
|
} |
35
|
|
|
if ($rounding_mode === Calculator::ROUND_DOWN) { |
36
|
|
|
return (string) floor($amount); |
37
|
|
|
} |
38
|
|
|
return (string) round($amount, $precision, $rounding_mode); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Asserts that rounding mode is a valid integer value. |
45
|
|
|
* |
46
|
|
|
* @param int $rounding_mode |
47
|
|
|
* @throws InvalidArgumentException If $roundingMode is not valid |
48
|
|
|
*/ |
49
|
|
|
private function verifyRoundingMode($rounding_mode) |
50
|
|
|
{ |
51
|
|
|
if ( |
52
|
|
|
! in_array( |
53
|
|
|
$rounding_mode, |
54
|
|
|
array( |
55
|
|
|
Calculator::ROUND_UP, |
56
|
|
|
Calculator::ROUND_DOWN, |
57
|
|
|
Calculator::ROUND_HALF_DOWN, |
58
|
|
|
Calculator::ROUND_HALF_EVEN, |
59
|
|
|
Calculator::ROUND_HALF_ODD, |
60
|
|
|
Calculator::ROUND_HALF_UP, |
61
|
|
|
), |
62
|
|
|
true |
63
|
|
|
) |
64
|
|
|
) { |
65
|
|
|
throw new InvalidArgumentException( |
66
|
|
|
esc_html__( |
67
|
|
|
'Rounding mode should be one of the following: Calculator::ROUND_UP, Calculator::ROUND_DOWN, Calculator::ROUND_HALF_DOWN, Calculator::ROUND_HALF_EVEN, Calculator::ROUND_HALF_ODD, or Calculator::ROUND_HALF_UP.', |
68
|
|
|
'event_espresso' |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $divisor |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
protected function validateDivisor($divisor) |
81
|
|
|
{ |
82
|
|
|
if ((int)$divisor === 0) { |
83
|
|
|
throw new InvalidArgumentException( |
84
|
|
|
esc_html__('Division by zero.', 'event_espresso') |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
// End of file CalculatorBase.php |
95
|
|
|
// Location: EventEspresso\core\domain\values\money/CalculatorBase.php |
96
|
|
|
|