Completed
Push — master ( 984d76...8152ca )
by WEBEWEB
04:38
created

AbstractTransformerSet   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 67
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 10 3
A setTransformers() 0 4 1
A transform() 0 7 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) {
43
        $this->transformers[] = $transformer;
44
        return $this;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getTransformers() {
51
        return $this->transformers;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function removeTransformer(TransformerInterface $transformer) {
58
        $count = count($this->transformers);
59
        for ($i = $count - 1; 0 <= $i; --$i) {
60
            if ($transformer !== $this->transformers[$i]) {
61
                continue;
62
            }
63
            unset($this->transformers[$i]);
64
        }
65
        return $this;
66
    }
67
68
    /**
69
     * Set the transformers.
70
     *
71
     * @param TransformerInterface[] $transformers The transformers.
72
     * @return TransformerSetInterface Returns this transformer set.
73
     */
74
    protected function setTransformers(array $transformers) {
75
        $this->transformers = $transformers;
76
        return $this;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function transform($value) {
83
        $result = $value;
84
        foreach ($this->getTransformers() as $current) {
85
            $result = $current->transform($result);
86
        }
87
        return $result;
88
    }
89
}
90