From 5d4643b201c0717a635a2ad000f2378ae263b72f Mon Sep 17 00:00:00 2001 From: Fonata Date: Sat, 16 Oct 2021 18:28:19 +0200 Subject: [PATCH] Allow _guess_delimiter to work with a single row of data Fix #206 --- src/Csv.php | 2 +- tests/example_files/single_row.csv | 1 + tests/methods/ParseTest.php | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 tests/example_files/single_row.csv diff --git a/src/Csv.php b/src/Csv.php index a9a4841..98b21e9 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -1261,7 +1261,7 @@ class Csv { } } - if ($equal) { + if ($equal || $depth === 1) { $match = $almost ? 2 : 1; $pref = strpos($preferred, $char); $pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999'; diff --git a/tests/example_files/single_row.csv b/tests/example_files/single_row.csv new file mode 100644 index 0000000..8c9c3c6 --- /dev/null +++ b/tests/example_files/single_row.csv @@ -0,0 +1 @@ +C1,C2,C3 diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 7eb2ddf..22c8655 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -121,6 +121,15 @@ class ParseTest extends TestCase { self::assertEquals($expected, $this->csv->data); } + public function testSingleRow() { + $this->csv->auto(__DIR__ . '/../example_files/single_row.csv'); + self::assertEquals([], $this->csv->data, 'Single row is detected as header'); + $this->csv->heading = false; + $this->csv->auto(__DIR__ . '/../example_files/single_row.csv'); + $expected = [['C1', 'C2', 'C3']]; + self::assertEquals($expected, $this->csv->data); + } + public function testMatomoData() { // Matomo (Piwik) export cannot be read with $this->csv->use_mb_convert_encoding = true;