Passed
Pull Request — main (#32)
by
unknown
04:32 queued 47s
created

FloatFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 0
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Num\Num;
8
use MichaelRubel\Formatters\Formatter;
9
10
class FloatFormatter implements Formatter
11
{
12
    /**
13
     * @param int|float|string|null $value The value to format.
14
     * @param string|null $decimal_separator The decimal separator to use for formatting.
15
     */
16 5
    public function __construct(
17
        public int|float|string|null $value = null,
18
        public ?string $decimal_separator = null,
19
    ) {
20 5
    }
21
22
    /**
23
     * Get the valid decimal separator or null.
24
     *
25
     * @param string|null $decimal_separator The decimal separator to convert.
26
     * @return string|null The valid decimal separator or null.
27
     */
28 5
    private function getDecimalSeparator(?string $decimal_separator): ?string
29
    {
30
        switch ($decimal_separator) {
31 5
            case '.':
32 1
                return '.';
33 4
            case ',':
34 1
                return ',';
35
            default:
36 3
                return null;
37
        }
38
    }
39
40
    /**
41
     * Format the value as a float using the specified decimal separator.
42
     *
43
     * @return float The formatted float value.
44
     */
45 5
    public function format(): float
46
    {
47 5
        return Num::float($this->value, $this->getDecimalSeparator($this->decimal_separator));
48
    }
49
}
50