forked from jschaedl/iban-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswift.php
113 lines (95 loc) · 3.7 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
<?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 > Swift/iban_registry.yaml";
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 (strpos($line, 'IBAN prefix country code (ISO 3166)') !== false) {
$countryCodes = preg_split('/\t+/', $line);
}
if (strpos($line, 'Name of country') !== false) {
$countryNames = preg_split('/\t+/', $line);
}
if (strpos($line, 'IBAN structure') !== false) {
$ibanStructure = preg_split('/\t+/', $line);
}
if (strpos($line, 'BBAN structure') !== false) {
$bbanStructure = preg_split('/\t+/', $line);
}
if (strpos($line, 'IBAN length') !== false) {
$ibanLength = preg_split('/\t+/', $line);
}
if (strpos($line, 'BBAN length') !== false) {
$bbanLength = preg_split('/\t+/', $line);
}
if (strpos($line, 'IBAN electronic format example') !== false) {
$ibanElectronicFormatExamples = preg_split('/\t+/', $line);
}
if (strpos($line, 'IBAN print format example') !== false) {
$ibanPrintFormatExamples = preg_split('/\t+/', $line);
}
if (strpos($line, 'Branch identifier position within the BBAN') !== false) {
$branchIdentifierPosition = preg_split('/\t+/', $line);
}
if (strpos($line, 'Branch identifier pattern') !== false) {
$branchIdentifierStructure = preg_split('/\t+/', $line);
}
}
$regexConverter = new RegexConverter();
$registry = [];
foreach ($countryCodes as $key => $countryCode) {
if (0 === $key) {
continue;
}
$registry[trim($countryCode)] = [
'country_name' => trim($countryNames[$key]),
'iban_structure' => trim($ibanStructure[$key]),
'bban_structure' => trim($bbanStructure[$key]),
'iban_regex' => '/^' . $regexConverter->convert(trim($ibanStructure[$key])) . '$/',
'bban_regex' => '/^' . $regexConverter->convert(trim($bbanStructure[$key])) . '$/',
'iban_length' => intval(trim($ibanLength[$key])),
'bban_length' => intval(trim($bbanLength[$key])),
'iban_electronic_format_example' => trim($ibanElectronicFormatExamples[$key]),
'iban_print_format_example' => trim($ibanPrintFormatExamples[$key]),
'branch_identifier_position' => trim($branchIdentifierPosition[$key]),
'branch_identifier_structure' => trim($branchIdentifierStructure[$key]),
'branch_identifier_regex' => empty(trim($branchIdentifierStructure[$key])) ? '' : '/^' . $regexConverter->convert(trim($branchIdentifierStructure[$key])) . '$/',
];
}
echo(Yaml::dump($registry));
exit(0);