public function testParsingWithExplicitHeaderRow() { /* If your CSV file doesn't have a header row */ $csv = new CSVParser($this->getCurrentRelativePath() . '/CsvBulkLoaderTest_PlayersWithHeader.csv'); $csv->provideHeaderRow(array('__fn', '__bio', '__bd', '__reg')); $firstNames = $birthdays = $biographies = $registered = array(); foreach ($csv as $record) { /* Each row in the CSV file will be keyed with the header row that you gave */ $this->assertEquals(array('__fn', '__bio', '__bd', '__reg'), array_keys($record)); $firstNames[] = $record['__fn']; $biographies[] = $record['__bio']; $birthdays[] = $record['__bd']; $registered[] = $record['__reg']; } /* And the first row will be returned in the data */ $this->assertEquals(array('FirstName', 'John', 'Jane', 'Jamie', 'Järg'), $firstNames); $this->assertEquals(array('Biography', "He's a good guy", "She is awesome." . PHP_EOL . "So awesome that she gets multiple rows and \"escaped\" strings in her biography", "Pretty old, with an escaped comma", "Unicode FTW"), $biographies); $this->assertEquals(array("Birthday", "31/01/1988", "31/01/1982", "31/01/1882", "31/06/1982"), $birthdays); $this->assertEquals(array('IsRegistered', '1', '0', '1', '1'), $registered); }
/** * @param string $filepath * @param boolean $preview * * @return BulkLoader_Result */ protected function processChunk($filepath, $preview = false) { $results = new BulkLoader_Result(); $csv = new CSVParser($filepath, $this->delimiter, $this->enclosure); // ColumnMap has two uses, depending on whether hasHeaderRow is set if ($this->columnMap) { // if the map goes to a callback, use the same key value as the map // value, rather than function name as multiple keys may use the // same callback $map = []; foreach ($this->columnMap as $k => $v) { if (strpos($v, "->") === 0) { $map[$k] = $k; } else { $map[$k] = $v; } } if ($this->hasHeaderRow) { $csv->mapColumns($map); } else { $csv->provideHeaderRow($map); } } foreach ($csv as $row) { $this->processRecord($row, $this->columnMap, $results, $preview); } return $results; }