Total Complexity | 6 |
Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
18 | final class Taxes |
||
19 | { |
||
20 | private float $rate; |
||
21 | private string $name; |
||
22 | |||
23 | public function __construct(float $rate) |
||
24 | { |
||
25 | $fmtPercent = new NumberFormatter('fr_FR', NumberFormatter::PERCENT); |
||
26 | $fmtPercent->setAttribute($fmtPercent::FRACTION_DIGITS, 2); |
||
27 | |||
28 | $this->rate = $rate / 100; |
||
29 | |||
30 | $fraction = \explode('.', (string) $rate); |
||
31 | if (\strlen($fraction[1]) > 2) { |
||
32 | $this->rate = $rate; |
||
33 | } |
||
34 | |||
35 | $this->name = $fmtPercent->format($this->rate); |
||
36 | } |
||
37 | |||
38 | public static function fromFloat(float $rate): self |
||
41 | } |
||
42 | |||
43 | public static function fromPercent(string $name): self |
||
44 | { |
||
45 | \preg_match('/^(\d*)(,(\d*?)) %$/u', \trim($name), $str); |
||
46 | $float = $str[1] . '.' . $str[3]; |
||
47 | |||
48 | return new self((float) $float); |
||
49 | } |
||
50 | |||
51 | public function rate(): float |
||
54 | } |
||
55 | |||
56 | public function name(): string |
||
61 |