Completed
Push — master ( d2b1b9...c80d49 )
by Antoine
22s queued 13s
created

ChainDataTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A transform() 0 9 3
A supportsTransformation() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\DataTransformer;
15
16
/**
17
 * Transforms an Input to a Resource object.
18
 *
19
 * @author Antoine Bluchet <[email protected]>
20
 */
21
final class ChainDataTransformer implements DataTransformerInterface
22
{
23
    private $transformers;
24
25
    public function __construct(iterable $transformers)
26
    {
27
        $this->transformers = $transformers;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function transform($object, string $to, array $context = [])
34
    {
35
        foreach ($this->transformers as $transformer) {
36
            if ($transformer->supportsTransformation($object, $to, $context)) {
37
                return $transformer->transform($object, $to, $context);
38
            }
39
        }
40
41
        return $object;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function supportsTransformation($object, string $to, array $context = []): bool
48
    {
49
        foreach ($this->transformers as $transformer) {
50
            if ($transformer->supportsTransformation($object, $to, $context)) {
51
                return true;
52
            }
53
        }
54
55
        return false;
56
    }
57
}
58