Completed
Push — master ( 7dbffa...714da8 )
by Hannes
05:26
created

TextVisitor::beforePayerNumberNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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-17 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Visitor;
24
25
use byrokrat\autogiro\Tree\IntervalNode;
26
use byrokrat\autogiro\Tree\PayeeBgcNumberNode;
27
use byrokrat\autogiro\Tree\PayerNumberNode;
28
use byrokrat\autogiro\Tree\RepetitionsNode;
29
use byrokrat\autogiro\Tree\TextNode;
30
31
/**
32
 * Validate the content of text nodes
33
 */
34
class TextVisitor extends ErrorAwareVisitor
35
{
36
    public function beforeTextNode(TextNode $node)
37
    {
38
        $this->validateRegexp($node, "Text value '%s' does not match expected %s on line %s");
39
    }
40
41
    public function beforeRepetitionsNode(RepetitionsNode $node)
42
    {
43
        $this->validateRegexp($node, "Repeats '%s' does not match expected %s on line %s");
44
    }
45
46
    public function beforePayeeBgcNumberNode(PayeeBgcNumberNode $node)
47
    {
48
        $this->validateRegexp($node, "BGC customer number '%s' does not match expected %s on line %s");
49
    }
50
51
    public function beforePayerNumberNode(PayerNumberNode $node)
52
    {
53
        $this->validateRegexp($node, "Payer number '%s' does not match expected %s on line %s");
54
    }
55
56
    private function validateRegexp(TextNode $node, string $errorMsg)
57
    {
58
        if (!$node->hasAttribute('validation_regexp') || !$node->getAttribute('validation_regexp')) {
59
            return;
60
        }
61
62
        if (!preg_match($node->getAttribute('validation_regexp'), $node->getValue())) {
63
            $this->getErrorObject()->addError(
64
                $errorMsg,
65
                $node->getValue(),
66
                $node->getAttribute('validation_regexp'),
67
                (string)$node->getLineNr()
68
            );
69
        }
70
    }
71
}
72