diff --git a/src/Iterators/CsvIterator.php b/src/Iterators/CsvIterator.php index 16b5697c..d6621e22 100644 --- a/src/Iterators/CsvIterator.php +++ b/src/Iterators/CsvIterator.php @@ -20,6 +20,9 @@ public function __construct($filePath) $this->filePath = $filePath; } + /** + * @deprecated use setColumns instead + */ public function parseColumns($columns): self { $this->columns = $columns; @@ -27,14 +30,21 @@ public function parseColumns($columns): self return $this; } + public function setColumns($columns): self + { + $this->columns = $columns; + + return $this; + } + public function rewind(): void { fclose($this->file); $this->file = fopen($this->filePath, 'r'); - - $this->currentCsvLine = fgetcsv($this->file); $this->currentRow = 0; + + $this->next(); } public function current(): array @@ -54,6 +64,12 @@ public function getGenerator(): Generator { $this->rewind(); + if (empty($this->columns)) { + $this->columns = $this->currentCsvLine; + + $this->next(); + } + while ($this->valid()) { $line = $this->current(); diff --git a/tests/CsvIteratorTest.php b/tests/CsvIteratorTest.php index 1bd191e4..4e1d5d4a 100644 --- a/tests/CsvIteratorTest.php +++ b/tests/CsvIteratorTest.php @@ -10,6 +10,7 @@ class CsvIteratorTest extends HelpersTestCase { protected CsvIterator $csvIteratorClass; protected ReflectionProperty $columnsProperty; + protected ReflectionProperty $filePathProperty; public function setUp(): void { @@ -19,6 +20,9 @@ public function setUp(): void $this->columnsProperty = new ReflectionProperty(CsvIterator::class, 'columns'); $this->columnsProperty->setAccessible(true); + + $this->filePathProperty = new ReflectionProperty(CsvIterator::class, 'filePath'); + $this->filePathProperty->setAccessible(true); } public function testOpenNotExistsFile() @@ -40,6 +44,17 @@ public function testParseColumns() $this->assertEquals($header, $actualHeader); } + public function testSetColumns() + { + $header = $this->getJsonFixture('header.json'); + + $this->csvIteratorClass->setColumns($header); + + $actualHeader = $this->columnsProperty->getValue($this->csvIteratorClass); + + $this->assertEquals($header, $actualHeader); + } + public function testCurrent() { $currentLine = $this->csvIteratorClass->current(); @@ -73,11 +88,11 @@ public function testRewind() $rowKey = $this->csvIteratorClass->key(); $currentLine = $this->csvIteratorClass->current(); - $this->assertEquals(0, $rowKey); + $this->assertEquals(1, $rowKey); $this->assertEqualsFixture('current_after_next_line.json', $currentLine); } - public function testGenerator() + public function testGeneratorWithoutSettingColumnHeaders() { $result = []; $generator = $this->csvIteratorClass->getGenerator(); @@ -88,16 +103,17 @@ public function testGenerator() $rowKey = $this->csvIteratorClass->key(); - $this->assertEquals(6, $rowKey); + $this->assertEquals(7, $rowKey); $this->assertEqualsFixture('all_data.json', $result); } - public function testGeneratorWithHeader() + public function testGeneratorWithSettingColumnHeaders() { $result = []; $header = $this->getJsonFixture('header.json'); - $this->csvIteratorClass->parseColumns($header); + $this->filePathProperty->setValue($this->csvIteratorClass, $this->getFixturePath('addresses_without_header.csv')); + $this->csvIteratorClass->setColumns($header); $generator = $this->csvIteratorClass->getGenerator(); @@ -108,7 +124,7 @@ public function testGeneratorWithHeader() $rowKey = $this->csvIteratorClass->key(); $this->assertEquals(6, $rowKey); - $this->assertEqualsFixture('all_data_with_header.json', $result); + $this->assertEqualsFixture('all_data.json', $result); } public function testGeneratorWithHeadersInvalidCount() @@ -118,7 +134,7 @@ public function testGeneratorWithHeadersInvalidCount() $header = $this->getJsonFixture('header_invalid_count.json'); - $this->csvIteratorClass->parseColumns($header); + $this->csvIteratorClass->setColumns($header); $generator = $this->csvIteratorClass->getGenerator(); diff --git a/tests/fixtures/CsvIteratorTest/addresses_without_header.csv b/tests/fixtures/CsvIteratorTest/addresses_without_header.csv new file mode 100644 index 00000000..c29646ec --- /dev/null +++ b/tests/fixtures/CsvIteratorTest/addresses_without_header.csv @@ -0,0 +1,5 @@ +John,Doe,120 jefferson st.,Riverside, NJ, 08075 +Jack,McGinnis,220 hobo Av.,Phila, PA,09119 +"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075 +Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234 +"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123 \ No newline at end of file diff --git a/tests/fixtures/CsvIteratorTest/all_data.json b/tests/fixtures/CsvIteratorTest/all_data.json index 2a12076c..c9c403cd 100644 --- a/tests/fixtures/CsvIteratorTest/all_data.json +++ b/tests/fixtures/CsvIteratorTest/all_data.json @@ -1,50 +1,42 @@ [ - [ - "First Name", - "Last Name", - "Address", - "City", - "State", - "Zip" - ], - [ - "John", - "Doe", - "120 jefferson st.", - "Riverside", - " NJ", - " 08075" - ], - [ - "Jack", - "McGinnis", - "220 hobo Av.", - "Phila", - " PA", - "09119" - ], - [ - "John \"Da Man\"", - "Repici", - "120 Jefferson St.", - "Riverside", - " NJ", - "08075" - ], - [ - "Stephen", - "Tyler", - "7452 Terrace \"At the Plaza\" road", - "SomeTown", - "SD", - " 91234" - ], - [ - "Joan \"the bone\", Anne", - "Jet", - "9th, at Terrace plc", - "Desert City", - "CO", - "00123" - ] + { + "First Name": "John", + "Last Name": "Doe", + "Address": "120 jefferson st.", + "City": "Riverside", + "State": " NJ", + "Zip": " 08075" + }, + { + "First Name": "Jack", + "Last Name": "McGinnis", + "Address": "220 hobo Av.", + "City": "Phila", + "State": " PA", + "Zip": "09119" + }, + { + "First Name": "John \"Da Man\"", + "Last Name": "Repici", + "Address": "120 Jefferson St.", + "City": "Riverside", + "State": " NJ", + "Zip": "08075" + }, + { + "First Name": "Stephen", + "Last Name": "Tyler", + "Address": "7452 Terrace \"At the Plaza\" road", + "City": "SomeTown", + "State": "SD", + "Zip": " 91234" + }, + { + "First Name": "Joan \"the bone\", Anne", + "Last Name": "Jet", + "Address": "9th, at Terrace plc", + "City": "Desert City", + "State": "CO", + "Zip": "00123" + } ] \ No newline at end of file diff --git a/tests/fixtures/CsvIteratorTest/all_data_with_header.json b/tests/fixtures/CsvIteratorTest/all_data_with_header.json deleted file mode 100644 index a4bfcce5..00000000 --- a/tests/fixtures/CsvIteratorTest/all_data_with_header.json +++ /dev/null @@ -1,50 +0,0 @@ -[ - { - "First Name": "First Name", - "Last Name": "Last Name", - "Address": "Address", - "City": "City", - "State": "State", - "Zip": "Zip" - }, - { - "First Name": "John", - "Last Name": "Doe", - "Address": "120 jefferson st.", - "City": "Riverside", - "State": " NJ", - "Zip": " 08075" - }, - { - "First Name": "Jack", - "Last Name": "McGinnis", - "Address": "220 hobo Av.", - "City": "Phila", - "State": " PA", - "Zip": "09119" - }, - { - "First Name": "John \"Da Man\"", - "Last Name": "Repici", - "Address": "120 Jefferson St.", - "City": "Riverside", - "State": " NJ", - "Zip": "08075" - }, - { - "First Name": "Stephen", - "Last Name": "Tyler", - "Address": "7452 Terrace \"At the Plaza\" road", - "City": "SomeTown", - "State": "SD", - "Zip": " 91234" - }, - { - "First Name": "Joan \"the bone\", Anne", - "Last Name": "Jet", - "Address": "9th, at Terrace plc", - "City": "Desert City", - "State": "CO", - "Zip": "00123" - } -] \ No newline at end of file