Completed
Push — master ( db3eef...c2bd5d )
by Hannes
01:54
created

MessageVisitor::beforeFileNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
c 1
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Tree\FileNode;
26
use byrokrat\autogiro\Tree\MessageNode;
27
use byrokrat\autogiro\Tree\IntervalNode;
28
use byrokrat\autogiro\Messages;
29
use byrokrat\autogiro\Intervals;
30
31
/**
32
 * Visitor of message nodes in tree
33
 */
34
class MessageVisitor extends ErrorAwareVisitor
35
{
36
    /**
37
     * @var string
38
     */
39
    private $layout;
40
41
    public function beforeFileNode(FileNode $node): void
42
    {
43
        $this->layout = $node->getAttribute('layout');
44
    }
45
46
    public function beforeMessageNode(MessageNode $node): void
47
    {
48
        if ($node->hasAttribute('message')) {
49
            return;
50
        }
51
52
        $messageId = $node->hasAttribute('message_id')
53
            ? $node->getAttribute('message_id')
54
            : $this->layout . '.' . $node->getValue();
55
56
        if (!isset(Messages::MESSAGE_MAP[$messageId])) {
57
            $this->getErrorObject()->addError(
58
                "Invalid message id %s on line %s",
59
                $messageId,
60
                (string)$node->getLineNr()
61
            );
62
            return;
63
        }
64
65
        $node->setAttribute('message', Messages::MESSAGE_MAP[$messageId]);
66
    }
67
68
    public function beforeIntervalNode(IntervalNode $node): void
69
    {
70
        if ($node->hasAttribute('message')) {
71
            return;
72
        }
73
74
        if (!isset(Intervals::MESSAGE_MAP[$node->getValue()])) {
75
            $this->getErrorObject()->addError(
76
                "Invalid interval %s on line %s",
77
                $node->getValue(),
78
                (string)$node->getLineNr()
79
            );
80
            return;
81
        }
82
83
        $node->setAttribute('message', Intervals::MESSAGE_MAP[$node->getValue()]);
84
    }
85
}
86