Test Failed
Push — master ( 68a257...701355 )
by Andy
07:45 queued 11s
created

ArrayNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 16
c 1
b 0
f 0
dl 0
loc 48
ccs 17
cts 19
cp 0.8947
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getNormalizedValue() 0 11 4
A normalize() 0 3 1
A __construct() 0 5 1
A setDelimiter() 0 8 1
A getDelimiter() 0 3 1
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 2
    public function __construct(?NormalizerInterface $normalizer = null)
13
    {
14 2
        $this->setDelimiter(',');
15
16 2
        parent::__construct($normalizer);
17 2
    }
18
19 2
    public function normalize(string $value)
20
    {
21 2
        return $this->getNormalizedValue($value);
22
    }
23
24 2
    protected function getNormalizedValue(string $value)
25
    {
26 2
        $normalizedValue = \explode($this->delimiter, $this->stringNormalizer->normalize($value)) ?: [];
27
28 2
        if ($this->normalizer) {
29 1
            foreach ($normalizedValue as &$part) {
30 1
                $part = $this->normalizer->normalize($part);
31
            }
32
        }
33
34 2
        return $normalizedValue;
35
    }
36
37
    /**
38
     * Sets the delimiter to pass to explode(). Defaults to , (comma).
39
     */
40 2
    public function setDelimiter(string $delimiter): self
41
    {
42 2
        $this->delimiter = $delimiter;
43
44 2
        $this->stringNormalizer = new StringNormalizer();
45 2
        $this->stringNormalizer->addTrimChar($this->delimiter);
46
47 2
        return $this;
48
    }
49
50
    public function getDelimiter(): string
51
    {
52
        return $this->delimiter;
53
    }
54
}
55