-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSepaUtilities.php
1453 lines (1278 loc) · 269 KB
/
SepaUtilities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/**
* SepaUtilities
*
* @license GNU LGPL v3.0 - For details have a look at the LICENSE file
* @copyright ©2020 Alexander Schickedanz
* @link https://github.com/AbcAeffchen/SepaUtilities
*
* @author Alexander Schickedanz <abcaeffchen@gmail.com>
*/
namespace AbcAeffchen\SepaUtilities;
use DateTime;
use Exception;
/**
* Returns a DateTime object of Easter Sunday in the given year.
* This is calculated with the Gaussian Algorithm.
*
* @param int $year The year written with four digits.
* @return DateTime DateTime object pointing to Easter Sunday of the $year.
*/
function easterDate(int $year): DateTime
{
$G = $year % 19;
$C = (int) ($year / 100);
$H = (int) ($C - (int) ($C / 4) - (int) ((8 * $C + 13) / 25) + 19 * $G + 15) % 30;
$I = $H - (int) ($H / 28) * (1 - (int) ($H / 28) * (int) (29 / ($H + 1)) * (int) ((21 - $G) / 11));
$J = ($year + (int) ($year / 4) + $I + 2 - $C + (int) ($C / 4)) % 7;
$L = $I - $J;
$m = 3 + (int) (($L + 40) / 44);
$d = $L + 28 - 31 * ((int) ($m / 4));
return DateTime::createFromFormat('Y-n-j', $year . '-' . $m . '-' . $d);
}
/**
* Useful methods to validate and sanitize input used in SEPA files
*/
class SepaUtilities
{
// credit transfers version
const SEPA_PAIN_001_002_03 = 100203;
const SEPA_PAIN_001_003_03 = 100303;
const SEPA_PAIN_001_001_03 = 100103;
const SEPA_PAIN_001_001_03_GBIC = 1001031;
const SEPA_PAIN_001_001_03_CH_02 = 1001032;
const SEPA_PAIN_001_001_09 = 100109;
// direct debit versions
const SEPA_PAIN_008_002_02 = 800202;
const SEPA_PAIN_008_003_02 = 800302;
const SEPA_PAIN_008_001_02 = 800102;
const SEPA_PAIN_008_001_02_GBIC = 8001021;
const SEPA_PAIN_008_001_02_AUSTRIAN_003 = 8001022;
const SEPA_PAIN_008_001_02_CH_03 = 8001023;
const SEPA_PAIN_008_001_08 = 800108;
const SEPA_TRANSACTION_TYPE_CT = 1;
const SEPA_TRANSACTION_TYPE_DD = 8;
const HTML_PATTERN_IBAN = '([a-zA-Z]\s*){2}([0-9]\s?){2}\s*([a-zA-Z0-9]\s*){1,30}';
const HTML_PATTERN_BIC = '([a-zA-Z]\s*){6}[a-zA-Z2-9]\s*[a-nA-Np-zP-Z0-9]\s*(([A-Z0-9]\s*){3}){0,1}';
const PATTERN_IBAN = '[A-Z]{2}[0-9]{2}[A-Z0-9]{1,30}';
const PATTERN_BIC = '[A-Z]{6}[A-Z2-9][A-NP-Z0-9]([A-Z0-9]{3})?';
/**
* equates to RestrictedPersonIdentifierSEPA
*/
const PATTERN_CREDITOR_IDENTIFIER = '[a-zA-Z]{2}[0-9]{2}([A-Za-z0-9]|[+?/\-:().,\']){3}([A-Za-z0-9]|[+?/\-:().,\']){1,28}';
/**
* used for Names, etc.
*/
const PATTERN_SHORT_TEXT = '[a-zA-Z0-9/\-?:().,\'+\s]{0,70}';
/**
* used for remittance information
*/
const PATTERN_LONG_TEXT = '[a-zA-Z0-9/\-?:().,\'+\s]{0,140}';
/**
* Used for Message-, Payment- and Transfer-IDs (since 2016 also for Mandate-ID)
*/
const PATTERN_RESTRICTED_IDENTIFICATION_SEPA1 = '([A-Za-z0-9]|[+?/\-:().,\'|\s]){1,35}';
/**
* Used for Mandate-ID
*/
const PATTERN_RESTRICTED_IDENTIFICATION_SEPA2 = '([A-Za-z0-9]|[+?/\-:().,\']){1,35}';
/**
* This is just for compatibility to v1.1.*
*/
const PATTERN_MANDATE_ID = self::PATTERN_RESTRICTED_IDENTIFICATION_SEPA2;
const FLAG_ALT_REPLACEMENT_GERMAN = 1; // 1 << 0
const FLAG_NO_REPLACEMENT_GERMAN = 32768; // 1 << 15
/**
* first direct debit
*/
const SEQUENCE_TYPE_FIRST = 'FRST';
/**
* recurring direct debit
*/
const SEQUENCE_TYPE_RECURRING = 'RCUR';
/**
* one time direct debit
*/
const SEQUENCE_TYPE_ONCE = 'OOFF';
/**
* final direct debit
*/
const SEQUENCE_TYPE_FINAL = 'FNAL';
/**
* normal direct debit
*/
const LOCAL_INSTRUMENT_CORE_DIRECT_DEBIT = 'CORE';
/**
* normal direct debit
*/
const LOCAL_INSTRUMENT_LSV_DIRECT_DEBIT = 'LSV+';
/**
* urgent direct debit
*/
const LOCAL_INSTRUMENT_CORE_DIRECT_DEBIT_D_1 = 'COR1';
/**
* business direct debit
*/
const LOCAL_INSTRUMENT_BUSINESS_2_BUSINESS = 'B2B';
/**
* @type int BIC_REQUIRED_THRESHOLD Until 2016-01-31 (incl.) the BIC is required for international
* payment transactions
*/
const BIC_REQUIRED_THRESHOLD = 20160131;
/**
* Valid maximal text length
*/
const TEXT_LENGTH_VERY_SHORT = 35;
const TEXT_LENGTH_SHORT = 70;
const TEXT_LENGTH_LONG = 140;
private const IBAN_PATTERNS = ['EG' => 'EG[0-9]{2}[0-9A-Z]{23}',
'AL' => 'AL[0-9]{10}[0-9A-Z]{16}',
'DZ' => 'DZ[0-9]{2}[0-9A-Z]{20}',
'AD' => 'AD[0-9]{10}[0-9A-Z]{12}',
'AO' => 'AL[0-9]{2}[0-9A-Z]{21}',
'AZ' => 'AZ[0-9]{2}[0-9A-Z]{24}',
'BH' => 'AL[0-9]{2}[0-9A-Z]{18}',
'BE' => 'BE[0-9]{14}',
'BJ' => 'BJ[0-9]{2}[0-9A-Z]{24}',
'BA' => 'BA[0-9]{18}',
'BR' => 'BR[0-9]{2}[0-9A-Z]{25}',
'VG' => 'VG[0-9]{2}[0-9A-Z]{20}',
'BG' => 'BG[0-9]{2}[A-Z]{4}[0-9]{6}[0-9A-Z]{8}',
'BF' => 'BF[0-9]{2}[0-9A-Z]{23}',
'BI' => 'BI[0-9]{2}[0-9A-Z]{12}',
'CR' => 'CR[0-9]{2}[0-9A-Z]{17}',
'CI' => 'CI[0-9]{2}[0-9A-Z]{24}',
'DK' => 'DK[0-9]{16}',
'DE' => 'DE[0-9]{20}',
'DO' => 'DO[0-9]{2}[0-9A-Z]{24}',
'EE' => 'EE[0-9]{18}',
'FO' => 'FO[0-9]{16}',
'FI' => 'FI[0-9]{16}',
'FR' => 'FR[0-9]{2}[0-9A-Z]{23}',
'GA' => 'GA[0-9]{2}[0-9A-Z]{23}',
'GE' => 'GE[0-9]{2}[A-Z]{2}[0-9A-Z]{16}',
'GI' => 'GI[0-9]{2}[A-Z]{4}[0-9]{15}',
'GR' => 'GR[0-9]{9}[0-9A-Z]{16}',
'GL' => 'GL[0-9]{16}',
'GT' => 'GT[0-9]{2}[0-9A-Z]{24}',
'IR' => 'IR[0-9]{2}[0-9A-Z]{22}',
'IE' => 'IE[0-9]{2}[A-Z]{4}[0-9]{14}',
'IS' => 'IS[0-9]{24}',
'IL' => 'IL[0-9]{21}',
'IT' => 'IT[0-9]{2}[A-Z]{1}[0-9]{10}[0-9A-Z]{12}',
'JO' => 'JO[0-9]{2}[0-9A-Z]{26}',
'CM' => 'CM[0-9]{2}[0-9A-Z]{23}',
'CV' => 'CV[0-9]{2}[0-9A-Z]{21}',
'KZ' => 'KZ[0-9]{5}[0-9A-Z]{13}',
'QA' => 'QA[0-9]{2}[0-9A-Z]{25}',
'CG' => 'CG[0-9]{2}[0-9A-Z]{23}',
'KS' => 'KS[0-9]{2}[0-9A-Z]{16}', // todo: This should be the IBAN format for Kosovo. Is this correct?
'HR' => 'HR[0-9]{19}',
'KW' => 'KW[0-9]{2}[A-Z]{4}[0-9A-Z]{22}',
'LV' => 'LV[0-9]{2}[A-Z]{4}[0-9A-Z]{13}',
'LB' => 'LB[0-9]{6}[0-9A-Z]{20}',
'LI' => 'LI[0-9]{7}[0-9A-Z]{12}',
'LT' => 'LT[0-9]{18}',
'LU' => 'LU[0-9]{5}[0-9A-Z]{13}',
'MG' => 'MG[0-9]{2}[0-9A-Z]{23}',
'ML' => 'ML[0-9]{2}[0-9A-Z]{24}',
'MT' => 'MT[0-9]{2}[A-Z]{4}[0-9]{5}[0-9A-Z]{18}',
'MR' => 'MR[0-9]{25}',
'MU' => 'MU[0-9]{2}[0-9A-Z]{23}[A-Z]{3}',
'MK' => 'MK[0-9]{5}[0-9A-Z]{10}[0-9]{2}',
'MD' => 'MD[0-9]{2}[0-9A-Z]{20}',
'MC' => 'MC[0-9]{12}[0-9A-Z]{11}[0-9]{2}',
'ME' => 'ME[0-9]{20}',
'MZ' => 'MZ[0-9]{2}[0-9A-Z]{21}',
'NL' => 'NL[0-9]{2}[A-Z]{4}[0-9]{10}',
'NO' => 'NO[0-9]{13}',
'AT' => 'AT[0-9]{18}',
'TL' => 'TL[0-9]{2}[0-9A-Z]{16}',
'PK' => 'PK[0-9]{2}[0-9A-Z]{20}',
'PS' => 'PS[0-9]{2}[0-9A-Z]{25}',
'PL' => 'PL[0-9]{26}',
'PT' => 'PT[0-9]{23}',
'RO' => 'RO[0-9]{2}[A-Z]{4}[0-9A-Z]{16}',
'SM' => 'SM[0-9]{2}[A-Z]{1}[0-9]{10}[0-9A-Z]{12}',
'ST' => 'ST[0-9]{2}[0-9A-Z]{21}',
'SA' => 'SA[0-9]{4}[0-9A-Z]{18}',
'SE' => 'SE[0-9]{22}',
'CH' => 'CH[0-9]{2}[0-9]{5}[0-9A-Z]{12}',
'SN' => 'SN[0-9]{2}[0-9A-Z]{24}',
'RS' => 'RS[0-9]{20}',
'SK' => 'SK[0-9]{22}',
'SI' => 'SI[0-9]{17}',
'ES' => 'ES[0-9]{22}',
'CZ' => 'CZ[0-9]{22}',
'TN' => 'TN[0-9]{22}',
'TR' => 'TR[0-9]{7}[0-9A-Z]{17}',
'HU' => 'HU[0-9]{26}',
'AE' => 'AE[0-9]{2}[0-9A-Z]{19}',
'GB' => 'GB[0-9]{2}[A-Z]{4}[0-9]{14}',
'CY' => 'CY[0-9]{10}[0-9A-Z]{16}',
'CF' => 'CF[0-9]{2}[0-9A-Z]{23}'];
private const ALPHABET = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'];
private const ALPHABET_VALUES = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
30, 31, 32, 33, 34, 35];
private const MOD_97_VALUES = [1, 10, 3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38,
89, 17, 73, 51, 25, 56, 75, 71, 31, 19, 93, 57, 85, 74, 61, 28, 86,
84, 64, 58, 95, 77, 91, 37, 79, 14, 43, 42, 32, 29, 96, 87, 94, 67,
88, 7, 70, 21, 16, 63, 48, 92, 47, 82, 44, 52, 35, 59, 8, 80, 24];
private const SPECIAL_CHARS_REPLACEMENT = [';' => ',', '[' => '(', '\\' => '/', ']' => ')', '^' => '.', '_' => '-', '`' => '\'', '{' => '(', '|' => '/', '}' => ')', '~' => '-', '¿' => '?', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'Þ' => 'T', 'ß' => 's', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'þ' => 't', 'ÿ' => 'y', 'Ā' => 'A', 'ā' => 'a', 'Ă' => 'A', 'ă' => 'a', 'Ą' => 'A', 'ą' => 'a', 'Ć' => 'C', 'ć' => 'c', 'Ĉ' => 'C', 'ĉ' => 'c', 'Ċ' => 'C', 'ċ' => 'c', 'Č' => 'C', 'č' => 'c', 'Ď' => 'D', 'ď' => 'd', 'Đ' => 'D', 'đ' => 'd', 'Ē' => 'E', 'ē' => 'e', 'Ĕ' => 'E', 'ĕ' => 'e', 'Ė' => 'E', 'ė' => 'e', 'Ę' => 'E', 'ę' => 'e', 'Ě' => 'E', 'ě' => 'e', 'Ĝ' => 'G', 'ĝ' => 'g', 'Ğ' => 'G', 'ğ' => 'g', 'Ġ' => 'G', 'ġ' => 'g', 'Ģ' => 'G', 'ģ' => 'g', 'Ĥ' => 'H', 'ĥ' => 'h', 'Ħ' => 'H', 'ħ' => 'h', 'Ĩ' => 'I', 'ĩ' => 'i', 'Ī' => 'I', 'ī' => 'i', 'Ĭ' => 'I', 'ĭ' => 'i', 'Į' => 'I', 'į' => 'i', 'İ' => 'I', 'ı' => 'i', 'IJ' => 'I', 'ij' => 'i', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ķ' => 'K', 'ķ' => 'k', 'ĸ' => '.', 'Ĺ' => 'L', 'ĺ' => 'l', 'Ļ' => 'L', 'ļ' => 'l', 'Ľ' => 'L', 'ľ' => 'l', 'Ŀ' => 'L', 'ŀ' => 'l', 'Ł' => 'L', 'ł' => 'l', 'Ń' => 'N', 'ń' => 'n', 'Ņ' => 'N', 'ņ' => 'n', 'Ň' => 'N', 'ň' => 'n', 'Ő' => 'O', 'ő' => 'o', 'Œ' => 'O', 'œ' => 'o', 'Ŕ' => 'R', 'ŕ' => 'r', 'Ŗ' => 'R', 'ŗ' => 'r', 'Ř' => 'R', 'ř' => 'r', 'Ś' => 'S', 'ś' => 's', 'Ŝ' => 'S', 'ŝ' => 's', 'Ş' => 'S', 'ş' => 's', 'Š' => 'S', 'š' => 's', 'Ţ' => 'T', 'ţ' => 't', 'Ť' => 'T', 'ť' => 't', 'Ŧ' => 'T', 'ŧ' => 't', 'Ũ' => 'U', 'ũ' => 'u', 'Ū' => 'U', 'ū' => 'u', 'Ŭ' => 'U', 'ŭ' => 'u', 'Ů' => 'U', 'ů' => 'u', 'Ű' => 'U', 'ű' => 'u', 'Ų' => 'U', 'ų' => 'u', 'Ŵ' => 'W', 'ŵ' => 'w', 'Ŷ' => 'Y', 'ŷ' => 'y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'ź' => 'z', 'Ż' => 'Z', 'ż' => 'z', 'Ž' => 'Z', 'ž' => 'z', 'Ș' => 'S', 'ș' => 's', 'Ț' => 'T', 'ț' => 't', 'Ά' => 'A', 'Έ' => 'E', 'Ή' => 'I', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ώ' => 'O', 'ΐ' => 'i', 'Α' => 'A', 'Β' => 'V', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'I', 'Θ' => 'TH', 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => 'X', 'Ο' => 'O', 'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'CH', 'Ψ' => 'PS', 'Ω' => 'O', 'Ϊ' => 'I', 'Ϋ' => 'Y', 'ά' => 'a', 'έ' => 'e', 'ή' => 'i', 'ί' => 'i', 'ΰ' => 'y', 'α' => 'a', 'β' => 'v', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'i', 'θ' => 'th', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => 'x', 'ο' => 'o', 'π' => 'p', 'ρ' => 'r', 'ς' => 's', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'ch', 'ψ' => 'ps', 'ω' => 'o', 'ϊ' => 'i', 'ϋ' => 'y', 'ό' => 'o', 'ύ' => 'y', 'ώ' => 'o', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'TS', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SHT', 'Ъ' => 'A', 'Ь' => 'Y', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'ts', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sht', 'ъ' => 'a', 'ь' => 'y', 'ю' => 'yu', 'я' => 'ya', '€' => 'E'];
/**
* @type array IBAN country code => array of valid BIC country codes
*/
private const BIC_IBAN_COUNTRY_CODE_EXCEPTIONS = ['FR' => ['GF', 'GP', 'MQ', 'RE',
'PF', 'TF', 'YT', 'NC',
'BL', 'MF', 'PM', 'WF'],
'GB' => ['IM', 'GG', 'JE']];
private const EXCEPTIONAL_BICS = [
'NOTAVAIL' // used in Austria to mark that no BIC is provided
];
private static function toUpperWithoutWhitespaces(string $input): string
{
return strtoupper(preg_replace('/\s+/u', '', $input));
}
/*
* Checks if a creditor identifier (ci) is valid. Note that also if the ci is valid it does
* not have to exist
*
* @param string $ci
* @return string|false The valid IBAN or false if it is not valid
*/
public static function checkCreditorIdentifier(string $ci): string|bool
{
$ci = self::toUpperWithoutWhitespaces($ci);
if(!self::checkRestrictedPersonIdentifierSEPA($ci))
return false;
$ciCopy = $ci;
// remove creditor business code
$nationalIdentifier = substr($ci, 7);
$check = substr($ci, 0, 4);
$concat = $nationalIdentifier . $check;
$concat = preg_replace('#[^a-zA-Z0-9]#u', '', $concat); // remove all non-alpha-numeric characters
$concat = str_replace(self::ALPHABET, self::ALPHABET_VALUES, $concat);
if(self::iso7064Mod97m10ChecksumCheck($concat))
return $ciCopy;
return false;
}
/**
* Checks if an iban is valid. Note that also if the iban is valid it does not have to exist
*
* @param string $iban
* @param ?array $options valid keys:
* - checkByCheckSum (boolean): If true, the IBAN checksum is
* calculated (default:true)
* - checkByFormat (boolean): If true, the format is checked by
* regular expression (default: true)
* @return string|false The valid iban or false if it is not valid
*/
public static function checkIBAN(string $iban, ?array $options = null): string|bool
{
$iban = self::toUpperWithoutWhitespaces($iban);
if(self::checkStringAgainstRegex($iban, self::PATTERN_IBAN) === false)
return false;
$ibanCopy = $iban;
if(!isset($options['checkByFormat']) || $options['checkByFormat'])
{
$countryCode = substr($iban, 0, 2);
if(isset(self::IBAN_PATTERNS[$countryCode])
&& self::checkStringAgainstRegex($iban, self::IBAN_PATTERNS[$countryCode]) === false)
return false;
}
if(!isset($options['checkByCheckSum']) || $options['checkByCheckSum'])
{
$iban = str_replace(self::ALPHABET, self::ALPHABET_VALUES, $iban);
$bban = substr($iban, 6);
$check = substr($iban, 0, 6);
$concat = $bban . $check;
if(!self::iso7064Mod97m10ChecksumCheck($concat))
return false;
}
return $ibanCopy;
}
private static function iso7064Mod97m10ChecksumCheck(string $input): bool
{
$checksum = 0;
$len = strlen($input);
for($i = 1; $i <= $len; $i++)
{
$checksum = (($checksum + self::MOD_97_VALUES[$i - 1] * $input[$len - $i]) % 97);
}
return $checksum === 1;
}
/**
* Checks if a bic is valid. Note that also if the bic is valid it does not have to exist
*
* @param string $bic
* @param ?array $options Takes the following keys:
* - `allowEmptyBic`: (bool) The BIC can be empty.
* - `forceLongBic`: (bool) If the BIC has exact 8 characters, `forceLongBicStr`
* is added. (default false)
* - `forceLongBicStr`: string (default 'XXX')
* @return string|false the valid bic or false if it is not valid
*/
public static function checkBIC(string $bic, ?array $options = null): string|bool
{
$bic = self::toUpperWithoutWhitespaces($bic);
if(!empty($options['forceLongBic']) && strlen($bic) === 8)
$bic .= empty($options['forceLongBicStr']) ? 'XXX' : $options['forceLongBicStr'];
if(empty($bic) && !empty($options['allowEmptyBic']))
return '';
return self::checkStringAgainstRegex($bic, self::PATTERN_BIC);
}
/**
* Checks if both IBANs do belong to the same country.
* This function does not check if the IBANs are valid.
*
* @param string $iban1
* @param string $iban2
* @return bool
*/
public static function isNationalTransaction(string $iban1, string $iban2): bool
{
$iban1 = self::toUpperWithoutWhitespaces($iban1);
$iban2 = self::toUpperWithoutWhitespaces($iban2);
// check the country code
return substr($iban1, 0, 2) === substr($iban2, 0, 2);
}
/**
* Checks if both IBANs belong to the EEA (European Economic Area)
* This function does not check if the IBANs are valid.
*
* @param string $iban1
* @param string $iban2
* @return bool
*/
public static function isEEATransaction(string $iban1, string $iban2): bool
{
$iban1 = self::toUpperWithoutWhitespaces($iban1);
$iban2 = self::toUpperWithoutWhitespaces($iban2);
// check if both county codes belong to the EEA
$EEA = ['IS' => 1, 'LI' => 1, 'NO' => 1, 'BE' => 1, 'BG' => 1, 'DK' => 1, 'DE' => 1,
'EE' => 1, 'FI' => 1, 'FR' => 1, 'GR' => 1, 'IE' => 1, 'IT' => 1, 'HR' => 1,
'LV' => 1, 'LT' => 1, 'LU' => 1, 'MT' => 1, 'NL' => 1, 'AT' => 1, 'PL' => 1,
'PT' => 1, 'RO' => 1, 'SE' => 1, 'SK' => 1, 'SI' => 1, 'ES' => 1, 'CZ' => 1,
'HU' => 1, 'GB' => 1, 'CY' => 1];
return isset($EEA[substr($iban1, 0, 2)], $EEA[substr($iban2, 0, 2)]);
}
/**
* Checks if IBAN and BIC belong to the same country. If not, they also can not belong to
* each other.
*
* @param string $iban
* @param string $bic
* @return bool
*/
public static function crossCheckIbanBic(string $iban, string $bic): bool
{
// check for special cases
if(in_array(strtoupper($bic), self::EXCEPTIONAL_BICS))
return true;
$iban = self::toUpperWithoutWhitespaces($iban);
$bic = self::toUpperWithoutWhitespaces($bic);
// check the country code
$ibanCountryCode = substr($iban, 0, 2);
$bicCountryCode = substr($bic, 4, 2);
return $ibanCountryCode === $bicCountryCode
|| (isset(self::BIC_IBAN_COUNTRY_CODE_EXCEPTIONS[$ibanCountryCode])
&& in_array($bicCountryCode, self::BIC_IBAN_COUNTRY_CODE_EXCEPTIONS[$ibanCountryCode]));
}
private static function checkDateFormat(string $input): string|bool
{
$dateObj = DateTime::createFromFormat('Y-m-d', $input);
if($dateObj !== false && $input === $dateObj->format('Y-m-d'))
return $input;
return false;
}
/**
* Tries to convert the given date into the format YYYY-MM-DD (Y-m-d). Therefor it tries the
* following input formats in the order of appearance: d.m.Y, d.m.y, j.n.Y, j.n.y, m.d.Y,
* m.d.y, n.j.Y, n.j.y, Y/m/d, y/m/d, Y/n/j, y/n/j, Y.m.d, y.m.d, Y.n.j, y.n.j.
* Notice that this method tries to interpret the first number as day-of-month. This can
* lead to wrong dates if you have something like the 1st of April 2016 written as 04.01.2016.
* This will be interpreted as the 4th of January 2016. This is why you have to call this
* method on your owen risk, and it is not included in the sanitize() method.
*
* @param string $input The date that should be reformatted
* @param array $preferredFormats An array of formats that will be checked first.
* @return string|false The sanitized date or false, if it is not sanitizable.
*/
public static function sanitizeDateFormat(string $input, array $preferredFormats = []): string|bool
{
$dateFormats = ['d.m.Y', 'd.m.y', 'j.n.Y', 'j.n.y', 'm.d.Y', 'm.d.y', 'n.j.Y', 'n.j.y',
'Y/m/d', 'y/m/d', 'Y/n/j', 'y/n/j', 'Y.m.d', 'y.m.d', 'Y.n.j', 'y.n.j'];
// input is already in the correct format?
$dateObj = DateTime::createFromFormat('Y-m-d', $input);
if($dateObj !== false)
return $input;
foreach($preferredFormats as $format)
{
$dateObj = DateTime::createFromFormat($format, $input);
if($dateObj !== false)
return $dateObj->format('Y-m-d');
}
foreach($dateFormats as $format)
{
$dateObj = DateTime::createFromFormat($format, $input);
if($dateObj !== false)
return $dateObj->format('Y-m-d');
}
return false;
}
/**
* Checks if the input has the format 'Y-m-d\TH:i:s'
* @param string $input
* @return string|false Returns $input if it is valid and false else.
*/
public static function checkCreateDateTime(string $input): string|bool
{
return self::checkDateTimeFormat($input);
}
/**
* Checks if the input has the format 'Y-m-d\TH:i:s'
* @param string $input
* @return string|false Returns $input if it is valid and false else.
*/
private static function checkDateTimeFormat(string $input): string|bool
{
$dateObj = DateTime::createFromFormat('Y-m-d\TH:i:s', $input);
if($dateObj !== false && $input === $dateObj->format('Y-m-d\TH:i:s'))
return $input;
return false;
}
private static function getDateObj(?string $date = null, string $inputFormat = 'd.m.Y'): DateTime|bool
{
try
{
return empty($date) ? new DateTime() : DateTime::createFromFormat($inputFormat, $date);
}
catch(Exception)
{
return false;
}
}
/**
* Reformat a date string from a given format to the ISODate format. Notice: 20.13.2014 is
* valid and becomes 2015-01-20.
*
* @param ?string $date A date string of the given input format
* @param string $inputFormat default is the german format DD.MM.YYYY
* @return string|false date as YYYY-MM-DD or false if the input is invalid
*/
public static function getDate(?string $date = null, string $inputFormat = 'd.m.Y'): string|bool
{
$dateTimeObj = self::getDateObj($date, $inputFormat);
if($dateTimeObj === false)
return false;
return $dateTimeObj->format('Y-m-d');
}
/**
* Computes the next TARGET2 day (including today) with respect to a TARGET2 offset.
*
* @param int $workdayOffset a positive number of workdays to skip.
* @param ?string $today if set, this date is used as today
* @param string $inputFormat
* @return string|false date as YYYY-MM-DD or false if the input is invalid
*/
public static function getDateWithOffset(int $workdayOffset, ?string $today = null, string $inputFormat = 'd.m.Y'): string|bool
{
$dateTimeObj = self::getDateObj($today, $inputFormat);
if($dateTimeObj === false)
return false;
$isTargetDay = self::dateIsTargetDay($dateTimeObj);
while(!$isTargetDay || $workdayOffset > 0)
{
date_modify($dateTimeObj, '+1 day');
if($isTargetDay)
$workdayOffset--;
$isTargetDay = self::dateIsTargetDay($dateTimeObj);
}
return $dateTimeObj->format('Y-m-d');
}
/**
* Returns the target date, if it has at least the given offset of TARGET2 days form
* today. Else the earliest date that respects the offset is returned.
*
* @param string $target
* @param int $workdayMinOffset
* @param string $inputFormat
* @param ?string $today
* @return string|false date as YYYY-MM-DD or false if the input is invalid
*/
public static function getDateWithMinOffsetFromToday(string $target, int $workdayMinOffset, string $inputFormat = 'd.m.Y', ?string $today = null): string|bool
{
$targetDateObj = self::getDateObj($target, $inputFormat);
$earliestDate = self::getDateWithOffset($workdayMinOffset, $today, $inputFormat);
if($targetDateObj === false || $earliestDate === false)
return false;
try
{
$earliestDateObj = new DateTime($earliestDate);
}
catch(Exception)
{
return false;
}
$isTargetDay = self::dateIsTargetDay($targetDateObj);
while(!$isTargetDay)
{
date_modify($targetDateObj, '+1 day');
$isTargetDay = self::dateIsTargetDay($targetDateObj);
}
if($targetDateObj > $earliestDateObj)
return $targetDateObj->format('Y-m-d');
return $earliestDateObj->format('Y-m-d');
}
/**
* Checks if $date is a SEPA TARGET day. Every day is a TARGET day except for saturdays, sundays
* New Year's Day, Good Friday, Easter Monday, the may holiday, first and second Christmas holiday.
* @param DateTime $date
* @return bool
*/
private static function dateIsTargetDay(DateTime $date): bool
{
// $date is a saturday or sunday?
if($date->format('N') === '6' || $date->format('N') === '7')
return false;
$day = $date->format('m-d');
if($day === '01-01' // New Year's Day
|| $day === '05-01' // labour day
|| $day === '12-25' // first Christmas day
|| $day === '12-26') // second Christmas day
return false;
$year = $date->format('Y');
$easter = easterDate((int) $year); // contains Easter Sunday
// date_modify does not throw in contrast to Date->modify. The date string is static so exception handling is not needed
$goodFriday = date_modify($easter, '-2 days')->format('m-d'); // $easter contains now Good Friday
$easterMonday = date_modify($easter, '+3 days')->format('m-d'); // $easter contains now Easter Monday
if($day === $goodFriday || $day === $easterMonday)
return false;
return true;
}
/**
* @param array $input Reference to an array
* @param int|string|string[] $keys The keys of the multidimensional array in order of
* appearance. e.g. `['key1','key2']` checks
* `$arr['key1']['key2']`
* @return mixed|false Returns the value of the field or null if the field does not exist.
*/
private static function getValFromMultiDimInput(array &$input, int|string|array $keys): mixed
{
$key = is_array($keys) ? array_shift($keys) : $keys;
if(!isset($input[$key]))
return false;
if(is_array($keys) && !empty($keys)) // another dimension
return self::getValFromMultiDimInput($input[$key], $keys);
return $input[$key];
}
/**
* Checks if the input holds for the field.
*
* @param string $field Valid fields are: 'orgnlcdtrschmeid_id','ci','msgid','pmtid','pmtinfid',
* 'orgnlmndtid','mndtid','initgpty','cdtr','dbtr','orgnlcdtrschmeid_nm',
* 'ultmtcdtr','ultmtdbtr','rmtinf','orgnldbtracct_iban','iban','bic',
* 'ccy','amendment', 'btchbookg','instdamt','seqtp','lclinstrm',
* 'elctrncsgntr','reqdexctndt','reqdexctndttm','purp','ctgypurp','orgnldbtragt', 'adrline'
* 'ctry', 'dbtrpstladr', 'cdtrpstladr', 'pstladr', 'orgid_id', 'orgid_sm', `bldgnm`,`bldgnb`,
* `twnnm`, `twnlctnnm`, `dstrctnm`, `ctrysubdvsn`, `pstbx`, `pstcd`, `dept`, `subdept`,
* `strtnm`, `flr`, `room`
* @param mixed $input
* @param ?array $options See `checkBIC()`, `checkIBAN()` and `checkLocalInstrument()` for
* details. In addition one can use the key `version`, which is relevant
* for validation 'mndtid'.
* @return false|mixed The checked input or false, if it is not valid
*/
public static function check(string $field, mixed $input, ?array $options = null): mixed
{
$field = strtolower($field);
$version = $options['version'] ?? null;
switch($field) // fall-through's are on purpose
{
case 'orgnlcdtrschmeid_id':
case 'ci':
return self::checkCreditorIdentifier($input);
case 'msgid':
case 'instrid':
case 'pmtid': // next line
case 'esr':
case 'mmbid':
case 'lsv':
case 'pmtinfid':
return self::checkRestrictedIdentificationSEPA1($input);
case 'orgnlmndtid':
case 'mndtid':
return $version === self::SEPA_PAIN_008_001_02
|| $version === self::SEPA_PAIN_008_001_02_GBIC
? self::checkRestrictedIdentificationSEPA1($input)
: self::checkRestrictedIdentificationSEPA2($input);
case 'bldgnb':
case 'pstbx':
case 'pstcd':
return self::checkText($input, 16);
case 'initgptyid':
if($version === self::SEPA_PAIN_008_001_02_AUSTRIAN_003)
return false; // not supported on this version
// if not => fall through to dbtr
case 'initgpty': // cannot be empty (and the following things also)
case 'cdtr': // cannot be empty (and the following things also)
case 'dbtr':
if(empty($input))
return false; // cannot be empty
// if not => fall through to orgid_id
case 'ultmtdbtrid':
case 'orgid_sm':
case 'orgid_id':
case 'bldgnm':
case 'twnnm':
case 'twnlctnnm':
case 'dstrctnm':
case 'ctrysubdvsn':
return self::checkText($input, self::TEXT_LENGTH_VERY_SHORT);
case 'adrline':
if(is_array($input))
{
if(count($input) === 0 || count($input) > 2)
return false;
foreach($input as &$value)
{
$value = self::check('adrline', $value, $options);
}
return in_array(false, $input, true) ? false : array_values($input);
} // if not => fall through to ultmtdbtr
case 'orgnlcdtrschmeid_nm':
case 'ultmtcdrt': // deprecated, just here for backwards compatibility
case 'ultmtcdtr':
case 'ultmtdbtr':
case 'dept':
case 'subdept':
case 'strtnm':
case 'flr':
case 'room':
return self::checkText($input, self::TEXT_LENGTH_SHORT);
case 'rmtinf':
return self::checkText($input, self::TEXT_LENGTH_LONG);
case 'orgnldbtracct_iban':
case 'iban':
return self::checkIBAN($input, $options);
case 'orgnldbtragt_bic':
case 'orgid_bob':
case 'bic':
return self::checkBIC($input, $options);
case 'ccy':
return self::checkActiveOrHistoricCurrencyCode($input);
case 'amdmntind':
case 'btchbookg':
return self::checkBoolean($input);
case 'instdamt':
return self::checkAmountFormat($input);
case 'seqtp':
return self::checkSeqType($input);
case 'lclinstrm':
return self::checkLocalInstrument($input, $options);
case 'elctrncsgntr':
return self::checkText($input, 1025);
case 'dtofsgntr':
case 'reqdcolltndt':
case 'reqdexctndt':
return self::checkDateFormat($input);
case 'reqdexctndttm':
return self::checkDateTimeFormat($input);
case 'purp':
return self::checkPurpose($input);
case 'ctgypurp':
return self::checkCategoryPurpose($input);
case 'ref':
case 'orgnldbtragt':
return $input; // nothing to check here
case 'ctry':
return self::checkCountryCode($input);
case 'pstladr':
if(in_array($version, [self::SEPA_PAIN_001_001_09, self::SEPA_PAIN_008_001_08], true))
{
if($input === null) return true;
if(!is_array($input)) return false;
$adrKeys = array_map(fn($k) => strtolower($k), array_keys($input));
foreach(['ctry', 'twnnm'] as $reqKey)
{
if(!in_array($reqKey, $adrKeys, true)) return false;
}
foreach($input as $key => &$value)
{
if(!in_array(strtolower($key), ['ctry',
'bldgnm',
'twnnm',
'twnlctnnm',
'dstrctnm',
'ctrysubdvsn',
'bldgnb',
'pstbx',
'pstcd',
'dept',
'subdept',
'strtnm',
'flr',
'room'], true))
return false;
$value = self::check($key, $value, $options);
}
return in_array(false, $input, true) ? false : $input;
} // if not => fall through to cdtrpstladr
case 'cdtrpstladr':
case 'dbtrpstladr':
if(is_array($input) && count($input) > 0 && count($input) <= 2)
{
foreach($input as $key => &$value)
{
if(!in_array(strtolower($key), ['ctry', 'adrline'], true))
return false;
$value = self::check($key, $value, $options);
}
return in_array(false, $input, true) ? false : $input;
} // if not => fall through
default:
return false;
}
}
/**
* This function checks if the index of the inputArray exists and if the input is valid. The
* function can be called as `checkInput($fieldName,$_POST,['input',$fieldName],$options)`
* and equals `check($fieldName,$_POST['input'][$fieldName],$options)`, but checks first, if
* the index exists.
*
* @param string $field see `check()` for valid values.
* @param array $inputArray
* @param string|int|array $inputKeys
* @param ?array $options see `check()` for valid values.
* @return mixed|false
*/
public static function checkInput(string $field, array &$inputArray, string|int|array $inputKeys, ?array $options = null): mixed
{
$value = self::getValFromMultiDimInput($inputArray, $inputKeys);
if($value === false)
return false;
return self::check($field, $value, $options);
}
/**
* This function checks if the index of the inputArray exists and if the input is valid. The
* function can be called as `sanitizeInput($fieldName,$_POST,['input',$fieldName],$flags)`
* and equals `sanitize($fieldName,$_POST['input'][$fieldName],$flags)`, but checks first, if
* the index exists.
* @param string $field see `sanitize()` for valid values.
* @param array $inputArray
* @param string|int|array $inputKeys
* @param int $flags see `sanitize()` for valid values.
* @return mixed|false
*/
public static function sanitizeInput(string $field, array &$inputArray, string|int|array $inputKeys, int $flags = 0): mixed
{
$value = self::getValFromMultiDimInput($inputArray, $inputKeys);
if($value === false)
return false;
return self::sanitize($field, $value, $flags);
}
/**
* Checks the input and tries to sanitize it if it is not valid.
*
* @param string $field all fields check and/or sanitize supports
* @param mixed $input
* @param int $flags see `sanitize()` for details
* @param ?array $options see `check()` for details
* @return mixed|false
*/
public static function checkAndSanitize(string $field, mixed $input, int $flags = 0, ?array $options = null): mixed
{
$checkedInput = self::check($field, $input, $options);
if($checkedInput !== false)
return $checkedInput;
return self::sanitize($field, $input, $flags);
}
/**
* Checks if the index of the inputArray exists and if the input is valid. The
* function can be called as `checkAndSanitizeInput($fieldName,$_POST,['input',$fieldName],$flags,$options)`
* and equals `checkAndSanitize($fieldName,$_POST['input'][$fieldName],$flags,$options)`, but checks first, if
* the index exists.
*
* @param string $field see `checkAndSanitize()` for valid values.
* @param array $inputArray
* @param string|int|array $inputKeys
* @param int $flags see `checkAndSanitize()` for valid values.
* @param ?array $options see `checkAndSanitize()` for valid values.
* @return mixed|false
*/
public static function checkAndSanitizeInput(string $field, array &$inputArray, string|int|array $inputKeys, int $flags = 0, ?array $options = null): mixed
{
$value = self::getValFromMultiDimInput($inputArray, $inputKeys);
if($value === false)
return false;
return self::checkAndSanitize($field, $value, $flags, $options);
}
/**
* Checks and sanitizes all fields in $input. $input is modified!
* @param array $inputs A reference to an input array (field => value)
* @param int $flags Flags for sanitizing
* @param array|null $options Options for checking
* @return true|array returns true if everything is ok or could be sanitized. Otherwise an
* array of field keys that could not be sanitized is returned.
*/
public static function checkAndSanitizeAll(array &$inputs, int $flags = 0, ?array $options = null): bool|array
{
$fieldsWithErrors = [];
foreach($inputs as $field => &$input)
{
$input = self::checkAndSanitize($field, $input, $flags, $options);
if($input === false)
$fieldsWithErrors[] = $field;
}
if(empty($fieldsWithErrors))
return true;
return $fieldsWithErrors;
}
/**
* @param int $length Use SepaUtilities::TEXT_LENGTH_* constants
* @param string $input
* @param bool $allowEmpty
* @param int $flags See `replaceSpecialChars()` for allowed flags
* @return string|false Sanitized $input or false if the input could not be sanitized.
*/
public static function sanitizeText(int $length, string $input, bool $allowEmpty = false, int $flags = 0): string|bool
{
$res = self::sanitizeLength(self::replaceSpecialChars($input, $flags), $length);
if($allowEmpty || !empty($res))
return $res;
return false;
}
/**
* Tries to sanitize the input so it fits in the field.
*
* @param string $field Valid fields are: 'ultmtcdtr', 'ultmtdbtr',
* 'orgnlcdtrschmeid_nm', 'initgpty', 'cdtr', 'dbtr', 'rmtinf', 'adrline'
* 'orgid_sm', 'orgid_id'
* @param mixed $input
* @param int $flags Flags used in replaceSpecialChars()
* @return mixed|false The sanitized input or false if the input is not sanitizable or
* invalid also after sanitizing.
*/
public static function sanitize(string $field, mixed $input, int $flags = 0): mixed
{
$field = strtolower($field);
switch($field) // fall-through's are on purpose
{
case 'orgid_sm':
case 'orgid_id':
return self::sanitizeText(self::TEXT_LENGTH_VERY_SHORT, $input, true, $flags);
case 'adrline':
if(is_array($input))
{
foreach($input as &$value)
$value = self::sanitize($field, $value, $flags);
return in_array(false, $input, true) ? false : $input;
}
// fallthrough on purpose
case 'ultmtcdrt': // deprecated, just here for backwards compatibility
case 'ultmtcdtr':
case 'ultmtdbtr':
case 'ultmtdebtr': // deprecated, just here for backwards compatibility
return self::sanitizeText(self::TEXT_LENGTH_SHORT, $input, true, $flags);
case 'orgnlcdtrschmeid_nm':
case 'initgpty':
case 'cdtr':
case 'dbtr':
return self::sanitizeText(self::TEXT_LENGTH_SHORT, $input, false, $flags);