Passed
Branch master (9671df)
by Andy
02:10
created

NumberFormatter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 48
ccs 13
cts 14
cp 0.9286
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 getFormattedValue() 0 10 3
1
<?php
2
3
namespace Palmtree\Csv\Formatter;
4
5
/**
6
 * NumberFormatter converts numeric strings to ints and floats.
7
 */
8
class NumberFormatter extends AbstractFormatter
9
{
10
    protected $decimals;
11
12
    /**
13
     * NumberFormatter constructor.
14
     *
15
     * @param null|FormatterInterface $formatter
16
     * @param null|int                $decimals
17
     */
18 3
    public function __construct($formatter = null, $decimals = null)
19
    {
20 3
        $this->setDecimals($decimals);
21
22 3
        parent::__construct($formatter);
23 3
    }
24
25
    /**
26
     * @param null|int $decimals
27
     *
28
     * @return NumberFormatter
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 getFormattedValue($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