1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of byrokrat\autogiro. |
4
|
|
|
* |
5
|
|
|
* byrokrat\autogiro 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 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. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
* |
18
|
|
|
* Copyright 2016-18 Hannes Forsgård |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
22
|
|
|
|
23
|
|
|
namespace byrokrat\autogiro\Visitor; |
24
|
|
|
|
25
|
|
|
use byrokrat\autogiro\MessageRetriever; |
26
|
|
|
use byrokrat\autogiro\Tree\Node; |
27
|
|
|
use byrokrat\autogiro\Tree\Text; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Visitor of message nodes in tree |
31
|
|
|
* |
32
|
|
|
* Creates string message as child 'Text' |
33
|
|
|
*/ |
34
|
|
|
class MessageVisitor extends ErrorAwareVisitor |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var MessageRetriever |
38
|
|
|
*/ |
39
|
|
|
private $messages; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $layoutName = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $recordName = ''; |
50
|
|
|
|
51
|
|
|
public function __construct(ErrorObject $errorObj, MessageRetriever $messages = null) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($errorObj); |
54
|
|
|
$this->messages = $messages ?: new MessageRetriever; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function beforeAutogiroFile(Node $node): void |
58
|
|
|
{ |
59
|
|
|
$this->layoutName = $node->getName(); |
60
|
|
|
$this->recordName = ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function beforeRecord(Node $node): void |
64
|
|
|
{ |
65
|
|
|
$this->recordName = $node->getName(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function beforeMessage(Node $node): void |
69
|
|
|
{ |
70
|
|
|
$code = (string)$node->getValueFrom('Number'); |
71
|
|
|
|
72
|
|
|
$message = $this->messages->readMessage( |
73
|
|
|
$this->layoutName, |
74
|
|
|
$this->recordName, |
75
|
|
|
$node->getName(), |
76
|
|
|
$code |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
if (!$message) { |
80
|
|
|
$this->getErrorObject()->addError( |
81
|
|
|
"Invalid message id '%s:%s:%s:%s' on line %s", |
82
|
|
|
$this->layoutName, |
83
|
|
|
$this->recordName, |
84
|
|
|
$node->getName(), |
85
|
|
|
$code, |
86
|
|
|
(string)$node->getLineNr() |
87
|
|
|
); |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$node->addChild(new Text($node->getLineNr(), $message)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|