Total Complexity | 8 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
10 | class FormatNumber implements Plugin |
||
11 | { |
||
12 | /** |
||
13 | * @return array{fn: string} |
||
14 | */ |
||
15 | 1 | public function getCallablesTable(): array |
|
16 | { |
||
17 | return [ |
||
18 | 1 | 'fn' => 'format', |
|
19 | ]; |
||
20 | } |
||
21 | |||
22 | /** @var int */ |
||
23 | private $defaultDecimals; |
||
24 | |||
25 | 6 | public function __construct(int $defaultDecimals = 2) |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Format a number, if the expression is not a number, then uses 0 |
||
32 | * |
||
33 | * @param mixed $number |
||
34 | * @param int $decimals = $this->defaultDecimals() |
||
35 | */ |
||
36 | 1 | public function format($number, int $decimals = -1): string |
|
37 | { |
||
38 | 1 | if ($decimals < 0) { |
|
39 | 1 | $decimals = $this->getDefaultDecimals(); |
|
40 | } |
||
41 | 1 | return number_format(is_numeric($number) ? (float) $number : 0, $decimals); |
|
42 | } |
||
43 | |||
44 | 4 | public function getDefaultDecimals(): int |
|
45 | { |
||
46 | 4 | return $this->defaultDecimals; |
|
47 | } |
||
48 | |||
49 | 6 | public function setDefaultDecimals(int $defaultDecimals): void |
|
55 | } |
||
56 | } |
||
57 |