/** * @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 processProlog() { $testArray = [ ]; $canRead = $this->setCsvReader($this->file); if ( ! $canRead) { return false; } $workflow = new Workflow($this->reader); $output = new ConsoleOutput(); // Don’t import the non-metadata $filter = new Filter\CallbackFilter(function ($row) { $i = trim($row['reg_id']); return ! empty( $i ); }); /** @var $filter Filter\CallbackFilter */ $workflow->addFilter($filter); $workflow->addItemConverter($this->mapping); $workflow->addWriter(new Writer\ConsoleProgressWriter($output, $this->reader)); $workflow->addWriter(new Writer\CallbackWriter(function ($row) { $this->setPrologColumns(); //if this is meta store it in the array $meta = mb_strtolower(trim($row['reg_id'])); switch ($meta) { case "uri": case "lang": case "type": foreach ($row as $column => $value) { $this->prolog['columns'][$column][$meta] = $value; if ('uri' != $meta) { $this->prolog['defaults'][$meta] = $row[$this->prolog['key_column']]; } } break; case "meta": case "prefix": $this->prolog[$meta][$row[$this->prolog['key_column']]] = $row[$this->prolog['value_column']]; break; default: } //if there's an error store it in the error array //if the error is fatal throw an exception })); //$workflow->addWriter(new Writer\ArrayWriter($testArray)); $this->processPrologResults = $workflow->process(); //add the token and the base_domain to the prefixes if ( ! is_array($this->prolog['meta']['token'])) { if ( ! array_key_exists($this->prolog['meta']['token'], $this->prolog['prefix'])) { $this->prolog['prefix'][$this->prolog['meta']['token']] = $this->prolog['meta']['base_domain']; } } if (isset( $this->prolog['meta']['status_id'] )) { $this->status = $this->prolog['meta']['status_id']; } if (isset( $this->prolog['meta']['user_id'] )) { $this->userId = $this->prolog['meta']['user_id']; } //use the prolog to configure namespaces, look up correct resources in the database $this->getDataColumnIds(); //store the row number of the first non-meta line $this->prologRowCount = $this->reader->count(); return $this->prolog; }
public function testVaryingElementCountWithoutColumnHeadersNotStrict() { $file = new \SplFileObject(__DIR__ . '/../Fixtures/data_no_column_headers_varying_element_count.csv'); $csvReader = new CsvReader($file); $csvReader->setStrict(false); $csvReader->setColumnHeaders(array('id', 'number', 'description')); $this->assertEquals(5, $csvReader->count()); $this->assertFalse($csvReader->hasErrors()); }