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

ArrayNormalizer::setDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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