Passed
Push — master ( 201e0e...28aced )
by Andy
01:40
created

ArrayNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 3 1
A __construct() 0 6 1
A getDelimiter() 0 3 1
A getNormalizedValue() 0 11 4
A setDelimiter() 0 8 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
        if ($this->normalizer) {
30 1
            foreach ($normalizedValue as &$part) {
31 1
                $part = $this->normalizer->normalize($part);
32
            }
33
        }
34
35 2
        return $normalizedValue;
36
    }
37
38
    /**
39
     * Sets the delimiter to pass to explode(). Defaults to , (comma).
40
     */
41 1
    public function setDelimiter(string $delimiter): self
42
    {
43 1
        $this->delimiter = $delimiter;
44
45 1
        $this->stringNormalizer = new StringNormalizer();
46 1
        $this->stringNormalizer->addTrimChar($this->delimiter);
47
48 1
        return $this;
49
    }
50
51
    public function getDelimiter(): string
52
    {
53
        return $this->delimiter;
54
    }
55
}
56