42 lines
799 B
PHP
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;
|
|
}
|
|
}
|