StringNormalizer   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 2
b 0
f 0
dl 0
loc 39
ccs 10
cts 13
cp 0.7692
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addTrimChar() 0 5 1
A getNormalizedValue() 0 7 2
A trim() 0 5 1
A trimChars() 0 5 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