Skip to content

Commit

Permalink
临时文件
Browse files Browse the repository at this point in the history
  • Loading branch information
z985342160 committed Oct 17, 2024
1 parent 0188a81 commit e50454c
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,12 @@ public function decode(string $content, array $context = []): array
{
$context = array_merge($this->defaultContext, $context);

$file = $this->writeTempFile($content);

$options = new Options();
$options->FIELD_DELIMITER = $context['delimiter'];

$stream = fopen('php://temp', 'r+');
fwrite($stream, $content);
rewind($stream);

$reader = new Reader($options);

$reader->open($stream);
$reader->open($file);

$rows = [];

Expand All @@ -40,8 +36,21 @@ public function decode(string $content, array $context = []): array
}

$reader->close();
fclose($stream);
unlink($file);

return $rows;
}

/**
* @param string $content
* @return string
*/
private function writeTempFile(string $content): string
{
$file = tempnam(sys_get_temp_dir(), 'csv_');

file_put_contents($file, $content);

return $file;
}
}

0 comments on commit e50454c

Please sign in to comment.