NormalizedArrayTransformer::transform()   B
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 11
nc 7
nop 1
crap 6
1
<?php
2
3
namespace TreeHouse\Feeder\Modifier\Data\Transformer;
4
5
use TreeHouse\Feeder\Exception\TransformationFailedException;
6
7
class NormalizedArrayTransformer implements TransformerInterface
8
{
9
    /**
10
     * @var bool
11
     */
12
    protected $nestedArrays;
13
14
    /**
15
     * @param bool $nestedArrays
16
     */
17 14
    public function __construct($nestedArrays = false)
18
    {
19 14
        $this->nestedArrays = $nestedArrays;
20 14
    }
21
22
    /**
23
     * @inheritdoc
24
     */
25 14
    public function transform($value)
26
    {
27 14
        if (is_null($value)) {
28 4
            return null;
29
        }
30
31 10
        if (is_scalar($value)) {
32 4
            $value = [$value];
33 4
        }
34
35 10
        if (!is_array($value)) {
36 2
            throw new TransformationFailedException(
37 2
                sprintf('Expected a scalar value or array to transform, got "%s" instead.', json_encode($value))
38 2
            );
39
        }
40
41 8
        if ($this->nestedArrays && !is_numeric(key($value))) {
42 2
            $value = [$value];
43 2
        }
44
45 8
        return $value;
46
    }
47
}
48