Passed
Push — master ( 24f0b7...5fac61 )
by Andy
01:44
created

NumberNormalizer::scale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Palmtree\Csv\Normalizer;
6
7
/**
8
 * NumberNormalizer converts numeric strings to integers and floats.
9
 */
10
class NumberNormalizer extends AbstractNormalizer
11
{
12
    private ?int $scale = null;
13
14
    /**
15
     * Sets the amount of decimal places to round to. Defaults to null which performs no rounding.
16
     */
17 1
    public function scale(?int $scale = null): self
18
    {
19 1
        $this->scale = $scale;
20
21 1
        return $this;
22
    }
23
24
    /**
25
     * @return float|int
26
     */
27 6
    protected function getNormalizedValue(string $value)
28
    {
29 6
        if (!is_numeric($value)) {
30
            return 0;
31
        }
32
33 6
        $value = trim($value);
34 6
        $value = ltrim($value, '0');
35
36 6
        $numberValue = json_decode($value);
37
38 6
        if ($this->scale !== null) {
39 1
            $numberValue = round($numberValue, $this->scale);
40
        }
41
42 6
        return $numberValue;
43
    }
44
}
45