ldc-boutique-cnc-sable/infra/Adapters/Services/CsvGeneratorImpl.php
2026-10-27 09:58:12 +01:00

42 lines
799 B
PHP

<?php
declare(strict_types=1);
namespace Infrastructure\Adapters\Services;
use Domain\Ports\Services\CsvGenerator;
use SplTempFileObject;
class CsvGeneratorImpl implements CsvGenerator
{
/**
* @param string[] $header
* @param array<string[]> $rows
*/
public function generate(array $header, array $rows): string
{
$file = new SplTempFileObject();
$file->fputcsv(
$header,
';',
'"',
'\\'
);
foreach ($rows as $item) {
$file->fputcsv($item, ';', '"', '\\');
}
$file->rewind();
$content = '';
while (! $file->eof()) {
$content .= $file->getCurrentLine();
$file->next();
}
return $content;
}
}