|
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\PayeeBankgiro; |
|
26
|
|
|
use byrokrat\autogiro\Tree\PayeeBgcNumber; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Validate that payee bankgiro and BGC customer number are constant within tree |
|
30
|
|
|
*/ |
|
31
|
|
|
class PayeeVisitor extends ErrorAwareVisitor |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string Payee bankgiro account number |
|
35
|
|
|
*/ |
|
36
|
|
|
private $payeeBg; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string Payee BGC customer number |
|
40
|
|
|
*/ |
|
41
|
|
|
private $payeeBgcNr; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Reset payee bankgiro and customer number before a new file is traversed |
|
45
|
|
|
*/ |
|
46
|
|
|
public function beforeAutogiroFile(): void |
|
47
|
|
|
{ |
|
48
|
|
|
$this->payeeBg = ''; |
|
49
|
|
|
$this->payeeBgcNr = ''; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Validate payee bankgiro number |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function beforePayeeBankgiro(PayeeBankgiro $node): void |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->payeeBg) { |
|
58
|
|
|
$this->payeeBg = $node->getValue(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($node->getValue() != $this->payeeBg) { |
|
62
|
|
|
$this->getErrorObject()->addError( |
|
63
|
|
|
"Non-matching payee bankgiro numbers (expecting: %s, found: %s) on line %s", |
|
64
|
|
|
$this->payeeBg, |
|
65
|
|
|
$node->getValue(), |
|
66
|
|
|
(string)$node->getLineNr() |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Validate payee BGC customer number |
|
73
|
|
|
*/ |
|
74
|
|
View Code Duplication |
public function beforePayeeBgcNumber(PayeeBgcNumber $node): void |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
if (!$this->payeeBgcNr) { |
|
77
|
|
|
$this->payeeBgcNr = $node->getValue(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if ($node->getValue() != $this->payeeBgcNr) { |
|
81
|
|
|
$this->getErrorObject()->addError( |
|
82
|
|
|
"Non-matching payee BGC customer numbers (expecting: %s, found: %s) on line %s", |
|
83
|
|
|
$this->payeeBgcNr, |
|
84
|
|
|
$node->getValue(), |
|
85
|
|
|
(string)$node->getLineNr() |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.