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

ArrayNormalizer::delimiter()   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 0
Metric Value
eloc 4
c 0
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
declare(strict_types=1);
4
5
namespace Palmtree\Csv\Normalizer;
6
7
class ArrayNormalizer extends AbstractNormalizer
8
{
9
    private string $delimiter = ',';
10
    private StringNormalizer $stringNormalizer;
11
12 2
    public function __construct(?NormalizerInterface $normalizer = null)
13
    {
14 2
        $this->stringNormalizer = new StringNormalizer();
15 2
        $this->stringNormalizer->addTrimChar($this->delimiter);
16
17 2
        parent::__construct($normalizer);
18 2
    }
19
20 2
    public function normalize(string $value)
21
    {
22 2
        return $this->getNormalizedValue($value);
23
    }
24
25 2
    protected function getNormalizedValue(string $value): array
26
    {
27 2
        $normalizedValue = explode($this->delimiter, $this->stringNormalizer->normalize($value)) ?: [];
28
29 2
        foreach ($normalizedValue as &$part) {
30 2
            $part = $this->normalizer->normalize($part);
31
        }
32
33 2
        return $normalizedValue;
34
    }
35
36
    /**
37
     * Sets the delimiter to pass to explode(). Defaults to , (comma).
38
     */
39 1
    public function delimiter(string $delimiter): self
40
    {
41 1
        $this->delimiter = $delimiter;
42
43 1
        $this->stringNormalizer = new StringNormalizer();
44 1
        $this->stringNormalizer->addTrimChar($this->delimiter);
45
46 1
        return $this;
47
    }
48
}
49