Transformer::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
declare (strict_types=1);
4
5
namespace Oxidmod\RestBundle\Transformer;
6
7
use League\Fractal\TransformerAbstract;
8
use Oxidmod\RestBundle\Transformer\Exception\UnsupportedObjectException;
9
10
/**
11
 * Transformer for fractal lib
12
 */
13
class Transformer extends TransformerAbstract
14
{
15
    /**
16
     * @var TransformerInterface[]
17
     */
18
    private $transformers = [];
19
20
    /**
21
     * @param TransformerInterface[] $transformers
22
     */
23
    public function __construct(array $transformers = [])
24
    {
25
        foreach ($transformers as $transformer) {
26
            $this->addTransformer($transformer);
27
        }
28
    }
29
30
    /**
31
     * @param TransformerInterface $transformer
32
     */
33
    private function addTransformer(TransformerInterface $transformer)
34
    {
35
        $this->transformers[] = $transformer;
36
    }
37
38
    /**
39
     * @param mixed $object
40
     *
41
     * @return array
42
     *
43
     * @throws UnsupportedObjectException
44
     */
45
    public function __invoke($object): array
46
    {
47
        foreach ($this->transformers as $transformer) {
48
            if ($transformer->supports($object)) {
49
                return $transformer->transform($object);
50
            }
51
        }
52
53
        throw new UnsupportedObjectException($object);
54
    }
55
}
56