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\Writer; |
24
|
|
|
|
25
|
|
|
use byrokrat\autogiro\Visitor\Visitor; |
26
|
|
|
use byrokrat\autogiro\Exception\RuntimeException; |
27
|
|
|
use byrokrat\autogiro\Exception\LogicException; |
28
|
|
|
use byrokrat\autogiro\Tree\Node; |
29
|
|
|
use byrokrat\amount\Currency\SEK; |
30
|
|
|
use byrokrat\banking\AccountNumber; |
31
|
|
|
use byrokrat\id\IdInterface; |
32
|
|
|
use byrokrat\id\PersonalId; |
33
|
|
|
use byrokrat\id\OrganizationId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Visitor that generates files to bgc from parse trees |
37
|
|
|
*/ |
38
|
|
|
class PrintingVisitor extends Visitor |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* End-of-line chars used when generating files |
42
|
|
|
*/ |
43
|
|
|
const EOL = "\r\n"; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Output |
47
|
|
|
*/ |
48
|
|
|
private $output; |
49
|
|
|
|
50
|
|
|
public function setOutput(Output $output): void |
51
|
|
|
{ |
52
|
|
|
$this->output = $output; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function beforeDate(Node $node): void |
56
|
|
|
{ |
57
|
|
|
if ($node->getValue() instanceof \DateTimeInterface) { |
58
|
|
|
$this->output->write($node->getValue()->format('Ymd')); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function beforeImmediateDate(): void |
63
|
|
|
{ |
64
|
|
|
$this->output->write('GENAST '); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function beforeText(Node $node): void |
68
|
|
|
{ |
69
|
|
|
$this->output->write($node->getValue()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function beforePayerNumber(Node $node): void |
73
|
|
|
{ |
74
|
|
|
$this->output->write(str_pad($node->getValue(), 16, '0', STR_PAD_LEFT)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function beforePayeeBgcNumber(Node $node): void |
78
|
|
|
{ |
79
|
|
|
$this->output->write(str_pad($node->getValue(), 6, '0', STR_PAD_LEFT)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function beforePayeeBankgiro(Node $node): void |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if ($node->getValue() instanceof AccountNumber) { |
85
|
|
|
$account = $node->getValue(); |
86
|
|
|
$this->output->write( |
87
|
|
|
str_pad($account->getSerialNumber() . $account->getCheckDigit(), 10, '0', STR_PAD_LEFT) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
public function beforeAccount(Node $node): void |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
if ($node->getValue() instanceof AccountNumber) { |
95
|
|
|
$account = $node->getValue(); |
96
|
|
|
$this->output->write( |
97
|
|
|
$account->getClearingNumber() |
98
|
|
|
. str_pad($account->getSerialNumber() . $account->getCheckDigit(), 12, '0', STR_PAD_LEFT) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function beforeInterval(Node $node): void |
104
|
|
|
{ |
105
|
|
|
if (!in_array((int)$node->getValue(), range('0', '8'))) { |
106
|
|
|
throw new RuntimeException('Interval must be between 0 and 8'); |
107
|
|
|
} |
108
|
|
|
$this->output->write($node->getValue()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function beforeAmount(Node $node): void |
112
|
|
|
{ |
113
|
|
|
if ($node->getValue() instanceof SEK) { |
114
|
|
|
$amount = $node->getValue(); |
115
|
|
|
|
116
|
|
|
if ($amount->isGreaterThan(new SEK('9999999999.99')) || $amount->isLessThan(new SEK('-9999999999.99'))) { |
117
|
|
|
throw new RuntimeException('Amount must be between 9999999999.99 and -9999999999.99'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->output->write( |
121
|
|
|
str_pad($amount->getSignalString(), 12, '0', STR_PAD_LEFT) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function beforeStateId(Node $node): void |
127
|
|
|
{ |
128
|
|
|
if ($node->getValue() instanceof PersonalId) { |
129
|
|
|
$this->output->write($node->getValue()->format('Ymdsk')); |
130
|
|
|
} |
131
|
|
|
if ($node->getValue() instanceof OrganizationId) { |
132
|
|
|
$this->output->write($node->getValue()->format('00Ssk')); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function beforeOpening(): void |
137
|
|
|
{ |
138
|
|
|
$this->output->write('01'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function beforeCreateMandateRequest(): void |
142
|
|
|
{ |
143
|
|
|
$this->output->write('04'); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function beforeDeleteMandateRequest(): void |
147
|
|
|
{ |
148
|
|
|
$this->output->write('03'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function beforeAcceptDigitalMandateRequest(): void |
152
|
|
|
{ |
153
|
|
|
$this->output->write('04'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function beforeRejectDigitalMandateRequest(): void |
157
|
|
|
{ |
158
|
|
|
$this->output->write('04'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function beforeUpdateMandateRequest(): void |
162
|
|
|
{ |
163
|
|
|
$this->output->write('05'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function beforeIncomingPaymentRequest(): void |
167
|
|
|
{ |
168
|
|
|
$this->output->write('82'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function beforeOutgoingPaymentRequest(): void |
172
|
|
|
{ |
173
|
|
|
$this->output->write('32'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function beforeAmendmentRequest(): void |
177
|
|
|
{ |
178
|
|
|
$this->output->write('23'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function afterRecord(): void |
182
|
|
|
{ |
183
|
|
|
$this->output->write(self::EOL); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
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.