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

NumberNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 13
c 1
b 0
f 0
dl 0
loc 48
ccs 15
cts 16
cp 0.9375
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedValue() 0 13 3
A getDecimals() 0 3 1
A __construct() 0 5 1
A setDecimals() 0 5 1
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