/** * @param \SplFileObject $file * @param array $data * @return array */ public function getCsvHeader(\SplFileObject $file, &$data) { $csvReader = new CsvReader($file, $data['delimiter']); $csvReader->setHeaderRowNumber(0, 1); $csvReader->setStrict(false); $data['count'] = $csvReader->count(); $data['header'] = $csvReader->getColumnHeaders(); if ($csvReader->hasErrors() || count($csvReader->getFields()) <= 1) { $this->setError('error read file'); } else { $row = $csvReader->getRow(1); foreach ($row as $key => $value) { $data['first'][$key] = $value; } } return $data; }
public function testLastRowInvalidCsv() { $file = new \SplFileObject(__DIR__ . '/../Fixtures/data_no_column_headers_varying_element_count.csv'); $reader = new CsvReader($file); $reader->setColumnHeaders(array('id', 'number', 'description')); $this->assertTrue($reader->hasErrors()); $this->assertCount(3, $reader->getErrors()); $errors = $reader->getErrors(); $this->assertEquals(1, key($errors)); $this->assertEquals(array('6', 'strictly invalid'), current($errors)); next($errors); $this->assertEquals(3, key($errors)); $this->assertEquals(array('3', '230', 'Yet more info', 'Even more info'), current($errors)); next($errors); $this->assertEquals(4, key($errors)); $this->assertEquals(array('strictly invalid'), current($errors)); }