Completed
Push — master ( ec86c3...703f3f )
by Hannes
02:24
created

MessageVisitor::beforeInterval()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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