Passed
Branch master (9671df)
by Andy
02:10
created

ArrayFormatter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Palmtree\Csv\Formatter;
4
5
class ArrayFormatter extends AbstractFormatter
6
{
7
    protected $delimiter;
8
9
    protected $stringFormatter;
10
11
    /**
12
     * ArrayFormatter constructor.
13
     *
14
     * @param null|FormatterInterface $formatter
15
     * @param string                  $delimiter
16
     */
17 1
    public function __construct($formatter = null, $delimiter = ',')
18
    {
19 1
        $this->setDelimiter($delimiter);
20
21 1
        $this->stringFormatter = new StringFormatter();
22 1
        $this->stringFormatter->setTrimCharMask($this->stringFormatter->getTrimCharMask() . $this->getDelimiter());
23
24 1
        parent::__construct($formatter);
25 1
    }
26
27 1
    public function format($value)
28
    {
29 1
        return $this->getFormattedValue($value);
30
    }
31
32 1
    public function getFormattedValue($value)
33
    {
34 1
        $value          = $this->stringFormatter->format($value);
35 1
        $formattedValue = explode($this->getDelimiter(), $value);
36
37 1
        foreach ($formattedValue as &$part) {
38 1
            $part = $this->getFormatter()->format($part);
39
        }
40
41 1
        return $formattedValue;
42
    }
43
44
    /**
45
     * @param string $delimiter
46
     *
47
     * @return ArrayFormatter
48
     */
49 1
    public function setDelimiter($delimiter)
50
    {
51 1
        $this->delimiter = $delimiter;
52
53 1
        return $this;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59 1
    public function getDelimiter()
60
    {
61 1
        return $this->delimiter;
62
    }
63
}
64