UTF8 conversion with iconv implemented

This commit is contained in:
Marko Jovanovic 2026-10-26 16:35:29 +01:00
parent 196317a62c
commit 78b21bdb36

View File

@ -72,7 +72,24 @@ final class CleanMobileCommand extends Command
// -------------------------------------------------------------
// 2️⃣ CSV reader / writer (semicolon‑separated)
// -------------------------------------------------------------
$csvReader = Reader::createFromPath($inputPath, 'r');
$contents = file_get_contents($inputPath);
if ($contents === false) {
$io->error("Could not read input file: $inputPath");
return Command::FAILURE;
}
// Keep UTF-8 files unchanged; convert legacy exports before parsing
// so every field (including clinic names and headers) is UTF-8.
if (preg_match('//u', $contents) !== 1) {
$contents = iconv('ISO-8859-1', 'UTF-8', $contents);
if ($contents === false) {
$io->error('Could not convert input file from ISO-8859-1 to UTF-8.');
return Command::FAILURE;
}
$io->note('Converted input from ISO-8859-1 to UTF-8.');
}
$csvReader = Reader::createFromString($contents);
$csvReader->setDelimiter(';');
$csvReader->setHeaderOffset(0); // first line = header
$header = $csvReader->getHeader();
@ -95,26 +112,6 @@ final class CleanMobileCommand extends Command
'17',
];
$sanitiseUtf8 = static function(?string $raw): ?string {
$utf8 = null;
if (mb_check_encoding($raw, 'UTF-8')) {
$utf8 = $raw;
} else {
$encodings = ['Windows-1252', 'ISO-8859-1', 'CP1252', 'ASCII'];
foreach ($encodings as $src) {
$utf8 = @iconv($src, 'UTF-8//TRANSLIT//IGNORE', $raw);
if ($utf8 !== false) {
break;
}
}
if ($utf8 === false) {
// Could not be converted – treat it as “invalid”.
return null;
}
}
return $utf8;
};
// -------------------------------------------------------------
// 4️⃣ Helper closures
// -------------------------------------------------------------
@ -234,7 +231,7 @@ final class CleanMobileCommand extends Command
$contact->setParsedAt(new \DateTimeImmutable());
$contact->setStudyId($study_id);
$contact->setParsedFileLinenum($rowCount + 1);
$contact->setParsedFileLine($sanitiseUtf8(implode(';', $row)));
$contact->setParsedFileLine(implode(';', $row));
// $contact->setMsgContentType($rowCount % 2 ? 1 : 2); // alternativ lang oder kurz
$contact->setMsgContentType(2); // nur kurzformat
$quartal = $today->format('Y') . ceil($today->format('n') / 3);