Test Failed
Push — master ( f0f126...457d36 )
by Andy
01:35
created

BooleanNormalizer::setCaseSensitive()   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
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Palmtree\Csv\Normalizer;
4
5
/**
6
 * BooleanNormalizer formats a CSV cell as a boolean.
7
 */
8
class BooleanNormalizer extends AbstractNormalizer
9
{
10
    /**
11
     * @var array Default truthy/falsey pairs.
12
     */
13
    public static $defaultPairs = [
14
        'true'    => 'false',
15
        '1'       => '0',
16
        'on'      => 'off',
17
        'yes'     => 'no',
18
        'enabled' => 'disabled',
19
    ];
20
21
    /** @var array */
22
    private $values = [];
23
    /** @var bool */
24
    private $nullable = false;
25
    /** @var bool */
26
    private $caseSensitive = false;
27
28 3
    public function __construct(NormalizerInterface $normalizer = null)
29
    {
30 3
        $this->setPairs(self::$defaultPairs);
31
32 3
        parent::__construct($normalizer);
33 3
    }
34
35 3
    public function setPairs(array $pairs): self
36
    {
37 3
        $this->values = [];
38
39 3
        foreach ($pairs as $truthy => $falsey) {
40 3
            $this->addPair($truthy, $falsey);
41
        }
42
43 3
        return $this;
44
    }
45
46 3
    public function addPair(string $truthy, string $falsey): void
47
    {
48 3
        if (!$this->caseSensitive) {
49 3
            $truthy = \strtolower($truthy);
50 3
            $falsey = \strtolower($falsey);
51
        }
52
53 3
        $this->values[$truthy] = true;
54 3
        $this->values[$falsey] = false;
55 3
    }
56
57
    public function getValues(): array
58
    {
59
        return $this->values;
60
    }
61
62
    /**
63
     * @return bool|null
64
     */
65 3
    protected function getNormalizedValue(string $value)
66
    {
67 3
        $value = \trim($value);
68
69 3
        if (!$this->caseSensitive) {
70 3
            $value = \strtolower($value);
71
        }
72
73 3
        if (isset($this->values[$value])) {
74 3
            return $this->values[$value];
75
        }
76
77 2
        return $this->nullable ? null : false;
78
    }
79
80
    /**
81
     * Sets whether a case-sensitive comparison should be made. If this is true, 'Enabled' will not match 'enabled'
82
     * and will return false if it is found (or null if isNullable is true). Defaults to false.
83
     */
84
    public function setCaseSensitive(bool $caseSensitive): self
85
    {
86
        $this->caseSensitive = $caseSensitive;
87
88
        return $this;
89
    }
90
91
    public function isCaseSensitive(): bool
92
    {
93
        return $this->caseSensitive;
94
    }
95
96
    /**
97
     * Sets whether the returned value can be null. Defaults to false, meaning any value present that is
98
     * not found in the truthy values will return false.
99
     */
100 1
    public function setNullable(bool $nullable): self
101
    {
102 1
        $this->nullable = $nullable;
103
104 1
        return $this;
105
    }
106
107
    public function isNullable(): bool
108
    {
109
        return $this->nullable;
110
    }
111
}
112