extended firefly import options, cleaned up language usage in code, new README in english, changed license references
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
co-authored by
Copilot
parent
90dc37cafc
commit
a6ed05a38a
+35
-35
@@ -3,10 +3,10 @@
|
||||
namespace UbsCsvTransformer;
|
||||
|
||||
/**
|
||||
* Liest und parst CSV-Dateien
|
||||
* Reads and parses CSV files
|
||||
*
|
||||
* Diese Klasse liest CSV-Dateien mit konfigurierbarem Delimiter
|
||||
* und separiert Metadaten-Zeilen von den eigentlichen Datenzeilen.
|
||||
* Reads CSV files with a configurable delimiter and separates
|
||||
* metadata lines from the actual data rows.
|
||||
*/
|
||||
class CsvReader
|
||||
{
|
||||
@@ -16,8 +16,8 @@ class CsvReader
|
||||
private bool $hasBom;
|
||||
|
||||
/**
|
||||
* @param string $filePath Pfad zur CSV-Datei
|
||||
* @param array $csvStructure CSV-Struktur aus Konfiguration
|
||||
* @param string $filePath Path to the CSV file
|
||||
* @param array $csvStructure CSV structure from configuration
|
||||
*/
|
||||
public function __construct(string $filePath, array $csvStructure)
|
||||
{
|
||||
@@ -28,25 +28,25 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Liest alle Zeilen aus der Datei
|
||||
* Reads all lines from the file
|
||||
*
|
||||
* @param int $maxLines Maximale Anzahl Zeilen (0 = alle)
|
||||
* @return array Array mit Zeilen (ohne Newlines)
|
||||
* @throws \RuntimeException wenn Datei nicht gelesen werden kann
|
||||
* @param int $maxLines Maximum number of lines (0 = all)
|
||||
* @return array Array of lines (without newlines)
|
||||
* @throws \RuntimeException if file cannot be read
|
||||
*/
|
||||
public function readLines(int $maxLines = 0): array
|
||||
{
|
||||
if (!file_exists($this->filePath) || !is_readable($this->filePath)) {
|
||||
throw new \RuntimeException("Konnte Datei nicht lesen: {$this->filePath}");
|
||||
throw new \RuntimeException("Could not read file: {$this->filePath}");
|
||||
}
|
||||
|
||||
$lines = file($this->filePath, FILE_IGNORE_NEW_LINES);
|
||||
|
||||
if ($lines === false) {
|
||||
throw new \RuntimeException("Konnte Datei nicht lesen: {$this->filePath}");
|
||||
throw new \RuntimeException("Could not read file: {$this->filePath}");
|
||||
}
|
||||
|
||||
// BOM entfernen falls vorhanden
|
||||
// Remove BOM if present
|
||||
if ($this->hasBom && !empty($lines)) {
|
||||
$lines[0] = $this->removeBom($lines[0]);
|
||||
}
|
||||
@@ -59,9 +59,9 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Liest die Metadaten-Zeilen (vor der Header-Zeile)
|
||||
* Reads the metadata lines (before the header line)
|
||||
*
|
||||
* @return array Array mit Metadaten-Zeilen
|
||||
* @return array Array of metadata lines
|
||||
*/
|
||||
public function readMetadataLines(): array
|
||||
{
|
||||
@@ -75,28 +75,28 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Liest die CSV-Daten mit Headers
|
||||
* Reads CSV data with headers
|
||||
*
|
||||
* @param int $maxDataRows Maximale Anzahl Datenzeilen (0 = alle)
|
||||
* @return array Array von assoziativen Arrays (mit Spalten-Namen als Keys)
|
||||
* @throws \RuntimeException wenn Header-Zeile nicht gefunden
|
||||
* @param int $maxDataRows Maximum number of data rows (0 = all)
|
||||
* @return array Array of associative arrays (with column names as keys)
|
||||
* @throws \RuntimeException if header line is not found
|
||||
*/
|
||||
public function readCsvData(int $maxDataRows = 0): array
|
||||
{
|
||||
$lines = $this->readLines();
|
||||
|
||||
if ($this->headerLine > count($lines)) {
|
||||
throw new \RuntimeException("Header-Zeile {$this->headerLine} nicht gefunden in Datei mit " . count($lines) . " Zeilen");
|
||||
throw new \RuntimeException("Header line {$this->headerLine} not found in file with " . count($lines) . " lines");
|
||||
}
|
||||
|
||||
// Header parsen
|
||||
// Parse header
|
||||
$headerLineContent = $lines[$this->headerLine - 1];
|
||||
$headers = str_getcsv($headerLineContent, $this->delimiter, '"', '\\');
|
||||
$headers = array_map(static fn(?string $v): string => trim($v ?? ''), $headers);
|
||||
|
||||
// Datenzeilen parsen
|
||||
// Parse data rows
|
||||
$data = [];
|
||||
$dataStartLine = $this->headerLine; // 0-basiert
|
||||
$dataStartLine = $this->headerLine; // 0-based
|
||||
$lineCount = 0;
|
||||
|
||||
for ($i = $dataStartLine; $i < count($lines); $i++) {
|
||||
@@ -106,7 +106,7 @@ class CsvReader
|
||||
|
||||
$lineContent = $lines[$i];
|
||||
|
||||
// Leere Zeilen überspringen
|
||||
// Skip empty lines
|
||||
if (trim($lineContent) === '') {
|
||||
continue;
|
||||
}
|
||||
@@ -114,7 +114,7 @@ class CsvReader
|
||||
$row = str_getcsv($lineContent, $this->delimiter, '"', '\\');
|
||||
$row = array_map(static fn(?string $v): string => trim($v ?? ''), $row);
|
||||
|
||||
// Zeile mit Header-Keys kombinieren
|
||||
// Combine row with header keys
|
||||
$rowData = [];
|
||||
foreach ($headers as $index => $header) {
|
||||
$rowData[$header] = $row[$index] ?? '';
|
||||
@@ -128,17 +128,17 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Spalten-Header zurück
|
||||
* Returns the column headers
|
||||
*
|
||||
* @return array Array mit Spalten-Namen
|
||||
* @throws \RuntimeException wenn Header-Zeile nicht gefunden
|
||||
* @return array Array of column names
|
||||
* @throws \RuntimeException if header line is not found
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
$lines = $this->readLines();
|
||||
|
||||
if ($this->headerLine > count($lines)) {
|
||||
throw new \RuntimeException("Header-Zeile {$this->headerLine} nicht gefunden");
|
||||
throw new \RuntimeException("Header line {$this->headerLine} not found");
|
||||
}
|
||||
|
||||
$headerLineContent = $lines[$this->headerLine - 1];
|
||||
@@ -148,10 +148,10 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Entfernt UTF-8 BOM (Byte Order Mark) von String
|
||||
* Removes UTF-8 BOM (Byte Order Mark) from string
|
||||
*
|
||||
* @param string $text String mit potenziellem BOM
|
||||
* @return string String ohne BOM
|
||||
* @param string $text String with potential BOM
|
||||
* @return string String without BOM
|
||||
*/
|
||||
private function removeBom(string $text): string
|
||||
{
|
||||
@@ -162,9 +162,9 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Gesamtzahl der Zeilen in der Datei zurück
|
||||
* Returns the total number of lines in the file
|
||||
*
|
||||
* @return int Anzahl Zeilen
|
||||
* @return int Number of lines
|
||||
*/
|
||||
public function countLines(): int
|
||||
{
|
||||
@@ -172,9 +172,9 @@ class CsvReader
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die Anzahl der Datenzeilen zurück (ohne Header und Metadaten)
|
||||
* Returns the number of data rows (excluding header and metadata)
|
||||
*
|
||||
* @return int Anzahl Datenzeilen
|
||||
* @return int Number of data rows
|
||||
*/
|
||||
public function countDataRows(): int
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user