Passed
Push — master ( 25c805...999286 )
by Andy
02:56 queued 11s
created

ArrayNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
dl 0
loc 46
ccs 18
cts 20
cp 0.9
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 3 1
A __construct() 0 8 1
A setDelimiter() 0 5 1
A getDelimiter() 0 3 1
A getNormalizedValue() 0 12 4
1
<?php
2
3
namespace Palmtree\Csv\Normalizer;
4
5
class ArrayNormalizer extends AbstractNormalizer
6
{
7
    /** @var string */
8
    private $delimiter;
9
    /** @var StringNormalizer */
10
    private $stringNormalizer;
11
12 1
    public function __construct(NormalizerInterface $normalizer = null, string $delimiter = ',')
13
    {
14 1
        $this->setDelimiter($delimiter);
15
16 1
        $this->stringNormalizer = new StringNormalizer();
17 1
        $this->stringNormalizer->setTrimCharMask($this->stringNormalizer->getTrimCharMask() . $this->delimiter);
18
19 1
        parent::__construct($normalizer);
20 1
    }
21
22 1
    public function normalize(string $value)
23
    {
24 1
        return $this->getNormalizedValue($value);
25
    }
26
27 1
    protected function getNormalizedValue(string $value)
28
    {
29 1
        $value           = $this->stringNormalizer->normalize($value);
30 1
        $normalizedValue = \explode($this->delimiter, $value) ?: [];
31
32 1
        if ($this->normalizer) {
33 1
            foreach ($normalizedValue as &$part) {
34 1
                $part = $this->normalizer->normalize($part);
35
            }
36
        }
37
38 1
        return $normalizedValue;
39
    }
40
41 1
    public function setDelimiter(string $delimiter): self
42
    {
43 1
        $this->delimiter = $delimiter;
44
45 1
        return $this;
46
    }
47
48
    public function getDelimiter(): string
49
    {
50
        return $this->delimiter;
51
    }
52
}
53