EmptyValueToNullTransformer::transform()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 7.7777
c 0
b 0
f 0
cc 8
eloc 9
nc 5
nop 1
crap 8
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Data\Transformer;
4
5
/**
6
 * Transforms empty values to null. This is not the exact equivalent of the
7
 * empty() function. The difference is that this transformer will leave 0 and
8
 * false alone, as they could be valid feed values.
9
 */
10
class EmptyValueToNullTransformer implements TransformerInterface
11
{
12
    /**
13
     * @inheritdoc
14
     */
15 28
    public function transform($value)
16
    {
17
        // let booleans, integers and floats pass
18 28
        if (is_null($value) || is_bool($value) || is_integer($value) || is_float($value)) {
19 12
            return $value;
20
        }
21
22 16
        if (is_string($value)) {
23 12
            if (trim($value) === '') {
24 4
                return;
25
            }
26 14
        } elseif (empty($value)) {
27 2
            return;
28
        }
29
30 12
        return $value;
31
    }
32
}
33