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

StringFormatter::setTrimCharMask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Palmtree\Csv\Formatter;
4
5
/**
6
 * StringFormatter formats a CSV cell as a string.
7
 * It will trim the string by default.
8
 */
9
class StringFormatter extends AbstractFormatter
10
{
11
    protected $trim = true;
12
    protected $trimCharMask = " \t\n\r\0\x0B";
13
14 6
    public function __construct($formatter = null, $trim = true, $trimCharMask = null)
15
    {
16 6
        parent::__construct($formatter);
17
18 6
        $this->setTrim($trim);
19
20 6
        if ($trimCharMask) {
21
            $this->setTrimCharMask($trimCharMask);
22
        }
23 6
    }
24
25
    /**
26
     * @param bool $trim
27
     *
28
     * @return StringFormatter
29
     */
30 6
    public function setTrim($trim)
31
    {
32 6
        $this->trim = $trim;
33
34 6
        return $this;
35
    }
36
37
    /**
38
     * @param null|string $trimCharMask
39
     *
40
     * @return StringFormatter
41
     */
42 1
    public function setTrimCharMask($trimCharMask)
43
    {
44 1
        $this->trimCharMask = $trimCharMask;
45
46 1
        return $this;
47
    }
48
49
    /**
50
     * @return string
51
     */
52 3
    public function getTrimCharMask()
53
    {
54 3
        return $this->trimCharMask;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60 4
    public function isTrim()
61
    {
62 4
        return $this->trim;
63
    }
64
65 4
    protected function getFormattedValue($value)
66
    {
67 4
        if ($this->isTrim()) {
68 3
            $value = trim($value, $this->getTrimCharMask());
69
        }
70
71 4
        return $value;
72
    }
73
}
74