NormalizedArrayTransformer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 41
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B transform() 0 22 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