Total Complexity | 10 |
Total Lines | 39 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | class PriceHelper |
||
13 | { |
||
14 | public static function buildMoney(string $price, string $currency): ?Money |
||
15 | { |
||
16 | if (!is_numeric($price)) { |
||
17 | throw new \InvalidArgumentException('price of the Money must be numeric'); |
||
18 | } |
||
19 | if (!self::checkFloat($price)) { |
||
20 | return new Money($price, new Currency($currency)); |
||
21 | } |
||
22 | if (!is_int($price) && ($price * 100) == ((int) ($price * 100))) { |
||
|
|||
23 | return (new DecimalMoneyParser(new ISOCurrencies()))->parse($price, new Currency($currency)); |
||
24 | } |
||
25 | return new Money( |
||
26 | (int) ($price * self::calculatePriceRate($price)), |
||
27 | new Currency($currency) |
||
28 | ); |
||
29 | } |
||
30 | |||
31 | public static function calculatePriceRate(string $price): int |
||
32 | { |
||
33 | $price = self::divide($price); |
||
34 | if (!is_array($price)) { |
||
35 | return 1; |
||
36 | } |
||
37 | return (int) (1 . implode(array_fill(0, strlen($price[1]),0))); |
||
38 | } |
||
39 | |||
40 | private static function divide(string $number): array | string |
||
41 | { |
||
42 | if (self::checkFloat($number)) { |
||
43 | return explode('.', $number); |
||
44 | } |
||
45 | return $number; |
||
46 | } |
||
47 | |||
48 | private static function checkFloat(string $number): bool |
||
51 | } |
||
52 | } |
||
53 |