Completed
Push — master ( 03230b...27b37b )
by Hannes
02:11
created

TextProcessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
wmc 5
lcom 0
cbo 3
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeTextNode() 0 13 3
A beforeRepetitionsNode() 0 11 2
1
<?php
2
/**
3
 * This file is part of byrokrat\autogiro\Processor.
4
 *
5
 * byrokrat\autogiro\Processor 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\Processor 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\Processor. If not, see <http://www.gnu.org/licenses/>.
17
 *
18
 * Copyright 2016 Hannes Forsgård
19
 */
20
21
declare(strict_types = 1);
22
23
namespace byrokrat\autogiro\Processor;
24
25
use byrokrat\autogiro\Tree\IntervalNode;
26
use byrokrat\autogiro\Tree\RepetitionsNode;
27
use byrokrat\autogiro\Tree\TextNode;
28
29
/**
30
 * Validate the content of text nodes
31
 */
32
class TextProcessor extends Processor
33
{
34
    /**
35
     * Validate that text nodes contain values matching a regular expression
36
     */
37
    public function beforeTextNode(TextNode $node)
38
    {
39
        $regexp = $node->getAttribute('validation_regexp');
40
41
        if ($regexp && !preg_match($regexp, $node->getValue())) {
42
            $this->addError(
43
                "Text value '%s' does not match expected %s on line %s",
44
                $node->getValue(),
45
                $regexp,
46
                (string)$node->getLineNr()
47
            );
48
        }
49
    }
50
51
    /**
52
     * Validate that repetition nodes contain values matching a regular expression
53
     */
54
    public function beforeRepetitionsNode(RepetitionsNode $node)
55
    {
56
        if (!preg_match($node->getAttribute('validation_regexp'), $node->getValue())) {
57
            $this->addError(
58
                "Repeats '%s' does not match expected %s on line %s",
59
                $node->getValue(),
60
                $node->getAttribute('validation_regexp'),
61
                (string)$node->getLineNr()
62
            );
63
        }
64
    }
65
}
66