1 | <?php |
||
17 | abstract class AbstractNumberType implements NumberTypeInterface |
||
18 | { |
||
19 | use ValueType; |
||
20 | use Transmutable; |
||
21 | use Boxable; |
||
22 | |||
23 | /** |
||
24 | * @var int |
||
25 | */ |
||
26 | private $precision; |
||
27 | |||
28 | /** |
||
29 | * @var number |
||
30 | */ |
||
31 | protected $value; |
||
32 | |||
33 | /** |
||
34 | * @var MathAdapterInterface |
||
35 | */ |
||
36 | protected $mathAdapter; |
||
37 | |||
38 | /** |
||
39 | * AbstractNumberType constructor. |
||
40 | * Precision order of priority: Argument != null > $num's precision > null precision. |
||
41 | * So for an int, 0 should be passed for precision, otherwise it will auto-convert to float (if null or $num > 0). |
||
42 | * |
||
43 | * @param number $num |
||
44 | * @param int|null $precision |
||
45 | * @param MathAdapterInterface|null $mathAdapter |
||
46 | */ |
||
47 | 60 | public function __construct($num, int $precision = null, MathAdapterInterface $mathAdapter = null) |
|
54 | |||
55 | /** |
||
56 | * Sums current NumberTypeInterface and number in argument. |
||
57 | * |
||
58 | * @param NumberTypeInterface|number|StringType|string $num |
||
59 | * |
||
60 | * @return NumberTypeInterface |
||
61 | */ |
||
62 | 2 | public function plus($num): NumberTypeInterface |
|
66 | |||
67 | /*** |
||
68 | * Subtracts number passed from current NumberTypeInterface. |
||
69 | * |
||
70 | * @param NumberTypeInterface|number|StringType|string $num |
||
71 | * |
||
72 | * @return NumberTypeInterface |
||
73 | */ |
||
74 | 2 | public function minus($num): NumberTypeInterface |
|
78 | |||
79 | /** |
||
80 | * Multiplies current NumberTypeInterface by the number passed. |
||
81 | * |
||
82 | * @param NumberTypeInterface|number|StringType|string $num |
||
83 | * |
||
84 | * @return NumberTypeInterface |
||
85 | */ |
||
86 | 2 | public function multipliedBy($num): NumberTypeInterface |
|
90 | |||
91 | /** |
||
92 | * Divides current NumberTypeInterface by the number passed. |
||
93 | * |
||
94 | * @param NumberTypeInterface|number|StringType|string $num |
||
95 | * |
||
96 | * @return NumberTypeInterface |
||
97 | */ |
||
98 | 2 | public function dividedBy($num): NumberTypeInterface |
|
102 | |||
103 | /** |
||
104 | * Compares current NumberTypeInterface to value passed. |
||
105 | * Same rules as spaceship or version_compare. |
||
106 | * |
||
107 | * @param NumberTypeInterface|number|StringType|string $num |
||
108 | * |
||
109 | * @return NumberTypeInterface |
||
110 | */ |
||
111 | 2 | public function compare($num): NumberTypeInterface |
|
115 | |||
116 | /** |
||
117 | * Returns value of NumberTypeInterface modulus num. |
||
118 | * |
||
119 | * @param NumberTypeInterface|number|StringType|string $num |
||
120 | * |
||
121 | * @return NumberTypeInterface |
||
122 | */ |
||
123 | 2 | public function modulus($num): NumberTypeInterface |
|
127 | |||
128 | /** |
||
129 | * Returns NumberTypeInterface to the power of num. |
||
130 | * |
||
131 | * @param NumberTypeInterface|number|StringType|string $num |
||
132 | * |
||
133 | * @return NumberTypeInterface |
||
134 | */ |
||
135 | 2 | public function power($num): NumberTypeInterface |
|
139 | |||
140 | /** |
||
141 | * Returns the square root of NumberTypeInterface. |
||
142 | * |
||
143 | * @return NumberTypeInterface |
||
144 | */ |
||
145 | 2 | public function squareRoot(): NumberTypeInterface |
|
155 | |||
156 | /** |
||
157 | * Returns the absolute value of NumberTypeInterface. |
||
158 | * |
||
159 | * @return NumberTypeInterface |
||
160 | */ |
||
161 | 2 | public function absolute(): NumberTypeInterface |
|
168 | |||
169 | /** |
||
170 | * Returns the negated/opposite of NumberTypeInterface value. |
||
171 | * |
||
172 | * @return NumberTypeInterface |
||
173 | */ |
||
174 | 2 | public function negate(): NumberTypeInterface |
|
181 | |||
182 | /** |
||
183 | * Returns NumberTypeInterface factorial. |
||
184 | * |
||
185 | * @return NumberTypeInterface |
||
186 | */ |
||
187 | 2 | public function factorial(): NumberTypeInterface |
|
194 | |||
195 | /** |
||
196 | * Returns the greatest common divider between NumberTypeInterface and num. |
||
197 | * |
||
198 | * @param NumberTypeInterface|number|StringType|string $num |
||
199 | * |
||
200 | * @return NumberTypeInterface |
||
201 | */ |
||
202 | 2 | public function gcd($num): NumberTypeInterface |
|
206 | |||
207 | /** |
||
208 | * Returns the root of NumberTypeInterface to the num. |
||
209 | * |
||
210 | * @param int $num |
||
211 | * |
||
212 | * @return NumberTypeInterface |
||
213 | */ |
||
214 | 2 | public function root(int $num): NumberTypeInterface |
|
221 | |||
222 | /** |
||
223 | * Return the next prime number after NumberTypeInterface. |
||
224 | * |
||
225 | * @return NumberTypeInterface |
||
226 | */ |
||
227 | 2 | public function getNextPrime(): NumberTypeInterface |
|
231 | |||
232 | /** |
||
233 | * Returns true of NumberTypeInterface is prime. False otherwise. |
||
234 | * |
||
235 | * @return BooleanType |
||
236 | */ |
||
237 | 2 | public function isPrime(): BooleanType |
|
241 | |||
242 | /** |
||
243 | * Returns true if NumberTypeInterface is a perfect square. False otherwise. |
||
244 | * |
||
245 | * @return BooleanType |
||
246 | */ |
||
247 | 2 | public function isPerfectSquare(): BooleanType |
|
251 | |||
252 | /** |
||
253 | * Gets the current precision (Should be 0 for IntType). |
||
254 | * |
||
255 | * @return int |
||
256 | */ |
||
257 | 60 | public function getPrecision(): int |
|
261 | |||
262 | /** |
||
263 | * @param callable $converterFunction |
||
264 | * @param $mixed |
||
265 | * |
||
266 | * @return mixed |
||
267 | */ |
||
268 | 48 | protected static function asSubType(callable $converterFunction, $mixed) |
|
290 | |||
291 | /** |
||
292 | * @return MathAdapterInterface |
||
293 | */ |
||
294 | 46 | protected function getMathAdapter() |
|
298 | |||
299 | /** |
||
300 | * @param string $operation |
||
301 | * @param $operand |
||
302 | * |
||
303 | * @return NumberTypeInterface |
||
304 | */ |
||
305 | 16 | private function getAdapterOperation(string $operation, $operand): NumberTypeInterface |
|
326 | } |
||
327 |