Completed
Push — master ( db5ad0...ac6025 )
by Andy
06:43
created

StringNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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
    protected $trim;
13
    /** @var string */
14
    protected $trimCharMask;
15
16 3
    public function __construct($normalizer = null, $trim = true, $trimCharMask = " \t\n\r\0\x0B")
17
    {
18 3
        parent::__construct($normalizer);
19
20 3
        $this->setTrim($trim)
21 3
             ->setTrimCharMask($trimCharMask);
22 3
    }
23
24
    /**
25
     * @param bool $trim
26
     *
27
     * @return StringNormalizer
28
     */
29 3
    public function setTrim($trim)
30
    {
31 3
        $this->trim = (bool)$trim;
32
33 3
        return $this;
34
    }
35
36
    /**
37
     * @param null|string $trimCharMask
38
     *
39
     * @return StringNormalizer
40
     */
41 3
    public function setTrimCharMask($trimCharMask)
42
    {
43 3
        $this->trimCharMask = $trimCharMask;
44
45 3
        return $this;
46
    }
47
48
    /**
49
     * @return string
50
     */
51 2
    public function getTrimCharMask()
52
    {
53 2
        return $this->trimCharMask;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59 3
    public function shouldTrim()
60
    {
61 3
        return $this->trim;
62
    }
63
64 3
    protected function getNormalizedValue($value)
65
    {
66 3
        if ($this->shouldTrim()) {
67 2
            $value = trim($value, $this->getTrimCharMask());
68
        }
69
70 3
        return $value;
71
    }
72
}
73