-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathswift.php
130 lines (108 loc) · 5.17 KB
/
swift.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
<?php
/*
* This file is part of the iban-validation library.
*
* (c) Jan Schädlich <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* This script reads the data from the iban_registry.txt provided by SWIFT and converts it into a yaml structure.
* An up to date iban registry file can be found here: https://www.swift.com/taxonomy/term/3876.
*
* @author Jan Schädlich <[email protected]>
*/
$usage = 'php swift.php iban_registry.txt > Resource/iban_registry.php';
if (2 !== $argc) {
echo 'Please provide path to iban_registry file provided by SWIFT!'.PHP_EOL;
echo 'Use: '.$usage.PHP_EOL;
exit(1);
}
$filename = __DIR__.'/'.$argv[1];
if (!file_exists($filename)) {
echo 'Given iban_registry file does not exist!'.PHP_EOL;
exit(1);
}
require_once __DIR__.'/vendor/autoload.php';
use Iban\Validation\Swift\RegexConverter;
use Symfony\Component\Yaml\Yaml;
$lines = file($filename);
$countryCodes = [];
$countryNames = [];
$ibanStructure = [];
$bbanStructure = [];
$ibanLength = [];
$bbanLength = [];
$ibanElectronicFormatExamples = [];
$ibanPrintFormatExamples = [];
$branchIdentifierPosition = [];
$branchIdentifierStructure = [];
foreach ($lines as $lineNumber => $line) {
if (false !== strpos($line, 'IBAN prefix country code (ISO 3166)')) {
$countryCodes = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'Name of country')) {
$countryNames = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'IBAN structure')) {
$ibanStructure = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'BBAN structure')) {
$bbanStructure = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'IBAN length')) {
$ibanLength = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'BBAN length')) {
$bbanLength = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'IBAN electronic format example')) {
$ibanElectronicFormatExamples = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'IBAN print format example')) {
$ibanPrintFormatExamples = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'Bank identifier position within the BBAN')) {
$bankIdentifierPosition = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'Bank identifier pattern')) {
$bankIdentifierStructure = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'Branch identifier position within the BBAN')) {
$branchIdentifierPosition = preg_split('/\t/', $line);
}
if (false !== strpos($line, 'Branch identifier pattern')) {
$branchIdentifierStructure = preg_split('/\t/', $line);
}
}
$regexConverter = new RegexConverter();
$registry = [];
foreach ($countryCodes as $key => $countryCode) {
if (0 === $key) {
continue;
}
$registry[trim($countryCode)] = [
'country_name' => isset($countryNames[$key]) ? trim($countryNames[$key]) : '',
'iban_structure' => isset($ibanStructure[$key]) ? trim($ibanStructure[$key]) : '',
'bban_structure' => isset($bbanStructure[$key]) ? trim($bbanStructure[$key]) : '',
'iban_regex' => isset($ibanStructure[$key]) ? '/^'.$regexConverter->convert(trim($ibanStructure[$key])).'$/' : '',
'bban_regex' => isset($bbanStructure[$key]) ? '/^'.$regexConverter->convert(trim($bbanStructure[$key])).'$/' : '',
'iban_length' => isset($ibanLength[$key]) ? intval(trim($ibanLength[$key])) : '',
'bban_length' => isset($bbanLength[$key]) ? intval(trim($bbanLength[$key])) : '',
'iban_electronic_format_example' => isset($ibanElectronicFormatExamples[$key]) ? trim($ibanElectronicFormatExamples[$key]) : '',
'iban_print_format_example' => isset($ibanPrintFormatExamples[$key]) ? trim($ibanPrintFormatExamples[$key]) : '',
'bank_identifier_position' => isset($bankIdentifierPosition[$key]) ? (trim('N/A' === $bankIdentifierPosition[$key] ? '' : $bankIdentifierPosition[$key])) : '',
'bank_identifier_structure' => isset($bankIdentifierStructure[$key]) ? (trim('N/A' === $bankIdentifierStructure[$key] ? '' : $bankIdentifierStructure[$key])) : '',
'bank_identifier_regex' => empty(trim('N/A' === $bankIdentifierStructure[$key] ? '' : $bankIdentifierStructure[$key])) ? '' : '/^'.$regexConverter->convert(trim($bankIdentifierStructure[$key])).'$/',
'branch_identifier_position' => isset($branchIdentifierPosition[$key]) ? (trim('N/A' === $branchIdentifierPosition[$key] ? '' : $branchIdentifierPosition[$key])) : '',
'branch_identifier_structure' => isset($branchIdentifierStructure[$key]) ? (trim('N/A' === $branchIdentifierStructure[$key] ? '' : $branchIdentifierStructure[$key])) : '',
'branch_identifier_regex' => empty('N/A' === trim($branchIdentifierStructure[$key]) ? '' : $branchIdentifierStructure[$key]) ? '' : '/^'.$regexConverter->convert(trim($branchIdentifierStructure[$key])).'$/',
];
}
echo '<?php '.PHP_EOL.PHP_EOL;
echo sprintf('# %s', basename($filename)).PHP_EOL.PHP_EOL;
echo 'return '.PHP_EOL;
echo var_export($registry, true);
echo ';'.PHP_EOL;
exit(0);