/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result) { $feed_config = $feed->getConfigurationFor($this); $state = $feed->getState(StateInterface::PARSE); // Load and configure parser. $parser = new ParserCSV(); $delimiter = $feed_config['delimiter'] == 'TAB' ? "\t" : $feed_config['delimiter']; $parser->setDelimiter($delimiter); $iterator = new ParserCSVIterator($fetcher_result->getFilePath()); if (empty($feed_config['no_headers'])) { // Get first line and use it for column names, convert them to lower case. $header = $this->parseHeader($parser, $iterator); if (!$header) { return; } $parser->setColumnNames($header); } // Determine section to parse, parse. $start = $state->pointer ? $state->pointer : $parser->lastLinePos(); $limit = $this->importer->getLimit(); $rows = $this->parseItems($parser, $iterator, $start, $limit); // Report progress. $state->total = filesize($fetcher_result->getFilePath()); $state->pointer = $parser->lastLinePos(); $progress = $parser->lastLinePos() ? $parser->lastLinePos() : $state->total; $state->progress($state->total, $progress); // Create a result object and return it. return new ParserResult($rows, $feed->id()); }
/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) { $feed_config = $feed->getConfigurationFor($this); if (!filesize($fetcher_result->getFilePath())) { throw new EmptyFeedException(); } // Load and configure parser. $parser = CsvFileParser::createFromFilePath($fetcher_result->getFilePath())->setDelimiter($feed_config['delimiter'] === 'TAB' ? "\t" : $feed_config['delimiter'])->setHasHeader(!$feed_config['no_headers'])->setStartByte((int) $state->pointer); // Wrap parser in a limit iterator. $parser = new \LimitIterator($parser, 0, $this->configuration['line_limit']); $header = !$feed_config['no_headers'] ? $parser->getHeader() : []; $result = new ParserResult(); foreach ($parser as $row) { $item = new DynamicItem(); foreach ($row as $delta => $cell) { $key = isset($header[$delta]) ? $header[$delta] : $delta; $item->set($key, $cell); } $result->addItem($item); } // Report progress. $state->total = filesize($fetcher_result->getFilePath()); $state->pointer = $parser->lastLinePos(); $state->progress($state->total, $state->pointer); return $result; }