AbstractTransformerSet   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addTransformer() 0 4 1
A getTransformers() 0 3 1
A removeTransformer() 0 14 3
A setTransformers() 0 4 1
A transform() 0 9 2
1
<?php
2
3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
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
namespace WBW\Library\Core\Transformer\Set;
13
14
use WBW\Library\Core\Transformer\API\TransformerInterface;
15
use WBW\Library\Core\Transformer\API\TransformerSetInterface;
16
17
/**
18
 * Abstract transformer set.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Transformer\Set
22
 */
23
abstract class AbstractTransformerSet implements TransformerSetInterface {
24
25
    /**
26
     * Transformers.
27
     *
28
     * @var TransformerInterface[]
29
     */
30
    private $transformers;
31
32
    /**
33
     * Constructor.
34
     */
35
    public function __construct() {
36
        $this->setTransformers([]);
37
    }
38
39
    /**
40
     * {@inheritDoc}
41
     */
42
    public function addTransformer(TransformerInterface $transformer): TransformerSetInterface {
43
        $this->transformers[] = $transformer;
44
        return $this;
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50
    public function getTransformers(): array {
51
        return $this->transformers;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function removeTransformer(TransformerInterface $transformer): TransformerSetInterface {
58
59
        $count = count($this->transformers);
60
        for ($i = $count - 1; 0 <= $i; --$i) {
61
62
            if ($transformer !== $this->transformers[$i]) {
63
                continue;
64
            }
65
66
            unset($this->transformers[$i]);
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Set the transformers.
74
     *
75
     * @param TransformerInterface[] $transformers The transformers.
76
     * @return TransformerSetInterface Returns this transformer set.
77
     */
78
    protected function setTransformers(array $transformers): TransformerSetInterface {
79
        $this->transformers = $transformers;
80
        return $this;
81
    }
82
83
    /**
84
     * {@inheritDoc}
85
     */
86
    public function transform($value) {
87
88
        $result = $value;
89
        foreach ($this->getTransformers() as $current) {
90
            $result = $current->transform($result);
91
        }
92
93
        return $result;
94
    }
95
}
96