Пример #1
0
 /**
  * {@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;
 }
Пример #2
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testInvalidFilePath()
 {
     CsvParser::createFromFilePath('beep boop');
 }