Taxes::fromFloat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the G.L.S.R. Apps package.
7
 *
8
 * (c) Dev-Int Création <[email protected]>.
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Core\Domain\Common\Model\Dependent;
15
16
use NumberFormatter;
17
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
39
    {
40
        return new self($rate);
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
52
    {
53
        return $this->rate;
54
    }
55
56
    public function name(): string
57
    {
58
        return $this->name;
59
    }
60
}
61