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

IntFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 9
c 2
b 0
f 2
dl 0
loc 38
ccs 10
cts 10
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 3 1
A getDecimalSeparator() 0 9 3
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 IntFormatter 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 an integer using the specified decimal separator.
42
     *
43
     * @return int The formatted integer value.
44
     */
45 5
    public function format(): int
46
    {
47 5
        return Num::int($this->value, $this->getDecimalSeparator($this->decimal_separator));
48
    }
49
}
50