1 | <?php |
||||
2 | /** |
||||
3 | * Swiss Payment Slip |
||||
4 | * |
||||
5 | * @license http://www.opensource.org/licenses/mit-license.php MIT License |
||||
6 | * @copyright 2012-2016 Some nice Swiss guys |
||||
7 | * @author Marc Würth [email protected] |
||||
8 | * @author Manuel Reinhard <[email protected]> |
||||
9 | * @author Peter Siska <[email protected]> |
||||
10 | * @link https://github.com/ravage84/SwissPaymentSlip/ |
||||
11 | */ |
||||
12 | |||||
13 | namespace SwissPaymentSlip\SwissPaymentSlip; |
||||
14 | |||||
15 | use InvalidArgumentException; |
||||
16 | use SwissPaymentSlip\SwissPaymentSlip\Exception\PaymentSlipException; |
||||
17 | use SwissPaymentSlip\SwissPaymentSlip\Exception\DisabledDataException; |
||||
18 | |||||
19 | /** |
||||
20 | * Swiss Payment Slip Data |
||||
21 | * |
||||
22 | * A base class for all Swiss payment slip data classes, |
||||
23 | * which encapsulate all the common data |
||||
24 | * of the various Swiss payment slips types. |
||||
25 | * |
||||
26 | * It actually doesn't do much. It's mostly a data container class to keep |
||||
27 | * including classes from having to care about how a particular slip actually works. |
||||
28 | * |
||||
29 | * But it provides a flexibility of which data it holds, because not always |
||||
30 | * all slip fields are needed in an application. |
||||
31 | * |
||||
32 | * Glossary: |
||||
33 | * ESR = Einzahlungsschein mit Referenznummer |
||||
34 | * ISR, (In-)Payment slip with reference number |
||||
35 | * Summary term for orange payment slips in Switzerland |
||||
36 | * BESR = Banken-Einzahlungsschein mit Referenznummer |
||||
37 | * Banking payment slip with reference number |
||||
38 | * Orange payment slip for paying into a bank account (in contrast to a post cheque account with a VESR) |
||||
39 | * VESR = Verfahren für Einzahlungsschein mit Referenznummer |
||||
40 | * Procedure for payment slip with reference number |
||||
41 | * Orange payment slip for paying into a post cheque account (in contrast to a banking account with a BESR) |
||||
42 | * (B|V)ESR+ = Einzahlungsschein mit Referenznummer ohne Betragsangabe |
||||
43 | * Payment slip with reference number without amount specification |
||||
44 | * An payment slip can be issued without a predefined payment amount |
||||
45 | * ES = Einzahlungsschein |
||||
46 | * IS, (In-)Payment slip |
||||
47 | * Also summary term for all payment slips. |
||||
48 | * Red payment slip for paying into a post cheque or bank account without reference number, with message box |
||||
49 | * |
||||
50 | * @link http://www.six-interbank-clearing.com/en/home/standardization/dta.html Payments in DTA format |
||||
51 | * |
||||
52 | * @todo Implement currency (CHF, EUR), means different prefixes in code line |
||||
53 | * @todo Implement payment on own account, means different prefixes in code line --> edge case! |
||||
54 | * @todo Implement cash on delivery (Nachnahme), means different prefixes in code line --> do it on demand |
||||
55 | * @todo Implement amount check for unrounded (.05) cents, document why (see manual) |
||||
56 | * @todo Create a getBankData method with formatting parameter, e.g. stripping blank lines |
||||
57 | * @todo Create a getRecipientData with formatting parameter, e.g. stripping blank lines |
||||
58 | */ |
||||
59 | abstract class PaymentSlipData |
||||
60 | { |
||||
61 | |||||
62 | /** |
||||
63 | * The array table for calculating the check digit by modulo 10 |
||||
64 | * |
||||
65 | * @var array |
||||
66 | */ |
||||
67 | private $moduloTable = [0, 9, 4, 6, 8, 2, 7, 1, 3, 5]; |
||||
68 | |||||
69 | /** |
||||
70 | * Determines if the payment slip has a recipient bank. Can be disabled for pre-printed payment slips |
||||
71 | * |
||||
72 | * @var bool |
||||
73 | */ |
||||
74 | protected $withBank = true; |
||||
75 | |||||
76 | /** |
||||
77 | * Determines if the payment slip has a account number. Can be disabled for pre-printed payment slips |
||||
78 | * |
||||
79 | * @var bool |
||||
80 | */ |
||||
81 | protected $withAccountNumber = true; |
||||
82 | |||||
83 | /** |
||||
84 | * Determines if the payment slip has a recipient. Can be disabled for pre-printed payment slips |
||||
85 | * |
||||
86 | * @var bool |
||||
87 | */ |
||||
88 | protected $withRecipient = true; |
||||
89 | |||||
90 | /** |
||||
91 | * Determines if it's an ESR or an ESR+ |
||||
92 | * |
||||
93 | * @var bool |
||||
94 | */ |
||||
95 | protected $withAmount = true; |
||||
96 | |||||
97 | /** |
||||
98 | * Determines if the payment slip has a payer. Can be disabled for pre-printed payment slips |
||||
99 | * |
||||
100 | * @var bool |
||||
101 | */ |
||||
102 | protected $withPayer = true; |
||||
103 | |||||
104 | /** |
||||
105 | * The name of the bank |
||||
106 | * |
||||
107 | * @var string |
||||
108 | */ |
||||
109 | protected $bankName = ''; |
||||
110 | |||||
111 | /** |
||||
112 | * The postal code and city of the bank |
||||
113 | * |
||||
114 | * @var string |
||||
115 | */ |
||||
116 | protected $bankCity = ''; |
||||
117 | |||||
118 | /** |
||||
119 | * The bank or post cheque account where the money will be transferred to |
||||
120 | * |
||||
121 | * @var string |
||||
122 | */ |
||||
123 | protected $accountNumber = ''; |
||||
124 | |||||
125 | /** |
||||
126 | * The first line of the recipient, e.g. "My Company Ltd." |
||||
127 | * |
||||
128 | * @var string |
||||
129 | */ |
||||
130 | protected $recipientLine1 = ''; |
||||
131 | |||||
132 | /** |
||||
133 | * The second line of the recipient, e.g. "Examplestreet 61" |
||||
134 | * |
||||
135 | * @var string |
||||
136 | */ |
||||
137 | protected $recipientLine2 = ''; |
||||
138 | |||||
139 | /** |
||||
140 | * The third line of the recipient, e.g. "8000 Zürich" |
||||
141 | * |
||||
142 | * @var string |
||||
143 | */ |
||||
144 | protected $recipientLine3 = ''; |
||||
145 | |||||
146 | /** |
||||
147 | * The fourth line of the recipient, if needed |
||||
148 | * |
||||
149 | * @var string |
||||
150 | */ |
||||
151 | protected $recipientLine4 = ''; |
||||
152 | |||||
153 | /** |
||||
154 | * The amount to be payed into. Can be disabled with withAmount = false for ESR+ slips |
||||
155 | * |
||||
156 | * @var float |
||||
157 | */ |
||||
158 | protected $amount = 0.0; |
||||
159 | |||||
160 | /** |
||||
161 | * The first line of the payer, e.g. "Hans Mustermann" |
||||
162 | * |
||||
163 | * @var string |
||||
164 | */ |
||||
165 | protected $payerLine1 = ''; |
||||
166 | |||||
167 | /** |
||||
168 | * The second line of the payer, e.g. "Main Street 11" |
||||
169 | * |
||||
170 | * @var string |
||||
171 | */ |
||||
172 | protected $payerLine2 = ''; |
||||
173 | |||||
174 | /** |
||||
175 | * The third line of the payer, e.g. "4052 Basel" |
||||
176 | * |
||||
177 | * @var string |
||||
178 | */ |
||||
179 | protected $payerLine3 = ''; |
||||
180 | |||||
181 | /** |
||||
182 | * The fourth line of the payer, if needed |
||||
183 | * |
||||
184 | * @var string |
||||
185 | */ |
||||
186 | protected $payerLine4 = ''; |
||||
187 | |||||
188 | /** |
||||
189 | * Determines if the payment slip must not be used for payment (XXXed out) |
||||
190 | * |
||||
191 | * @var bool |
||||
192 | */ |
||||
193 | protected $notForPayment = false; |
||||
194 | |||||
195 | /** |
||||
196 | * Set if payment slip has a bank specified |
||||
197 | * |
||||
198 | * Resets the bank data when disabling. |
||||
199 | * |
||||
200 | * @param bool $withBank True for yes, false for no |
||||
201 | * @return $this The current instance for a fluent interface. |
||||
202 | */ |
||||
203 | 2 | public function setWithBank($withBank = true) |
|||
204 | { |
||||
205 | 2 | $this->isBool($withBank, 'withBank'); |
|||
206 | 1 | $this->withBank = $withBank; |
|||
207 | |||||
208 | 1 | if ($withBank === false) { |
|||
209 | 1 | $this->bankName = ''; |
|||
210 | 1 | $this->bankCity = ''; |
|||
211 | } |
||||
212 | |||||
213 | 1 | return $this; |
|||
214 | } |
||||
215 | |||||
216 | /** |
||||
217 | * Get if payment slip has recipient specified |
||||
218 | * |
||||
219 | * @return bool True if payment slip has the recipient specified, else false. |
||||
220 | */ |
||||
221 | 1 | public function getWithBank() |
|||
222 | { |
||||
223 | 1 | return $this->withBank; |
|||
224 | } |
||||
225 | |||||
226 | /** |
||||
227 | * Set if payment slip has an account number specified |
||||
228 | * |
||||
229 | * Resets the account number when disabling. |
||||
230 | * |
||||
231 | * @param bool $withAccountNumber True if yes, false if no. |
||||
232 | * @return $this The current instance for a fluent interface. |
||||
233 | */ |
||||
234 | 2 | public function setWithAccountNumber($withAccountNumber = true) |
|||
235 | { |
||||
236 | 2 | $this->isBool($withAccountNumber, 'withAccountNumber'); |
|||
237 | 1 | $this->withAccountNumber = $withAccountNumber; |
|||
238 | |||||
239 | 1 | if ($withAccountNumber === false) { |
|||
240 | 1 | $this->accountNumber = ''; |
|||
241 | } |
||||
242 | |||||
243 | 1 | return $this; |
|||
244 | } |
||||
245 | |||||
246 | /** |
||||
247 | * Get if payment slip has an account number specified |
||||
248 | * |
||||
249 | * @return bool True if payment slip has an account number specified, else false. |
||||
250 | */ |
||||
251 | 1 | public function getWithAccountNumber() |
|||
252 | { |
||||
253 | 1 | return $this->withAccountNumber; |
|||
254 | } |
||||
255 | |||||
256 | /** |
||||
257 | * Set if payment slip has a recipient specified |
||||
258 | * |
||||
259 | * Resets the recipient data when disabling. |
||||
260 | * |
||||
261 | * @param bool $withRecipient True if yes, false if no. |
||||
262 | * @return $this The current instance for a fluent interface. |
||||
263 | */ |
||||
264 | 2 | public function setWithRecipient($withRecipient = true) |
|||
265 | { |
||||
266 | 2 | $this->isBool($withRecipient, 'withRecipient'); |
|||
267 | 1 | $this->withRecipient = $withRecipient; |
|||
268 | |||||
269 | 1 | if ($withRecipient === false) { |
|||
270 | 1 | $this->recipientLine1 = ''; |
|||
271 | 1 | $this->recipientLine2 = ''; |
|||
272 | 1 | $this->recipientLine3 = ''; |
|||
273 | 1 | $this->recipientLine4 = ''; |
|||
274 | } |
||||
275 | |||||
276 | 1 | return $this; |
|||
277 | } |
||||
278 | |||||
279 | /** |
||||
280 | * Get if payment slip has a recipient specified |
||||
281 | * |
||||
282 | * @return bool True if payment slip has a recipient specified, else false. |
||||
283 | */ |
||||
284 | 1 | public function getWithRecipient() |
|||
285 | { |
||||
286 | 1 | return $this->withRecipient; |
|||
287 | } |
||||
288 | |||||
289 | /** |
||||
290 | * Set if payment slip has an amount specified |
||||
291 | * |
||||
292 | * Resets the amount when disabling. |
||||
293 | * |
||||
294 | * @param bool $withAmount True for yes, false for no. |
||||
295 | * @return $this The current instance for a fluent interface. |
||||
296 | */ |
||||
297 | 2 | public function setWithAmount($withAmount = true) |
|||
298 | { |
||||
299 | 2 | $this->isBool($withAmount, 'withAmount'); |
|||
300 | 1 | $this->withAmount = $withAmount; |
|||
301 | |||||
302 | 1 | if ($withAmount === false) { |
|||
303 | 1 | $this->amount = 0.0; |
|||
304 | } |
||||
305 | |||||
306 | 1 | return $this; |
|||
307 | } |
||||
308 | |||||
309 | /** |
||||
310 | * Get if payment slip has an amount specified |
||||
311 | * |
||||
312 | * @return bool True if payment slip has an amount specified, else false. |
||||
313 | */ |
||||
314 | 1 | public function getWithAmount() |
|||
315 | { |
||||
316 | 1 | return $this->withAmount; |
|||
317 | } |
||||
318 | |||||
319 | /** |
||||
320 | * Set if payment slip has a payer specified |
||||
321 | * |
||||
322 | * Resets the payer data when disabling. |
||||
323 | * |
||||
324 | * @param bool $withPayer True if yes, false if no. |
||||
325 | * @return $this The current instance for a fluent interface. |
||||
326 | */ |
||||
327 | 2 | public function setWithPayer($withPayer = true) |
|||
328 | { |
||||
329 | 2 | $this->isBool($withPayer, 'withPayer'); |
|||
330 | 1 | $this->withPayer = $withPayer; |
|||
331 | |||||
332 | 1 | if ($withPayer === false) { |
|||
333 | 1 | $this->payerLine1 = ''; |
|||
334 | 1 | $this->payerLine2 = ''; |
|||
335 | 1 | $this->payerLine3 = ''; |
|||
336 | 1 | $this->payerLine4 = ''; |
|||
337 | } |
||||
338 | |||||
339 | 1 | return $this; |
|||
340 | } |
||||
341 | |||||
342 | /** |
||||
343 | * Get if payment slip has a payer specified |
||||
344 | * |
||||
345 | * @return bool True if payment slip has a payer specified, else false. |
||||
346 | */ |
||||
347 | 1 | public function getWithPayer() |
|||
348 | { |
||||
349 | 1 | return $this->withPayer; |
|||
350 | } |
||||
351 | |||||
352 | /** |
||||
353 | * Sets the name, city and account number of the bank |
||||
354 | * |
||||
355 | * @param string $bankName Name of the bank. |
||||
356 | * @param string $bankCity City of the bank. |
||||
357 | * @return $this The current instance for a fluent interface. |
||||
358 | */ |
||||
359 | 1 | public function setBankData($bankName, $bankCity) |
|||
360 | { |
||||
361 | 1 | $this->setBankName($bankName); |
|||
362 | 1 | $this->setBankCity($bankCity); |
|||
363 | |||||
364 | 1 | return $this; |
|||
365 | } |
||||
366 | |||||
367 | /** |
||||
368 | * Set the name of the bank |
||||
369 | * |
||||
370 | * @param string $bankName The name of the bank. |
||||
371 | * @return $this The current instance for a fluent interface. |
||||
372 | * @throws DisabledDataException If the data is disabled. |
||||
373 | * |
||||
374 | * @todo Implement max length check |
||||
375 | */ |
||||
376 | 2 | public function setBankName($bankName) |
|||
377 | { |
||||
378 | 2 | if (!$this->getWithBank()) { |
|||
379 | 1 | throw new DisabledDataException('bank name'); |
|||
380 | } |
||||
381 | 1 | $this->bankName = $bankName; |
|||
382 | |||||
383 | 1 | return $this; |
|||
384 | } |
||||
385 | |||||
386 | /** |
||||
387 | * Get the name of the bank |
||||
388 | * |
||||
389 | * @return string The name of the bank, if withBank is set to true. |
||||
390 | * @throws DisabledDataException If the data is disabled. |
||||
391 | */ |
||||
392 | 2 | public function getBankName() |
|||
393 | { |
||||
394 | 2 | if (!$this->getWithBank()) { |
|||
395 | 1 | throw new DisabledDataException('bank name'); |
|||
396 | } |
||||
397 | 1 | return $this->bankName; |
|||
398 | } |
||||
399 | |||||
400 | /** |
||||
401 | * Set the postal code and city of the bank |
||||
402 | * |
||||
403 | * @param string $bankCity The postal code and city of the bank |
||||
404 | * @return $this The current instance for a fluent interface. |
||||
405 | * @throws DisabledDataException If the data is disabled. |
||||
406 | * |
||||
407 | * @todo Implement max length check |
||||
408 | */ |
||||
409 | 2 | public function setBankCity($bankCity) |
|||
410 | { |
||||
411 | 2 | if (!$this->getWithBank()) { |
|||
412 | 1 | throw new DisabledDataException('bank city'); |
|||
413 | } |
||||
414 | 1 | $this->bankCity = $bankCity; |
|||
415 | |||||
416 | 1 | return $this; |
|||
417 | } |
||||
418 | |||||
419 | /** |
||||
420 | * Get the postal code and city of the bank |
||||
421 | * |
||||
422 | * @return string The postal code and city, if withBank is set to true. |
||||
423 | * @throws DisabledDataException If the data is disabled. |
||||
424 | */ |
||||
425 | 2 | public function getBankCity() |
|||
426 | { |
||||
427 | 2 | if (!$this->getWithBank()) { |
|||
428 | 1 | throw new DisabledDataException('bank city'); |
|||
429 | } |
||||
430 | 1 | return $this->bankCity; |
|||
431 | } |
||||
432 | |||||
433 | /** |
||||
434 | * Set the bank or post cheque account where the money will be transferred to |
||||
435 | * |
||||
436 | * @param string $accountNumber The bank or post cheque account. |
||||
437 | * @return $this The current instance for a fluent interface. |
||||
438 | * @throws DisabledDataException If the data is disabled. |
||||
439 | * |
||||
440 | * @todo Implement parameter validation (two hyphens, min & max length) |
||||
441 | */ |
||||
442 | 2 | public function setAccountNumber($accountNumber) |
|||
443 | { |
||||
444 | 2 | if (!$this->getWithAccountNumber()) { |
|||
445 | 1 | throw new DisabledDataException('account number'); |
|||
446 | } |
||||
447 | 1 | $this->accountNumber = $accountNumber; |
|||
448 | |||||
449 | 1 | return $this; |
|||
450 | } |
||||
451 | |||||
452 | /** |
||||
453 | * Get the bank or post cheque account where the money will be transferred to |
||||
454 | * |
||||
455 | * @return string The bank or post cheque account, if withAccountNumber is set to true. |
||||
456 | * @throws DisabledDataException If the data is disabled. |
||||
457 | */ |
||||
458 | 2 | public function getAccountNumber() |
|||
459 | { |
||||
460 | 2 | if (!$this->getWithAccountNumber()) { |
|||
461 | 1 | throw new DisabledDataException('account number'); |
|||
462 | } |
||||
463 | 1 | return $this->accountNumber; |
|||
464 | } |
||||
465 | |||||
466 | /** |
||||
467 | * Sets the four lines of the recipient |
||||
468 | * |
||||
469 | * @param string $recipientLine1 The first line of the recipient, e.g. "My Company Ltd.". |
||||
470 | * @param string $recipientLine2 The second line of the recipient, e.g. "Examplestreet 61". |
||||
471 | * @param string $recipientLine3 The third line of the recipient, e.g. "8000 Zürich". |
||||
472 | * @param string $recipientLine4 The fourth line of the recipient, if needed. |
||||
473 | * @return $this The current instance for a fluent interface. |
||||
474 | */ |
||||
475 | 1 | public function setRecipientData($recipientLine1, $recipientLine2, $recipientLine3 = '', $recipientLine4 = '') |
|||
476 | { |
||||
477 | 1 | $this->setRecipientLine1($recipientLine1); |
|||
478 | 1 | $this->setRecipientLine2($recipientLine2); |
|||
479 | 1 | $this->setRecipientLine3($recipientLine3); |
|||
480 | 1 | $this->setRecipientLine4($recipientLine4); |
|||
481 | |||||
482 | 1 | return $this; |
|||
483 | } |
||||
484 | |||||
485 | /** |
||||
486 | * Set the first line of the recipient |
||||
487 | * |
||||
488 | * @param string $recipientLine1 The first line of the recipient, e.g. "My Company Ltd.". |
||||
489 | * @return $this The current instance for a fluent interface. |
||||
490 | * @throws DisabledDataException If the data is disabled. |
||||
491 | */ |
||||
492 | 2 | public function setRecipientLine1($recipientLine1) |
|||
493 | { |
||||
494 | 2 | if (!$this->getWithRecipient()) { |
|||
495 | 1 | throw new DisabledDataException('recipient line 1'); |
|||
496 | } |
||||
497 | 1 | $this->recipientLine1 = $recipientLine1; |
|||
498 | |||||
499 | 1 | return $this; |
|||
500 | } |
||||
501 | |||||
502 | /** |
||||
503 | * Get the first line of the recipient |
||||
504 | * |
||||
505 | * @return string The first line of the recipient, if withRecipient is set to true. |
||||
506 | * @throws DisabledDataException If the data is disabled. |
||||
507 | */ |
||||
508 | 2 | public function getRecipientLine1() |
|||
509 | { |
||||
510 | 2 | if (!$this->getWithRecipient()) { |
|||
511 | 1 | throw new DisabledDataException('recipient line 1'); |
|||
512 | } |
||||
513 | 1 | return $this->recipientLine1; |
|||
514 | } |
||||
515 | |||||
516 | /** |
||||
517 | * Set the second line of the recipient |
||||
518 | * |
||||
519 | * @param string $recipientLine2 The second line of the recipient, e.g. "Examplestreet 61". |
||||
520 | * @return $this The current instance for a fluent interface. |
||||
521 | * @throws DisabledDataException If the data is disabled. |
||||
522 | */ |
||||
523 | 2 | public function setRecipientLine2($recipientLine2) |
|||
524 | { |
||||
525 | 2 | if (!$this->getWithRecipient()) { |
|||
526 | 1 | throw new DisabledDataException('recipient line 2'); |
|||
527 | } |
||||
528 | 1 | $this->recipientLine2 = $recipientLine2; |
|||
529 | |||||
530 | 1 | return $this; |
|||
531 | } |
||||
532 | |||||
533 | /** |
||||
534 | * Get the second line of the recipient |
||||
535 | * |
||||
536 | * @return string The second line of the recipient, if withRecipient is set to true. |
||||
537 | * @throws DisabledDataException If the data is disabled. |
||||
538 | */ |
||||
539 | 2 | public function getRecipientLine2() |
|||
540 | { |
||||
541 | 2 | if (!$this->getWithRecipient()) { |
|||
542 | 1 | throw new DisabledDataException('recipient line 2'); |
|||
543 | } |
||||
544 | 1 | return $this->recipientLine2; |
|||
545 | } |
||||
546 | |||||
547 | /** |
||||
548 | * Set the third line of the recipient |
||||
549 | * |
||||
550 | * @param string $recipientLine3 The third line of the recipient, e.g. "8000 Zürich". |
||||
551 | * @return $this The current instance for a fluent interface. |
||||
552 | * @throws DisabledDataException If the data is disabled. |
||||
553 | */ |
||||
554 | 2 | public function setRecipientLine3($recipientLine3) |
|||
555 | { |
||||
556 | 2 | if (!$this->getWithRecipient()) { |
|||
557 | 1 | throw new DisabledDataException('recipient line 3'); |
|||
558 | } |
||||
559 | 1 | $this->recipientLine3 = $recipientLine3; |
|||
560 | |||||
561 | 1 | return $this; |
|||
562 | } |
||||
563 | |||||
564 | /** |
||||
565 | * Get the third line of the recipient |
||||
566 | * |
||||
567 | * @return string The third line of the recipient, if withRecipient is set to true. |
||||
568 | * @throws DisabledDataException If the data is disabled. |
||||
569 | */ |
||||
570 | 2 | public function getRecipientLine3() |
|||
571 | { |
||||
572 | 2 | if (!$this->getWithRecipient()) { |
|||
573 | 1 | throw new DisabledDataException('recipient line 3'); |
|||
574 | } |
||||
575 | 1 | return $this->recipientLine3; |
|||
576 | } |
||||
577 | |||||
578 | /** |
||||
579 | * Set the fourth line of the recipient |
||||
580 | * |
||||
581 | * @param string $recipientLine4 The fourth line of the recipient, if needed. |
||||
582 | * @return $this The current instance for a fluent interface. |
||||
583 | * @throws DisabledDataException If the data is disabled. |
||||
584 | */ |
||||
585 | 2 | public function setRecipientLine4($recipientLine4) |
|||
586 | { |
||||
587 | 2 | if (!$this->getWithRecipient()) { |
|||
588 | 1 | throw new DisabledDataException('recipient line 4'); |
|||
589 | } |
||||
590 | 1 | $this->recipientLine4 = $recipientLine4; |
|||
591 | |||||
592 | 1 | return $this; |
|||
593 | } |
||||
594 | |||||
595 | /** |
||||
596 | * Get the fourth line of the recipient |
||||
597 | * |
||||
598 | * @return string The fourth line of the recipient, if withRecipient is set to true. |
||||
599 | * @throws DisabledDataException If the data is disabled. |
||||
600 | */ |
||||
601 | 2 | public function getRecipientLine4() |
|||
602 | { |
||||
603 | 2 | if (!$this->getWithRecipient()) { |
|||
604 | 1 | throw new DisabledDataException('recipient line 4'); |
|||
605 | } |
||||
606 | 1 | return $this->recipientLine4; |
|||
607 | } |
||||
608 | |||||
609 | /** |
||||
610 | * Set the amount of the payment slip. Only possible if it's not a ESR+. |
||||
611 | * |
||||
612 | * @param float $amount The amount to be payed into |
||||
613 | * @return $this The current instance for a fluent interface. |
||||
614 | * @throws DisabledDataException If the data is disabled. |
||||
615 | */ |
||||
616 | 4 | public function setAmount($amount = 0.0) |
|||
617 | { |
||||
618 | 4 | if (!$this->getWithAmount()) { |
|||
619 | 1 | throw new DisabledDataException('amount'); |
|||
620 | } |
||||
621 | 3 | if (is_numeric($amount)) { |
|||
0 ignored issues
–
show
introduced
by
![]() |
|||||
622 | 3 | $this->amount = round($amount, 2); |
|||
623 | } else { |
||||
624 | $this->amount = $amount; |
||||
625 | } |
||||
626 | |||||
627 | 3 | return $this; |
|||
628 | } |
||||
629 | |||||
630 | /** |
||||
631 | * Get the amount to be payed into |
||||
632 | * |
||||
633 | * @return float The amount to be payed into. |
||||
634 | * @throws DisabledDataException If the data is disabled. |
||||
635 | */ |
||||
636 | 2 | public function getAmount() |
|||
637 | { |
||||
638 | 2 | if (!$this->getWithAmount()) { |
|||
639 | 1 | throw new DisabledDataException('amount'); |
|||
640 | } |
||||
641 | 1 | return $this->amount; |
|||
642 | } |
||||
643 | |||||
644 | /** |
||||
645 | * Sets the four lines of the payer |
||||
646 | * |
||||
647 | * At least two lines are necessary. |
||||
648 | * |
||||
649 | * @param string $payerLine1 The first line of the payer, e.g. "Hans Mustermann". |
||||
650 | * @param string $payerLine2 The second line of the payer, e.g. "Main Street 11". |
||||
651 | * @param string $payerLine3 The third line of the payer, e.g. "4052 Basel". |
||||
652 | * @param string $payerLine4 The fourth line of the payer, if needed. |
||||
653 | * @return $this The current instance for a fluent interface. |
||||
654 | */ |
||||
655 | 1 | public function setPayerData($payerLine1, $payerLine2, $payerLine3 = '', $payerLine4 = '') |
|||
656 | { |
||||
657 | 1 | $this->setPayerLine1($payerLine1); |
|||
658 | 1 | $this->setPayerLine2($payerLine2); |
|||
659 | 1 | $this->setPayerLine3($payerLine3); |
|||
660 | 1 | $this->setPayerLine4($payerLine4); |
|||
661 | |||||
662 | 1 | return $this; |
|||
663 | } |
||||
664 | |||||
665 | /** |
||||
666 | * Set the first line of the payer |
||||
667 | * |
||||
668 | * @param string $payerLine1 The first line of the payer, e.g. "Hans Mustermann". |
||||
669 | * @return $this The current instance for a fluent interface. |
||||
670 | * @throws DisabledDataException If the data is disabled. |
||||
671 | */ |
||||
672 | 2 | public function setPayerLine1($payerLine1) |
|||
673 | { |
||||
674 | 2 | if (!$this->getWithPayer()) { |
|||
675 | 1 | throw new DisabledDataException('payer line 1'); |
|||
676 | } |
||||
677 | 1 | $this->payerLine1 = $payerLine1; |
|||
678 | |||||
679 | 1 | return $this; |
|||
680 | } |
||||
681 | |||||
682 | /** |
||||
683 | * Get the first line of the payer |
||||
684 | * |
||||
685 | * @return string The first line of the payer, if withPayer is set to true. |
||||
686 | * @throws DisabledDataException If the data is disabled. |
||||
687 | */ |
||||
688 | 2 | public function getPayerLine1() |
|||
689 | { |
||||
690 | 2 | if (!$this->getWithPayer()) { |
|||
691 | 1 | throw new DisabledDataException('payer line 1'); |
|||
692 | } |
||||
693 | 1 | return $this->payerLine1; |
|||
694 | } |
||||
695 | |||||
696 | /** |
||||
697 | * Set the second line of the payer |
||||
698 | * |
||||
699 | * @param string $payerLine2 The second line of the payer, e.g. "Main Street 11". |
||||
700 | * @return $this The current instance for a fluent interface. |
||||
701 | * @throws DisabledDataException If the data is disabled. |
||||
702 | */ |
||||
703 | 2 | public function setPayerLine2($payerLine2) |
|||
704 | { |
||||
705 | 2 | if (!$this->getWithPayer()) { |
|||
706 | 1 | throw new DisabledDataException('payer line 2'); |
|||
707 | } |
||||
708 | 1 | $this->payerLine2 = $payerLine2; |
|||
709 | |||||
710 | 1 | return $this; |
|||
711 | } |
||||
712 | |||||
713 | /** |
||||
714 | * Get the second line of the payer |
||||
715 | * |
||||
716 | * @return string The second line of the payer, if withPayer is set to true. |
||||
717 | * @throws DisabledDataException If the data is disabled. |
||||
718 | */ |
||||
719 | 2 | public function getPayerLine2() |
|||
720 | { |
||||
721 | 2 | if (!$this->getWithPayer()) { |
|||
722 | 1 | throw new DisabledDataException('payer line 2'); |
|||
723 | } |
||||
724 | 1 | return $this->payerLine2; |
|||
725 | } |
||||
726 | |||||
727 | /** |
||||
728 | * Set the third line of the payer |
||||
729 | * |
||||
730 | * @param string $payerLine3 The third line of the payer, e.g. "4052 Basel". |
||||
731 | * @return $this The current instance for a fluent interface. |
||||
732 | * @throws DisabledDataException If the data is disabled. |
||||
733 | */ |
||||
734 | 2 | public function setPayerLine3($payerLine3) |
|||
735 | { |
||||
736 | 2 | if (!$this->getWithPayer()) { |
|||
737 | 1 | throw new DisabledDataException('payer line 3'); |
|||
738 | } |
||||
739 | 1 | $this->payerLine3 = $payerLine3; |
|||
740 | |||||
741 | 1 | return $this; |
|||
742 | } |
||||
743 | |||||
744 | /** |
||||
745 | * Get the third line of the payer |
||||
746 | * |
||||
747 | * @return string The third line of the payer, if withPayer is set to true. |
||||
748 | * @throws DisabledDataException If the data is disabled. |
||||
749 | */ |
||||
750 | 2 | public function getPayerLine3() |
|||
751 | { |
||||
752 | 2 | if (!$this->getWithPayer()) { |
|||
753 | 1 | throw new DisabledDataException('payer line 3'); |
|||
754 | } |
||||
755 | 1 | return $this->payerLine3; |
|||
756 | } |
||||
757 | |||||
758 | /** |
||||
759 | * Set the fourth line of the payer |
||||
760 | * |
||||
761 | * @param string $payerLine4 The fourth line of the payer, if needed. |
||||
762 | * @return $this The current instance for a fluent interface. |
||||
763 | * @throws DisabledDataException If the data is disabled. |
||||
764 | */ |
||||
765 | 2 | public function setPayerLine4($payerLine4) |
|||
766 | { |
||||
767 | 2 | if (!$this->getWithPayer()) { |
|||
768 | 1 | throw new DisabledDataException('payer line 4'); |
|||
769 | } |
||||
770 | 1 | $this->payerLine4 = $payerLine4; |
|||
771 | |||||
772 | 1 | return $this; |
|||
773 | } |
||||
774 | |||||
775 | /** |
||||
776 | * Get the fourth line of the payer |
||||
777 | * |
||||
778 | * @return string The fourth line of the payer, if withPayer is set to true. |
||||
779 | * @throws DisabledDataException If the data is disabled. |
||||
780 | */ |
||||
781 | 2 | public function getPayerLine4() |
|||
782 | { |
||||
783 | 2 | if (!$this->getWithPayer()) { |
|||
784 | 1 | throw new DisabledDataException('payer line 4'); |
|||
785 | } |
||||
786 | 1 | return $this->payerLine4; |
|||
787 | } |
||||
788 | |||||
789 | /** |
||||
790 | * Clear the account of the two hyphens |
||||
791 | * |
||||
792 | * @return string The account of the two hyphens, 'XXXXXXXXX' if not for payment or else false. |
||||
793 | * @throws DisabledDataException If the data is disabled. |
||||
794 | * @throws PaymentSlipException If account number does not contain two hyphens. |
||||
795 | * @todo Cover the edge cases with tests |
||||
796 | */ |
||||
797 | 2 | protected function getAccountDigits() |
|||
798 | { |
||||
799 | 2 | if (!$this->getWithAccountNumber()) { |
|||
800 | throw new DisabledDataException('account number'); |
||||
801 | } |
||||
802 | 2 | if ($this->getNotForPayment()) { |
|||
803 | 1 | return 'XXXXXXXXX'; |
|||
804 | } |
||||
805 | 1 | $accountNumber = $this->getAccountNumber(); |
|||
806 | 1 | if ($accountNumber === '') { |
|||
807 | return $accountNumber; |
||||
808 | } |
||||
809 | 1 | $accountDigits = str_replace('-', '', $accountNumber, $replacedHyphens); |
|||
810 | 1 | if ($replacedHyphens != 2) { |
|||
811 | throw new PaymentSlipException('Invalid Account number. Does not contain two hyphens.'); |
||||
812 | } |
||||
813 | 1 | return $accountDigits; |
|||
814 | } |
||||
815 | |||||
816 | /** |
||||
817 | * Get the francs amount without cents |
||||
818 | * |
||||
819 | * @return bool|int Francs amount without cents. |
||||
820 | */ |
||||
821 | 2 | public function getAmountFrancs() |
|||
822 | { |
||||
823 | 2 | if ($this->getNotForPayment()) { |
|||
824 | 1 | return 'XXXXXXXX'; |
|||
0 ignored issues
–
show
|
|||||
825 | } |
||||
826 | 2 | $amount = $this->getAmount(); |
|||
827 | 2 | $francs = intval($amount); |
|||
828 | 2 | return $francs; |
|||
829 | } |
||||
830 | |||||
831 | /** |
||||
832 | * Get the zero filled, right padded, two digits long cents amount |
||||
833 | * |
||||
834 | * @return bool|string Amount of Cents, zero filled, right padded, two digits long. |
||||
835 | */ |
||||
836 | 2 | public function getAmountCents() |
|||
837 | { |
||||
838 | 2 | if ($this->getNotForPayment()) { |
|||
839 | 1 | return 'XX'; |
|||
840 | } |
||||
841 | 2 | $amount = $this->getAmount(); |
|||
842 | 2 | $francs = intval($amount); |
|||
843 | 2 | $cents = round(($amount - $francs) * 100); |
|||
844 | 2 | return str_pad($cents, 2, '0', STR_PAD_LEFT); |
|||
845 | } |
||||
846 | |||||
847 | /** |
||||
848 | * Set payment slip for not to be used for payment |
||||
849 | * |
||||
850 | * XXXes out all fields to prevent people using the payment slip. |
||||
851 | * |
||||
852 | * @param boolean $notForPayment True if not for payment, else false. |
||||
853 | * @return $this The current instance for a fluent interface. |
||||
854 | */ |
||||
855 | 2 | public function setNotForPayment($notForPayment = true) |
|||
856 | { |
||||
857 | 2 | $this->notForPayment = $notForPayment; |
|||
858 | |||||
859 | 2 | if ($notForPayment === true) { |
|||
860 | 2 | if ($this->getWithBank() === true) { |
|||
861 | 1 | $this->setBankData('XXXXXX', 'XXXXXX'); |
|||
862 | } |
||||
863 | 2 | if ($this->getWithAccountNumber() === true) { |
|||
864 | 1 | $this->setAccountNumber('XXXXXX'); |
|||
865 | } |
||||
866 | 2 | if ($this->getWithRecipient() === true) { |
|||
867 | 1 | $this->setRecipientData('XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX'); |
|||
868 | } |
||||
869 | 2 | if ($this->getWithPayer() === true) { |
|||
870 | 1 | $this->setPayerData('XXXXXX', 'XXXXXX', 'XXXXXX', 'XXXXXX'); |
|||
871 | } |
||||
872 | 2 | if ($this->getWithAmount() === true) { |
|||
873 | 1 | $this->setAmount('XXXXXXXX.XX'); |
|||
0 ignored issues
–
show
'XXXXXXXX.XX' of type string is incompatible with the type double expected by parameter $amount of SwissPaymentSlip\SwissPa...ntSlipData::setAmount() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
874 | } |
||||
875 | } |
||||
876 | |||||
877 | 2 | return $this; |
|||
878 | } |
||||
879 | |||||
880 | /** |
||||
881 | * Get whether this payment slip must not be used for payment |
||||
882 | * |
||||
883 | * @return bool True if yes, else false. |
||||
884 | */ |
||||
885 | 1 | public function getNotForPayment() |
|||
886 | { |
||||
887 | 1 | return $this->notForPayment; |
|||
888 | } |
||||
889 | |||||
890 | /** |
||||
891 | * Creates Modulo10 recursive check digit |
||||
892 | * |
||||
893 | * @copyright As found on http://www.developers-guide.net/forums/5431,modulo10-rekursiv (thanks, dude!) |
||||
894 | * @param string $number Number to create recursive check digit off. |
||||
895 | * @return int Recursive check digit. |
||||
896 | */ |
||||
897 | 2 | protected function modulo10($number) |
|||
898 | { |
||||
899 | 2 | $next = 0; |
|||
900 | 2 | for ($i=0; $i < strlen($number); $i++) { |
|||
901 | 2 | $next = $this->moduloTable[($next + intval(substr($number, $i, 1))) % 10]; |
|||
902 | } |
||||
903 | |||||
904 | 2 | return (10 - $next) % 10; |
|||
905 | } |
||||
906 | |||||
907 | /** |
||||
908 | * Get a given string broken down in blocks of a certain size |
||||
909 | * |
||||
910 | * Example: 000000000000000 becomes more readable 00000 00000 00000 |
||||
911 | * |
||||
912 | * @param string $string The to be formatted string. |
||||
913 | * @param int $blockSize The Block size of choice. |
||||
914 | * @param bool $alignFromRight Right aligned, blocks are build from right. |
||||
915 | * @return string Given string divided in blocks of given block size separated by one space. |
||||
916 | */ |
||||
917 | 1 | protected function breakStringIntoBlocks($string, $blockSize = 5, $alignFromRight = true) |
|||
918 | { |
||||
919 | // Lets reverse the string (because we want the block to be aligned from the right) |
||||
920 | 1 | if ($alignFromRight === true) { |
|||
921 | 1 | $string = strrev($string); |
|||
922 | } |
||||
923 | |||||
924 | // Chop it into blocks |
||||
925 | 1 | $string = trim(chunk_split($string, $blockSize, ' ')); |
|||
926 | |||||
927 | // Re-reverse |
||||
928 | 1 | if ($alignFromRight === true) { |
|||
929 | 1 | $string = strrev($string); |
|||
930 | } |
||||
931 | |||||
932 | 1 | return $string; |
|||
933 | } |
||||
934 | |||||
935 | /** |
||||
936 | * Verify that a given parameter is boolean |
||||
937 | * |
||||
938 | * @param mixed $parameter The given parameter to validate. |
||||
939 | * @param string $varName The name of the variable. |
||||
940 | * @return true If the parameter is a boolean. |
||||
941 | * @throws InvalidArgumentException If the parameter is not a boolean. |
||||
942 | */ |
||||
943 | 14 | protected function isBool($parameter, $varName) |
|||
944 | { |
||||
945 | 14 | if (!is_bool($parameter)) { |
|||
946 | 7 | throw new InvalidArgumentException( |
|||
947 | 7 | sprintf( |
|||
948 | 7 | '$%s is not a boolean.', |
|||
949 | 7 | $varName |
|||
950 | ) |
||||
951 | ); |
||||
952 | } |
||||
953 | 7 | return true; |
|||
954 | } |
||||
955 | } |
||||
956 |