Completed
Push — master ( 98aac0...15b539 )
by Hannes
02:24
created

ContainingVisitor::getVisitors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro\Visitor.
4
 *
5
 * byrokrat\autogiro\Visitor is free software: you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * byrokrat\autogiro\Visitor is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with byrokrat\autogiro\Visitor. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Visitor;
24
25
use byrokrat\autogiro\Tree\Node;
26
27
/**
28
 * Container for multiple visitors
29
 */
30
class ContainingVisitor extends ErrorAwareVisitor
31
{
32
    /**
33
     * @var Visitor[] Contained visitors
34
     */
35
    private $visitors;
36
37
    public function __construct(ErrorObject $errorObj, Visitor ...$visitors)
38
    {
39
        parent::__construct($errorObj);
40
        $this->visitors = $visitors;
41
    }
42
43
    /**
44
     * Get contained visitors
45
     *
46
     * @return Visitor[]
47
     */
48
    public function getVisitors(): array
49
    {
50
        return $this->visitors;
51
    }
52
53
    /**
54
     * Add a visitor to container
55
     */
56
    public function addVisitor(Visitor $visitor)
57
    {
58
        $this->visitors[] = $visitor;
59
    }
60
61
    /**
62
     * Delegate visit before to registered visitors
63
     */
64
    public function visitBefore(Node $node)
65
    {
66
        parent::visitBefore($node);
67
68
        foreach ($this->visitors as $visitor) {
69
            $visitor->visitBefore($node);
70
        }
71
    }
72
73
    /**
74
     * Delegate visit after to registered visitors
75
     */
76
    public function visitAfter(Node $node)
77
    {
78
        foreach ($this->visitors as $visitor) {
79
            $visitor->visitAfter($node);
80
        }
81
82
        parent::visitAfter($node);
83
    }
84
85
    /**
86
     * Reset the error container before a file node
87
     */
88
    public function beforeFileNode()
89
    {
90
        $this->getErrorObject()->resetErrors();
91
    }
92
93
    /**
94
     * Throw exception if there are errors after iteration
95
     */
96
    public function afterFileNode()
97
    {
98
        if ($this->getErrorObject()->hasErrors()) {
99
            // TODO komponera meddelande på bättre sätt...
100
            throw new \Exception();
101
        }
102
    }
103
}
104