Passed
Push — master ( 25c805...999286 )
by Andy
02:56 queued 11s
created

NumberNormalizer::getDecimals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Palmtree\Csv\Normalizer;
4
5
/**
6
 * NumberNormalizer converts numeric strings to integers and floats.
7
 */
8
class NumberNormalizer extends AbstractNormalizer
9
{
10
    /** @var int|null */
11
    private $decimals;
12
13 1
    public function setDecimals(?int $decimals = null): self
14
    {
15 1
        $this->decimals = $decimals;
16
17 1
        return $this;
18
    }
19
20
    public function getDecimals(): ?int
21
    {
22
        return $this->decimals;
23
    }
24
25 6
    protected function getNormalizedValue(string $value)
26
    {
27 6
        if (!\is_numeric($value)) {
28
            return 0;
29
        }
30
31 6
        $numberValue = \trim($value) * 1;
32
33 6
        if ($this->decimals !== null) {
34 1
            $numberValue = \round($numberValue, $this->decimals);
35
        }
36
37 6
        return $numberValue;
38
    }
39
}
40