Skip to content

Commit 611cdbb

Browse files
committed
fix: CSVReader row has not same length as header
Change-Id: I407db16125c50948167e71524b37e5bcdd42d420
1 parent 002e590 commit 611cdbb

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

app/Services/Utils/CSVReader.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ public static function buildFrom(string $content):CSVReader
7171
continue;
7272
}
7373
$line = [];
74-
if(count($row) != count($header)) continue;
7574
for($i = 0; $i < count($header); $i++){
76-
$line[$header[$i]] = trim($row[$i]);
75+
$line[$header[$i]] = $row[$i] ?? '';
7776
}
7877
$lines[] = $line;
7978

@@ -146,4 +145,8 @@ public function rewind()
146145
{
147146
$this->position = 0;
148147
}
148+
149+
public function count():int{
150+
return count($this->lines);
151+
}
149152
}

tests/CSVReaderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ public function testOneColCSV()
4040
}
4141
}
4242

43+
public function testRowHasMoreColsThanHeaderCSV()
44+
{
45+
$data = <<<CSV
46+
code,class_name,quantity_available,sponsor_id,contact_email
47+
TEST_SANTI_SPONS_DIS,SPONSOR_DISCOUNT_CODE,10,,,
48+
TEST_SANTI_SPONS_PROMO,SPONSOR_PROMO_CODE,10,252,,
49+
SANTI_SPONS_PROMO1,SPONSOR_PROMO_CODE,10,,santipalenque@gmail.com
50+
SANTI_SPONS_PROMO2,SPONSOR_PROMO_CODE,10
51+
CSV;
52+
53+
$reader = CSVReader::buildFrom($data);
54+
$this->assertTrue($reader->hasColumn("contact_email"));
55+
$this->assertTrue($reader->count() == 4);
56+
}
4357
public function test3ColCSV()
4458
{
4559
$data = <<<CSV

0 commit comments

Comments
 (0)