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

NumberNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 52
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

4 Methods

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