From 7766bf7c3bd3bf8369b92d32791ad5b6e7b7bcd4 Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Tue, 27 Feb 2018 15:00:14 +0100 Subject: [PATCH 1/2] added two tests for reproducing the heading and offset bug. test with heading fails at the moment. if this one is green, bug would be solved --- tests/methods/ParseTest.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index 9566499..5f26a21 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -157,6 +157,31 @@ class ParseTest extends TestCase $this->assertEquals($expected, $this->csv->data_types); } + + public function testDataArrayKeysWhenSettingOffsetWithHeading() { + $this->csv->offset = 2; + $this->csv->auto(__DIR__ . '/fixtures/datatype.csv'); + $expected = [ + 'title', + 'isbn', + 'publishedAt', + 'published', + 'count', + 'price' + ]; + + $this->assertEquals($expected, array_keys($this->csv->data[0])); + } + + public function testDataArrayKeysWhenSettingOffsetWithoutHeading() { + $this->csv->heading = false; + $this->csv->offset = 2; + $this->csv->auto(__DIR__ . '/fixtures/datatype.csv'); + $expected = range(0,5, 1); + + $this->assertEquals($expected, array_keys($this->csv->data[0])); + } + protected function _get_magazines_data() { return [ [ From 11b20a31443523ee2dd3beec22f92d2b1d4c24dc Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Tue, 27 Feb 2018 15:10:55 +0100 Subject: [PATCH 2/2] fixes #42. If csv has heading and offset is set, first row will always be parsed --- src/Csv.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Csv.php b/src/Csv.php index 74381e4..a4062f6 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -987,7 +987,8 @@ class Csv { return $this->sort_by !== null || $this->offset === null || - $current_row >= $this->offset; + $current_row >= $this->offset || + ($this->heading && $current_row == 0); } /**