Completed
Push — master ( ac6025...434cc0 )
by Andy
07:49
created

NumberNormalizer::getNormalizedValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 5
cts 7
cp 0.7143
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 3.2098
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
    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
        if (!is_numeric($value)) {
48
            return 0;
49
        }
50
51 3
        $numberValue = trim($value) + 0;
52
53 3
        if ($this->getDecimals() !== null) {
54
            $numberValue = round($numberValue, $this->getDecimals());
55
        }
56
57 3
        return $numberValue;
58
    }
59
}
60