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\MessageRetriever; |
27
|
|
|
use byrokrat\autogiro\Tree\Node; |
28
|
|
|
use byrokrat\autogiro\Tree\Text; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Visitor of message nodes in tree |
32
|
|
|
* |
33
|
|
|
* Creates string message as child 'Text' |
34
|
|
|
*/ |
35
|
|
|
final class MessageVisitor extends Visitor |
36
|
|
|
{ |
37
|
|
|
use ErrorAwareTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var MessageRetriever |
41
|
|
|
*/ |
42
|
|
|
private $messages; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $layoutName = ''; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
private $recordName = ''; |
53
|
|
|
|
54
|
|
|
public function __construct(ErrorObject $errorObj, MessageRetriever $messages = null) |
55
|
|
|
{ |
56
|
|
|
$this->setErrorObject($errorObj); |
57
|
|
|
$this->messages = $messages ?: new MessageRetriever(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function beforeAutogiroFile(Node $node): void |
61
|
|
|
{ |
62
|
|
|
$this->layoutName = $node->getName(); |
63
|
|
|
$this->recordName = ''; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function beforeRecord(Node $node): void |
67
|
|
|
{ |
68
|
|
|
$this->recordName = $node->getName(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function beforeMessage(Node $node): void |
72
|
|
|
{ |
73
|
|
|
$code = (string)$node->getValueFrom(Node::NUMBER); |
74
|
|
|
|
75
|
|
|
$message = $this->messages->readMessage( |
76
|
|
|
$this->layoutName, |
77
|
|
|
$this->recordName, |
78
|
|
|
$node->getName(), |
79
|
|
|
$code |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
if (!$message) { |
83
|
|
|
$this->getErrorObject()->addError( |
84
|
|
|
"Invalid message id '%s:%s:%s:%s' on line %s", |
85
|
|
|
$this->layoutName, |
86
|
|
|
$this->recordName, |
87
|
|
|
$node->getName(), |
88
|
|
|
$code, |
89
|
|
|
(string)$node->getLineNr() |
90
|
|
|
); |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$node->addChild(new Text($node->getLineNr(), $message)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|