|
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\Intervals; |
|
26
|
|
|
use byrokrat\autogiro\Tree\AutogiroFile; |
|
27
|
|
|
use byrokrat\autogiro\Tree\ImmediateDate; |
|
28
|
|
|
use byrokrat\autogiro\Tree\Number; |
|
29
|
|
|
use byrokrat\autogiro\Tree\Obj; |
|
30
|
|
|
use byrokrat\autogiro\Tree\Record; |
|
31
|
|
|
use byrokrat\autogiro\Tree\Section; |
|
32
|
|
|
use byrokrat\autogiro\Tree\Text; |
|
33
|
|
|
use byrokrat\banking\AccountNumber; |
|
34
|
|
|
use byrokrat\banking\Bankgiro; |
|
35
|
|
|
use byrokrat\id\IdInterface; |
|
36
|
|
|
use byrokrat\amount\Currency\SEK; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Build trees representing autogiro request files |
|
40
|
|
|
*/ |
|
41
|
|
|
class TreeBuilder |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* Map section classes to record store array names |
|
45
|
|
|
*/ |
|
46
|
|
|
private const SECTION_TO_RECORD_STORE_MAP = [ |
|
47
|
|
|
'MandateRequestSection' => 'mandates', |
|
48
|
|
|
'PaymentRequestSection' => 'payments', |
|
49
|
|
|
'AmendmentRequestSection' => 'amendments' |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var Record Opening record used for each section |
|
54
|
|
|
*/ |
|
55
|
|
|
private $opening; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Record[] List of created mandate requests |
|
59
|
|
|
*/ |
|
60
|
|
|
private $mandates; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var Record[] List of created payment requests |
|
64
|
|
|
*/ |
|
65
|
|
|
private $payments; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var Record[] List of created amendment requests |
|
69
|
|
|
*/ |
|
70
|
|
|
private $amendments; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string Payee BGC customer number |
|
74
|
|
|
*/ |
|
75
|
|
|
private $bgcNr; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var Obj Wrapper around payee bankgiro account number |
|
79
|
|
|
*/ |
|
80
|
|
|
private $payeeBgNode; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var \DateTimeInterface Date of file creation |
|
84
|
|
|
*/ |
|
85
|
|
|
private $date; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @var RepititionsFormatter |
|
89
|
|
|
*/ |
|
90
|
|
|
private $repititionsFormatter; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $bgcNr The BGC customer number of payee |
|
94
|
|
|
* @param Bankgiro $bankgiro Payee bankgiro account number |
|
95
|
|
|
* @param \DateTimeInterface $date Creation date |
|
96
|
|
|
* @param RepititionsFormatter $repititionsFormatter Repititions formatter |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __construct( |
|
99
|
|
|
string $bgcNr, |
|
100
|
|
|
Bankgiro $bankgiro, |
|
101
|
|
|
\DateTimeInterface $date, |
|
102
|
|
|
RepititionsFormatter $repititionsFormatter |
|
103
|
|
|
) { |
|
104
|
|
|
$this->bgcNr = $bgcNr; |
|
105
|
|
|
$this->payeeBgNode = new Obj(0, $bankgiro, 'PayeeBankgiro'); |
|
|
|
|
|
|
106
|
|
|
$this->date = $date; |
|
107
|
|
|
$this->repititionsFormatter = $repititionsFormatter; |
|
108
|
|
|
$this->reset(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Reset builder to initial state |
|
113
|
|
|
*/ |
|
114
|
|
|
public function reset(): void |
|
115
|
|
|
{ |
|
116
|
|
|
$this->opening = new Record( |
|
117
|
|
|
'Opening', |
|
118
|
|
|
new Obj(0, $this->date, 'Date'), |
|
|
|
|
|
|
119
|
|
|
new Text(0, 'AUTOGIRO'), |
|
120
|
|
|
new Text(0, str_pad('', 44)), |
|
121
|
|
|
new Number(0, $this->bgcNr, 'PayeeBgcNumber'), |
|
122
|
|
|
$this->payeeBgNode, |
|
123
|
|
|
new Text(0, ' ') |
|
124
|
|
|
); |
|
125
|
|
|
$this->mandates = []; |
|
126
|
|
|
$this->payments = []; |
|
127
|
|
|
$this->amendments = []; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Add a new mandate request to tree |
|
132
|
|
|
*/ |
|
133
|
|
|
public function addCreateMandateRequest(string $payerNr, AccountNumber $account, IdInterface $id): void |
|
134
|
|
|
{ |
|
135
|
|
|
$this->mandates[] = new Record( |
|
136
|
|
|
'CreateMandateRequest', |
|
137
|
|
|
$this->payeeBgNode, |
|
138
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
139
|
|
|
new Obj(0, $account, 'Account'), |
|
|
|
|
|
|
140
|
|
|
new Obj(0, $id, 'StateId'), |
|
|
|
|
|
|
141
|
|
|
new Text(0, str_pad('', 24)) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Add a delete mandate request to tree |
|
147
|
|
|
*/ |
|
148
|
|
|
public function addDeleteMandateRequest(string $payerNr): void |
|
149
|
|
|
{ |
|
150
|
|
|
$this->mandates[] = new Record( |
|
151
|
|
|
'DeleteMandateRequest', |
|
152
|
|
|
$this->payeeBgNode, |
|
153
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
154
|
|
|
new Text(0, str_pad('', 52)) |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Add an accept digital mandate request to tree |
|
160
|
|
|
*/ |
|
161
|
|
|
public function addAcceptDigitalMandateRequest(string $payerNr): void |
|
162
|
|
|
{ |
|
163
|
|
|
$this->mandates[] = new Record( |
|
164
|
|
|
'AcceptDigitalMandateRequest', |
|
165
|
|
|
$this->payeeBgNode, |
|
166
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
167
|
|
|
new Text(0, str_pad('', 52)) |
|
168
|
|
|
); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Add a reject digital mandate request to tree |
|
173
|
|
|
*/ |
|
174
|
|
|
public function addRejectDigitalMandateRequest(string $payerNr): void |
|
175
|
|
|
{ |
|
176
|
|
|
$this->mandates[] = new Record( |
|
177
|
|
|
'RejectDigitalMandateRequest', |
|
178
|
|
|
$this->payeeBgNode, |
|
179
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
180
|
|
|
new Text(0, str_pad('', 48)), |
|
181
|
|
|
new Text(0, 'AV'), |
|
182
|
|
|
new Text(0, ' ') |
|
183
|
|
|
); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Add an update mandate request to tree |
|
188
|
|
|
*/ |
|
189
|
|
|
public function addUpdateMandateRequest(string $payerNr, string $newPayerNr): void |
|
190
|
|
|
{ |
|
191
|
|
|
$this->mandates[] = new Record( |
|
192
|
|
|
'UpdateMandateRequest', |
|
193
|
|
|
$this->payeeBgNode, |
|
194
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
195
|
|
|
$this->payeeBgNode, |
|
196
|
|
|
new Number(0, $newPayerNr, 'PayerNumber'), |
|
197
|
|
|
new Text(0, str_pad('', 26)) |
|
198
|
|
|
); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Add an incoming payment request to tree |
|
203
|
|
|
*/ |
|
204
|
|
|
public function addIncomingPaymentRequest( |
|
205
|
|
|
string $payerNr, |
|
206
|
|
|
SEK $amount, |
|
207
|
|
|
\DateTimeInterface $date, |
|
208
|
|
|
string $ref, |
|
209
|
|
|
string $interval, |
|
210
|
|
|
int $repetitions |
|
211
|
|
|
): void { |
|
212
|
|
|
$this->addPaymentRequest( |
|
213
|
|
|
'IncomingPaymentRequest', |
|
214
|
|
|
$payerNr, |
|
215
|
|
|
$amount, |
|
216
|
|
|
$date, |
|
217
|
|
|
$ref, |
|
218
|
|
|
$interval, |
|
219
|
|
|
$repetitions |
|
220
|
|
|
); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Add an outgoing payment request to tree |
|
225
|
|
|
*/ |
|
226
|
|
|
public function addOutgoingPaymentRequest( |
|
227
|
|
|
string $payerNr, |
|
228
|
|
|
SEK $amount, |
|
229
|
|
|
\DateTimeInterface $date, |
|
230
|
|
|
string $ref, |
|
231
|
|
|
string $interval, |
|
232
|
|
|
int $repetitions |
|
233
|
|
|
): void { |
|
234
|
|
|
$this->addPaymentRequest( |
|
235
|
|
|
'OutgoingPaymentRequest', |
|
236
|
|
|
$payerNr, |
|
237
|
|
|
$amount, |
|
238
|
|
|
$date, |
|
239
|
|
|
$ref, |
|
240
|
|
|
$interval, |
|
241
|
|
|
$repetitions |
|
242
|
|
|
); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Add an incoming payment at next possible bank date request to tree |
|
247
|
|
|
*/ |
|
248
|
|
|
public function addImmediateIncomingPaymentRequest(string $payerNr, SEK $amount, string $ref): void |
|
249
|
|
|
{ |
|
250
|
|
|
$this->addImmediatePaymentRequest('IncomingPaymentRequest', $payerNr, $amount, $ref); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Add an outgoing payment at next possible bank date request to tree |
|
255
|
|
|
*/ |
|
256
|
|
|
public function addImmediateOutgoingPaymentRequest(string $payerNr, SEK $amount, string $ref): void |
|
257
|
|
|
{ |
|
258
|
|
|
$this->addImmediatePaymentRequest('OutgoingPaymentRequest', $payerNr, $amount, $ref); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Add a delete payment request to tree |
|
263
|
|
|
*/ |
|
264
|
|
|
public function addDeletePaymentRequest(string $payerNr): void |
|
265
|
|
|
{ |
|
266
|
|
|
$this->amendments[] = new Record( |
|
267
|
|
|
'AmendmentRequest', |
|
268
|
|
|
$this->payeeBgNode, |
|
269
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
270
|
|
|
new Text(0, str_repeat(' ', 52)) |
|
271
|
|
|
); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Get the created request tree |
|
276
|
|
|
*/ |
|
277
|
|
|
public function buildTree(): AutogiroFile |
|
278
|
|
|
{ |
|
279
|
|
|
$sections = []; |
|
280
|
|
|
|
|
281
|
|
|
foreach (self::SECTION_TO_RECORD_STORE_MAP as $sectionName => $recordStore) { |
|
282
|
|
|
if (!empty($this->$recordStore)) { |
|
283
|
|
|
$sections[] = new Section($sectionName, $this->opening, ...$this->$recordStore); |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return new AutogiroFile('AutogiroRequestFile', ...$sections); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
private function addPaymentRequest( |
|
291
|
|
|
string $nodename, |
|
292
|
|
|
string $payerNr, |
|
293
|
|
|
SEK $amount, |
|
294
|
|
|
\DateTimeInterface $date, |
|
|
|
|
|
|
295
|
|
|
string $ref, |
|
296
|
|
|
string $interval, |
|
297
|
|
|
int $repetitions |
|
298
|
|
|
): void { |
|
299
|
|
|
$this->payments[] = new Record( |
|
300
|
|
|
$nodename, |
|
301
|
|
|
new Obj(0, $this->date, 'Date'), |
|
|
|
|
|
|
302
|
|
|
new Number(0, $interval, 'Interval'), |
|
303
|
|
|
new Text(0, $this->repititionsFormatter->format($repetitions), 'Repetitions'), |
|
304
|
|
|
new Text(0, ' '), |
|
305
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
306
|
|
|
new Obj(0, $amount, 'Amount'), |
|
|
|
|
|
|
307
|
|
|
$this->payeeBgNode, |
|
308
|
|
|
new Text(0, str_pad($ref, 16, ' ', STR_PAD_LEFT)), |
|
309
|
|
|
new Text(0, str_pad('', 11)) |
|
310
|
|
|
); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
private function addImmediatePaymentRequest(string $nodename, string $payerNr, SEK $amount, string $ref): void |
|
314
|
|
|
{ |
|
315
|
|
|
$this->payments[] = new Record( |
|
316
|
|
|
$nodename, |
|
317
|
|
|
new ImmediateDate, |
|
318
|
|
|
new Number(0, Intervals::INTERVAL_ONCE, 'Interval'), |
|
319
|
|
|
new Text(0, ' ', 'Repetitions'), |
|
320
|
|
|
new Text(0, ' '), |
|
321
|
|
|
new Number(0, $payerNr, 'PayerNumber'), |
|
322
|
|
|
new Obj(0, $amount, 'Amount'), |
|
|
|
|
|
|
323
|
|
|
$this->payeeBgNode, |
|
324
|
|
|
new Text(0, str_pad($ref, 16, ' ', STR_PAD_LEFT)), |
|
325
|
|
|
new Text(0, str_pad('', 11)) |
|
326
|
|
|
); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: