Skip to content

Commit

Permalink
Allow _guess_delimiter to work with a single row of data
Browse files Browse the repository at this point in the history
Fix #206
  • Loading branch information
gogowitsch committed Oct 16, 2021
1 parent 009820d commit faf6231
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ protected function _check_count($char, $array, $depth, $preferred) {
}
}

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';
Expand Down
1 change: 1 addition & 0 deletions tests/example_files/single_row.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C1,C2,C3
9 changes: 9 additions & 0 deletions tests/methods/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public function testSingleColumn() {
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;
Expand Down

0 comments on commit faf6231

Please sign in to comment.