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

StringNormalizer::setTrimChars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Palmtree\Csv\Normalizer;
4
5
/**
6
 * StringNormalizer formats a CSV cell as a string.
7
 * It will trim the string by default.
8
 */
9
class StringNormalizer extends AbstractNormalizer
10
{
11
    /** @var bool */
12
    private $trim = true;
13
    /** @var array */
14
    private $trimChars = [' ', "\t", "\n", "\r", "\0", "\x0B"];
15
16
    /**
17
     * Sets whether the string should be trimmed. Defaults to true.
18
     */
19 2
    public function setTrim(bool $trim): self
20
    {
21 2
        $this->trim = $trim;
22
23 2
        return $this;
24
    }
25
26
    /**
27
     * Sets the character mask passed to trim(). Defaults to the mask used by trim itself.
28
     */
29
    public function setTrimChars(array $trimChars): self
30
    {
31
        $this->trimChars = $trimChars;
32
33
        return $this;
34
    }
35
36 2
    public function addTrimChar(string $char): self
37
    {
38 2
        $this->trimChars[] = $char;
39
40 2
        return $this;
41
    }
42
43
    public function getTrimChars(): array
44
    {
45
        return $this->trimChars;
46
    }
47
48
    public function shouldTrim(): bool
49
    {
50
        return $this->trim;
51
    }
52
53 4
    protected function getNormalizedValue(string $value): string
54
    {
55 4
        if ($this->trim) {
56 3
            $value = \trim($value, \implode('', $this->trimChars));
57
        }
58
59 4
        return $value;
60
    }
61
}
62