mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-14 20:16:34 +00:00
38a8330748
A follow-up to PR #4837. PR #4827, to which I am not necessarily committed, shows that there could be a use case for letting a user perform customized logic to convert a non-UTF8-encoded CSV. In addition, several other CSV Reader properties require the use of static or locale properties, which is somewhat problematic; this PR allows them to be set as instance variables, falling back to static/locale only when unset.
33 lines
1.1 KiB
PHP
33 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Csv;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
|
|
|
|
class CsvIconv2 extends Csv
|
|
{
|
|
protected function convertNonUtf8(string $filename): void
|
|
{
|
|
fclose($this->fileHandle);
|
|
$filtered = "php://filter/convert.iconv.{$this->inputEncoding}.utf-8/resource=$filename";
|
|
$sourceStream = fopen($filtered, 'rb');
|
|
if ($sourceStream === false) {
|
|
throw new ReaderException("Unable to get contents of $filename"); // @codeCoverageIgnore
|
|
}
|
|
$fileHandle = fopen('php://memory', 'r+b');
|
|
if ($fileHandle === false) {
|
|
throw new ReaderException('Unable to open php://memory'); // @codeCoverageIgnore
|
|
}
|
|
$bytes = stream_copy_to_stream($sourceStream, $fileHandle);
|
|
fclose($sourceStream);
|
|
if ($bytes === false) {
|
|
throw new ReaderException('Unable to copy stream'); // @codeCoverageIgnore
|
|
}
|
|
$this->fileHandle = $fileHandle;
|
|
$this->skipBOM();
|
|
}
|
|
}
|