StringToBooleanTransformer::transform()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 31
Code Lines 15

Duplication

Lines 5
Ratio 16.13 %

Code Coverage

Tests 16
CRAP Score 7

Importance

Changes 0
Metric Value
dl 5
loc 31
ccs 16
cts 16
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 1
crap 7
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Data\Transformer;
4
5
use TreeHouse\Feeder\Exception\TransformationFailedException;
6
7
class StringToBooleanTransformer implements TransformerInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $defaultTruthyValues = [
13
        'true',
14
        'yes',
15
        'on',
16
    ];
17
18
    /**
19
     * @var array
20
     */
21
    protected $defaultFalsyValues = [
22
        'false',
23
        'no',
24
        'off',
25
    ];
26
27
    /**
28
     * @var array
29
     */
30
    protected $truthyValues;
31
32
    /**
33
     * @var array
34
     */
35
    protected $falsyValues;
36
37
    /**
38
     * @param array $truthy
39
     * @param array $falsy
40
     * @param bool  $merge
41
     */
42 36
    public function __construct(array $truthy = [], array $falsy = [], $merge = true)
43
    {
44 36
        if (empty($truthy)) {
45 36
            $truthy = $this->defaultTruthyValues;
46 36
        } elseif ($merge) {
47 2
            $truthy = array_merge($this->defaultTruthyValues, $truthy);
48 2
        }
49
50 36
        if (empty($falsy)) {
51 36
            $falsy = $this->defaultFalsyValues;
52 36
        } elseif ($merge) {
53 2
            $falsy = array_merge($this->defaultFalsyValues, $falsy);
54 2
        }
55
56 36
        $this->truthyValues = array_map('mb_strtolower', $truthy);
57 36
        $this->falsyValues = array_map('mb_strtolower', $falsy);
58 36
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 36
    public function transform($value)
64
    {
65
        // only transform when we have something to transform
66 36
        if (is_null($value)) {
67 2
            return $value;
68
        }
69
70 34
        if (is_bool($value)) {
71 4
            return $value;
72
        }
73
74 30 View Code Duplication
        if (!is_scalar($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75 2
            throw new TransformationFailedException(
76 2
                sprintf('Expected a scalar value to transform, got %s instead.', var_export($value, true))
77 2
            );
78
        }
79
80 28
        if (trim($value) === '') {
81 2
            return null;
82
        }
83
84 26
        if (in_array(mb_strtolower($value), $this->truthyValues)) {
85 8
            return true;
86
        }
87
88 18
        if (in_array(mb_strtolower($value), $this->falsyValues)) {
89 8
            return false;
90
        }
91
92 10
        return (boolean) $value;
93
    }
94
}
95