-
Notifications
You must be signed in to change notification settings - Fork 23
/
parse.php
105 lines (77 loc) · 2.44 KB
/
parse.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
<?php
declare(strict_types=1);
/**
* This script needed only for one-time building the database after the submodule update
*/
$banks_db_path = __DIR__ . '/banks-db/banks';
if (file_exists('db/bank_db.php')) {
unlink('db/bank_db.php');
}
if (!file_exists($banks_db_path . '/index.js')) {
echo 'Please update git submodule';
exit(1);
}
$database = [
'prefixes' => [],
'banks' => [],
];
$bank_id = 0;
$max_length = 6;
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($banks_db_path));
/** @var SplFileInfo $file */
foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
if ($file->getExtension() !== 'json') {
continue;
}
$json_file = $file->openFile();
$contents = $json_file->fread($json_file->getSize());
$bank_data = json_decode($contents, true);
if (!isset($bank_data['prefixes'])) {
printError('Corrupted file: ' . $file->getPathname());
continue;
}
++$bank_id;
$bank_data_stored = $bank_data;
unset($bank_data_stored['prefixes']);
$database['banks'][$bank_id] = $bank_data_stored;
foreach ($bank_data['prefixes'] as $prefix) {
$len = strlen((string) $prefix);
for ($i = $len; $i <= $len; $i++) {
$len_diff = $max_length - $len;
if ($len_diff > 0) {
for ($l = 1; $l <= $len_diff; $l++) {
$count_prefixes = 10 ** $l;
for ($k = 0; $k < $count_prefixes; $k++) {
$new_prefix = (int) sprintf('%d%d', $prefix, $k);
addPrefix($new_prefix, $bank_id, $database);
}
}
} else {
addPrefix($prefix, $bank_id, $database);
}
}
}
}
$database_export = sprintf('<?php return %s;', var_export($database, true));
if (file_put_contents('db/bank_db.php', $database_export)) {
echo sprintf(
"Successfully exported %d prefixes for %d banks with prefixes\n",
count($database['prefixes']),
count($database['banks'])
);
}
function printError(string $text)
{
echo '! ' . $text . "\n";
}
function addPrefix(int $prefix, int $bank_id, array &$database)
{
if (isset($database['prefixes'][$prefix])) {
printError(sprintf('Duplicated prefix: %s (%s <-> %s)', $prefix, $bank_id, $database['prefixes'][$prefix]));
return;
}
$database['prefixes'][$prefix] = $bank_id;
}