-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFixTWU.php
More file actions
59 lines (43 loc) · 1.92 KB
/
FixTWU.php
File metadata and controls
59 lines (43 loc) · 1.92 KB
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
<?php
// Program expect to get an input path, and an output path.
require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$inputDirectory = new DirectoryIterator($argv[0]);
$outputDirectory = $argv[1];
function fixTWUOnus(string $filename, string $outputDirectory)
{
$file = new X937\X937File($filename);
$writerX937 = \X937\Writer\Factory::Generate(\X937\Writer\Factory::FORMAT_FILE_X937, $outputDirectory . $filename);
// loop through each record, and correct record type 25s that have an onus
// field (6) that doesn't match our format. Writer everything back out.
foreach ($file as $record) {
if ($record->getType() == '25') {
$onus = $record->getFieldByNumber('6')->getValue();
$position = $record->getFieldByNumber('6')->getPosition();
if (preg_match('/\//', $onus)) {
// echo "'$onus' VALID" . PHP_EOL;
} else {
$rawData = $record->getDataRaw();
$data = $record->getData();
$onusNoSpace = str_replace(' ', '', $onus);
$onusSlashAdded = $onusNoSpace . '/';
$onusPadded = str_pad($onusSlashAdded, 20, ' ', STR_PAD_LEFT);
$onusDataLength = strlen($onusPadded);
if ($onusDataLength > 20) {
echo "onus to long, aborting!";
break;
}
$newData = substr_replace($data, $onusPadded, $position - 1, 20);
$record->setData($newData);
}
}
$writerX937->writeRecord($record);
}
}
$timeStart = microtime(true);
foreach ($inputDirectory as $fileInfo) {
if ($fileinfo->getExtension == 'X937') {
fixTWUOnus($fileinfo->getPathname, $outputDirectory);
}
}
$timeEnd = microtime(true);
echo $timeEnd - $timeStart . PHP_EOL;