Transformer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
eloc 23
dl 0
loc 92
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 5 2
A transformObject() 0 5 1
A transformAny() 0 9 3
A pipe() 0 8 1
A transformArray() 0 9 2
A __construct() 0 3 1
1
<?php
2
3
namespace Lneicelis\Transformer;
4
5
use Lneicelis\Transformer\Contract\CanPipe;
6
use Lneicelis\Transformer\Contract\CanTransform;
7
use Lneicelis\Transformer\Exception\TransformerNotFoundException;
8
use Lneicelis\Transformer\ValueObject\Context;
9
use Lneicelis\Transformer\ValueObject\Path;
10
11
class Transformer
12
{
13
    /** @var CanPipe[] */
14
    protected $pipes;
15
16
    /**
17
     * @param CanPipe[] $pipes
18
     */
19 14
    public function __construct(array $pipes = [])
20
    {
21 14
        $this->pipes = $pipes;
22 14
    }
23
24
    /**
25
     * @param mixed $resource
26
     * @param Context|null $context
27
     * @return string|int|float|array
28
     * @throws TransformerNotFoundException
29
     */
30 14
    public function transform($resource, Context $context = null)
31
    {
32 14
        $context = $context ?: new Context();
33
34 14
        return $this->transformAny($resource, $context, new Path());
35
    }
36
37
    /**
38
     * @param $resource
39
     * @param Context $context
40
     * @param Path $path
41
     * @return array|float|int|string
42
     * @throws TransformerNotFoundException
43
     */
44 14
    protected function transformAny($resource, Context $context, Path $path)
45
    {
46
        switch (true) {
47 14
            case is_object($resource):
48 9
                return $this->transformObject($resource, $context, $path);
49 14
            case is_array($resource):
50 8
                return $this->transformArray($resource, $context, $path);
51
            default:
52 14
                return $resource;
53
        }
54
    }
55
56
    /**
57
     * @param object $resource
58
     * @param Context $context
59
     * @param Path $path
60
     * @return string|int|float|array
61
     * @throws TransformerNotFoundException
62
     */
63 9
    protected function transformObject($resource, Context $context, Path $path)
64
    {
65 9
        $result = $this->pipe($resource, $context, $path, null);
66
67 9
        return $this->transformAny($result, $context, $path);
68
    }
69
70
    /**
71
     * @param array $resourceByKey
72
     * @param Context $context
73
     * @param Path $path
74
     * @return array
75
     * @throws TransformerNotFoundException
76
     */
77 8
    protected function transformArray(array $resourceByKey, Context $context, Path $path)
78
    {
79 8
        $dataByKey = [];
80
81 8
        foreach ($resourceByKey as $key => $resource) {
82 8
            $dataByKey[$key] = $this->transformAny($resource, $context, $path->stepIn($key));
83
        }
84
85 8
        return $dataByKey;
86
    }
87
88
    /**
89
     * @param object $resource
90
     * @param Context $context
91
     * @param Path $path
92
     * @param string|int|float|array $data
93
     * @return string|int|float|array
94
     */
95 9
    protected function pipe($resource, Context $context, Path $path, $data)
96
    {
97 9
        return array_reduce(
98 9
            $this->pipes,
99 9
            function ($data, CanPipe $middleware) use ($resource, $context, $path) {
100 9
                return $middleware->pipe($resource, $context, $path, $data);
101 9
            },
102 9
            $data
103
        );
104
    }
105
}
106