You are here

public function FeedsCSVParser::parse in Feeds 8.2

Implements FeedsParser::parse().

Overrides FeedsParser::parse

File

lib/Drupal/feeds/Plugin/feeds/parser/FeedsCSVParser.php, line 33
Contains the FeedsCSVParser class.

Class

FeedsCSVParser
Defines a CSV feed parser.

Namespace

Drupal\feeds\Plugin\feeds\parser

Code

public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  $source_config = $source
    ->getConfigFor($this);
  $state = $source
    ->state(FEEDS_PARSE);

  // Load and configure parser.
  feeds_include_library('ParserCSV.inc', 'ParserCSV');
  $parser = new ParserCSV();
  $delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
  $parser
    ->setDelimiter($delimiter);
  $iterator = new ParserCSVIterator($fetcher_result
    ->getFilePath());
  if (empty($source_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 = $source->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 FeedsParserResult($rows, $source->feed_nid);
}