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

StringNormalizer::trim()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
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
/**
8
 * StringNormalizer formats a CSV cell as a string.
9
 * It will trim the string by default.
10
 */
11
class StringNormalizer extends AbstractNormalizer
12
{
13
    private bool $trim = true;
14
    private array $trimChars = [' ', "\t", "\n", "\r", "\0", "\x0B"];
15
16
    /**
17
     * Sets whether the string should be trimmed. Defaults to true.
18
     */
19 2
    public function trim(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 trimChars(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 4
    protected function getNormalizedValue(string $value): string
44
    {
45 4
        if ($this->trim) {
46 3
            $value = trim($value, implode('', $this->trimChars));
47
        }
48
49 4
        return $value;
50
    }
51
}
52