Completed
Push — master ( 1cc7be...c5c5ef )
by Andy
04:04
created

NumberNormalizer::getNormalizedValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 6
cts 7
cp 0.8571
rs 10
cc 3
nc 3
nop 1
crap 3.0261
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 */
11
    private $decimals;
12
13
    /**
14
     * @param int|null $decimals
15
     */
16 6
    public function __construct(NormalizerInterface $normalizer = null, $decimals = null)
17
    {
18 6
        $this->setDecimals($decimals);
19
20 6
        parent::__construct($normalizer);
21 6
    }
22
23
    /**
24
     * @param int|null $decimals
25
     *
26
     * @return self
27
     */
28 6
    public function setDecimals($decimals)
29
    {
30 6
        $this->decimals = $decimals;
31
32 6
        return $this;
33
    }
34
35
    /**
36
     * @return int|null
37
     */
38 6
    public function getDecimals()
39
    {
40 6
        return $this->decimals;
41
    }
42
43 6
    protected function getNormalizedValue($value)
44
    {
45 6
        if (!\is_numeric($value)) {
46
            return 0;
47
        }
48
49 6
        $numberValue = \trim($value) * 1;
50
51 6
        if ($this->getDecimals() !== null) {
52 1
            $numberValue = \round($numberValue, $this->getDecimals());
53
        }
54
55 6
        return $numberValue;
56
    }
57
}
58