122 lines
3.2 KiB
PHP
122 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace UbsCsvTransformer;
|
|
|
|
/**
|
|
* Schreibt transformierte Daten in CSV-Datei
|
|
*
|
|
* Diese Klasse schreibt die transformierten Daten in eine
|
|
* Firefly III-kompatible CSV-Datei.
|
|
*/
|
|
class CsvWriter
|
|
{
|
|
private string $outputFile;
|
|
private string $delimiter;
|
|
|
|
/**
|
|
* @param string $outputFile Pfad zur Output-Datei
|
|
* @param array $csvStructure CSV-Struktur aus Konfiguration
|
|
*/
|
|
public function __construct(string $outputFile, array $csvStructure = [])
|
|
{
|
|
$this->outputFile = $outputFile;
|
|
$this->delimiter = $csvStructure['outputDelimiter'] ?? ',';
|
|
}
|
|
|
|
/**
|
|
* Schreibt Daten in CSV-Datei
|
|
*
|
|
* @param array $data Array von assoziativen Arrays (Zeilen)
|
|
* @throws \RuntimeException wenn Datei nicht geschrieben werden kann
|
|
*/
|
|
public function write(array $data): void
|
|
{
|
|
if (empty($data)) {
|
|
throw new \RuntimeException("Keine Daten zum Schreiben");
|
|
}
|
|
|
|
// Output-Verzeichnis erstellen falls nicht vorhanden
|
|
$dir = dirname($this->outputFile);
|
|
if (!is_dir($dir)) {
|
|
if (!mkdir($dir, 0755, true)) {
|
|
throw new \RuntimeException("Konnte Output-Verzeichnis nicht erstellen: {$dir}");
|
|
}
|
|
}
|
|
|
|
$fp = fopen($this->outputFile, 'w');
|
|
|
|
if ($fp === false) {
|
|
throw new \RuntimeException("Konnte Output-Datei nicht erstellen: {$this->outputFile}");
|
|
}
|
|
|
|
try {
|
|
// Headers schreiben (Spalten-Namen aus erster Zeile)
|
|
$headers = array_keys($data[0]);
|
|
$this->writeCsvLine($fp, $headers);
|
|
|
|
// Datenzeilen schreiben
|
|
foreach ($data as $row) {
|
|
// Sicherstellen dass alle Spalten vorhanden sind
|
|
$values = [];
|
|
foreach ($headers as $header) {
|
|
$values[] = $row[$header] ?? '';
|
|
}
|
|
|
|
$this->writeCsvLine($fp, $values);
|
|
}
|
|
} finally {
|
|
fclose($fp);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Schreibt eine CSV-Zeile mit fputcsv
|
|
*
|
|
* @param resource $fp File-Handle
|
|
* @param array $values Array mit Werten
|
|
* @throws \RuntimeException wenn Schreiben fehlschlägt
|
|
*/
|
|
private function writeCsvLine($fp, array $values): void
|
|
{
|
|
$result = fputcsv($fp, $values, $this->delimiter, '"', '\\');
|
|
|
|
if ($result === false) {
|
|
throw new \RuntimeException("Fehler beim Schreiben der CSV-Zeile");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gibt den Pfad zur Output-Datei zurück
|
|
*
|
|
* @return string Output-Dateipfad
|
|
*/
|
|
public function getOutputFile(): string
|
|
{
|
|
return $this->outputFile;
|
|
}
|
|
|
|
/**
|
|
* Prüft ob Output-Datei erstellt wurde
|
|
*
|
|
* @return bool True wenn Datei existiert
|
|
*/
|
|
public function fileExists(): bool
|
|
{
|
|
return file_exists($this->outputFile);
|
|
}
|
|
|
|
/**
|
|
* Gibt die Größe der Output-Datei zurück
|
|
*
|
|
* @return int|false Dateigröße in Bytes oder false bei Fehler
|
|
*/
|
|
public function getFileSize(): int|false
|
|
{
|
|
if (!$this->fileExists()) {
|
|
return false;
|
|
}
|
|
|
|
return filesize($this->outputFile);
|
|
}
|
|
}
|