Test Failed
Push — master ( f0f126...457d36 )
by Andy
01:35
created

ArrayNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 70%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
dl 0
loc 49
ccs 14
cts 20
cp 0.7
rs 10
c 1
b 0
f 0

5 Methods

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