diff --git a/src/Csv/Native.php b/src/Csv/Native.php index 5c46a18..a98e679 100644 --- a/src/Csv/Native.php +++ b/src/Csv/Native.php @@ -6,6 +6,7 @@ use Generator; use LeKoala\SpreadCompat\SpreadCompat; +use PhpOffice\PhpSpreadsheet\Spreadsheet; use RuntimeException; /** @@ -22,32 +23,10 @@ public function readString( string $contents, ...$opts ): Generator { - $this->configure(...$opts); - $this->configureSeparator($contents); - - // check for bom - if (strncmp($contents, self::BOM, 3) === 0) { - $contents = substr($contents, 3); - } - $separator = $this->getSeparator(); - - // parse rows and take into account enclosure and escaped parts - // we use $this->eol as a separator as a mean to split lines - /** @var array $data */ - $data = str_getcsv($contents, $this->eol, $this->enclosure, $this->escape); - - $headers = null; - foreach ($data as $line) { - $row = str_getcsv($line, $separator, $this->enclosure, $this->escape); - if ($this->assoc) { - if ($headers === null) { - $headers = $row; - continue; - } - $row = array_combine($headers, $row); - } - yield $row; - } + $temp = SpreadCompat::getMaxMemTempStream(); + fwrite($temp, $contents); + rewind($temp); + return $this->readStream($temp, ...$opts); } /** diff --git a/tests/SpreadCompatCsvTest.php b/tests/SpreadCompatCsvTest.php index b23a052..238f1ba 100644 --- a/tests/SpreadCompatCsvTest.php +++ b/tests/SpreadCompatCsvTest.php @@ -314,4 +314,21 @@ public function testSpreadsheetCanWriteCsv() $expected = file_get_contents(__DIR__ . '/data/separator.csv'); $this->assertEquals($expected, $string); } + + public function testLineSplitCsvNative() + { + $document = <<assertCount(1, $data, "Should be one line"); + + $generator = SpreadCompat::readString($document, 'csv', adapter: SpreadCompat::NATIVE); + $data = iterator_to_array($generator); + $this->assertCount(1, $data, "Should be one line"); + } }