Completed
Push — master ( db5ad0...ac6025 )
by Andy
06:43
created

NumberNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Palmtree\Csv\Normalizer;
4
5
/**
6
 * NumberNormalizer converts numeric strings to ints and floats.
7
 */
8
class NumberNormalizer extends AbstractNormalizer
9
{
10
    protected $decimals;
11
12
    /**
13
     * NumberNormalizer constructor.
14
     *
15
     * @param null|NormalizerInterface $normalizer
16
     * @param null|int                 $decimals
17
     */
18 3
    public function __construct($normalizer = null, $decimals = null)
19
    {
20 3
        $this->setDecimals($decimals);
21
22 3
        parent::__construct($normalizer);
23 3
    }
24
25
    /**
26
     * @param null|int $decimals
27
     *
28
     * @return NumberNormalizer
29
     */
30 3
    public function setDecimals($decimals)
31
    {
32 3
        $this->decimals = $decimals;
33
34 3
        return $this;
35
    }
36
37
    /**
38
     * @return null|int
39
     */
40 3
    public function getDecimals()
41
    {
42 3
        return $this->decimals;
43
    }
44
45 3
    protected function getNormalizedValue($value)
46
    {
47 3
        $numberValue = is_numeric($value) ? trim($value) + 0 : 0;
48
49 3
        if ($this->getDecimals() !== null) {
50
            $numberValue = round($numberValue, $this->getDecimals());
51
        }
52
53 3
        return $numberValue;
54
    }
55
}
56