You are here

public function CSVFIReader::init in Feed Import 8

Here you'll init your reader.

Overrides FeedImportReader::init

File

feed_import_base/src/CSVFIReader.php, line 18

Class

CSVFIReader
CSV Reader class, used to read csv files.

Namespace

Drupal\feed_import_base

Code

public function init() {

  // Require url.
  if (empty($this->options['url'])) {
    return FALSE;
  }

  // Set default options.
  $this->options += array(
    'length' => 0,
    'delimiter' => ',',
    'enclosure' => '"',
    'escape' => '\\',
    'use_column_names' => FALSE,
    'stream' => NULL,
  );

  // Check for stream options.
  if ($ctx = $this
    ->getStreamContext($this->options['stream'])) {

    // Open the file using stream options.
    $this->fh = fopen($this->options['url'], 'rb', FALSE, $ctx);
  }
  else {

    // Open the file.
    $this->fh = fopen($this->options['url'], 'rb');
  }
  if (!$this->fh) {
    return FALSE;
  }

  // Check to see if column names are used.
  if ($this->options['use_column_names']) {
    if ($this->columns = $this
      ->get()) {
      $this->columns = array_flip($this->columns);
    }
    else {
      return FALSE;
    }
  }
  return TRUE;
}