1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FinanCalc\Utils { |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use FinanCalc\Constants\Defaults; |
7
|
|
|
use FinanCalc\Constants\ErrorMessages; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Helpers |
12
|
|
|
* @package FinanCalc\Utils |
13
|
|
|
*/ |
14
|
|
|
class Helpers |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param $checkedVariable |
18
|
|
|
* @param $nameOfTheExpectedClass |
19
|
|
|
* @return bool |
20
|
|
|
* @throws Exception |
21
|
|
|
*/ |
22
|
|
|
public static function checkIfInstanceOfAClassOrThrowAnException($checkedVariable, $nameOfTheExpectedClass) |
23
|
|
|
{ |
24
|
|
|
if (is_a($checkedVariable, $nameOfTheExpectedClass)) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
throw new InvalidArgumentException(ErrorMessages::getIncompatibleTypesMessage($nameOfTheExpectedClass, |
29
|
|
|
get_class($checkedVariable))); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param $leftOperand |
34
|
|
|
* @param $rightOperand |
35
|
|
|
* @param string $messageOnException |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
|
|
public static function checkIfLeftOperandGreaterOrThrowAnException($leftOperand, $rightOperand, $messageOnException) |
39
|
|
|
{ |
40
|
|
|
if (MathFuncs::comp($leftOperand, $rightOperand) === 1) { |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
throw new InvalidArgumentException($messageOnException); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $checkedVariable |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public static function checkIfPositiveNumberOrThrowAnException($checkedVariable) |
52
|
|
|
{ |
53
|
|
|
if (Helpers::checkIfPositiveNumber($checkedVariable)) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new InvalidArgumentException(ErrorMessages::getMustBePositiveNumberMessage($checkedVariable)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $checkedVariable |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public static function checkIfNotNegativeNumberOrThrowAnException($checkedVariable) |
65
|
|
|
{ |
66
|
|
|
if (Helpers::checkIfNotNegativeNumber($checkedVariable)) { |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new InvalidArgumentException(ErrorMessages::getMustNotBeNegativeNumberMessage($checkedVariable)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param null|\FinanCalc\Constants\AnnuityPaymentTypes $checkedVariable |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public static function checkIfNotNull($checkedVariable) |
78
|
|
|
{ |
79
|
|
|
return $checkedVariable !== null; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $checkedVariable |
84
|
|
|
* @return bool|null |
85
|
|
|
*/ |
86
|
|
|
public static function checkIfPositiveNumber($checkedVariable) |
87
|
|
|
{ |
88
|
|
|
return Helpers::checkNumberRelativityToZero($checkedVariable, 1); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param $checkedVariable |
93
|
|
|
* @return bool|null |
94
|
|
|
*/ |
95
|
|
|
public static function checkIfZero($checkedVariable) |
96
|
|
|
{ |
97
|
|
|
return Helpers::checkNumberRelativityToZero($checkedVariable, 0); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $checkedVariable |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
public static function checkIfNotNegativeNumber($checkedVariable) |
105
|
|
|
{ |
106
|
|
|
return Helpers::checkIfPositiveNumber($checkedVariable) || Helpers::checkIfZero($checkedVariable); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param $checkedVariable |
111
|
|
|
* @return bool|null |
112
|
|
|
*/ |
113
|
|
|
public static function checkIfNegativeNumber($checkedVariable) |
114
|
|
|
{ |
115
|
|
|
return Helpers::checkNumberRelativityToZero($checkedVariable, -1); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $checkedVariable |
120
|
|
|
* @param integer $expectedResult |
121
|
|
|
* @return bool|null |
122
|
|
|
*/ |
123
|
|
|
public static function checkNumberRelativityToZero($checkedVariable, $expectedResult) |
124
|
|
|
{ |
125
|
|
|
if (is_numeric((string)$checkedVariable)) { |
126
|
|
|
return MathFuncs::comp($checkedVariable, "0.00") == $expectedResult; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $inputValue |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public static function roundMoneyForDisplay($inputValue) |
137
|
|
|
{ |
138
|
|
|
return (string)round($inputValue, Defaults::MONEY_DECIMAL_PLACES_DISPLAY); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|