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