You are here

class CSVFIReader in Feed Import 7.3

CSV Reader class, used to read csv files.

Hierarchy

Expanded class hierarchy of CSVFIReader

1 string reference to 'CSVFIReader'
feed_import_feed_import_reader_info in ./feed_import.module
Implements hook_feed_import_reader_info().

File

feed_import_base/inc/feed_import_readers.inc, line 531
This file contains implementations of feed import readers.

View source
class CSVFIReader extends FeedImportUniVectorReader {

  // File handle.
  protected $fh;

  // Column names.
  protected $columns = FALSE;

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return fgetcsv($this->fh, $this->options['length'], $this->options['delimiter'], $this->options['enclosure'], $this->options['escape']);
  }

  /**
   * {@inheritdoc}
   */
  public function formatPath($path) {
    $path = parent::formatPath($path);
    if ($this->columns) {
      foreach ($path as &$p) {
        if (isset($this->columns[$p])) {
          $p = $this->columns[$p];
        }
      }
    }
    return $path;
  }

  /**
   * {@inheritdoc}
   */
  public function __destruct() {

    // Close file handle if any.
    if ($this->fh) {
      try {
        fclose($this->fh);
      } catch (Exception $e) {
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CSVFIReader::$columns protected property
CSVFIReader::$fh protected property
CSVFIReader::formatPath public function Override this to preprocess your paths before they are used in map(). Overrides FeedImportUniVectorReader::formatPath
CSVFIReader::get public function This method returns the next available item or NULL if there are no items left. Overrides FeedImportReader::get
CSVFIReader::init public function Here you'll init your reader. Overrides FeedImportReader::init
CSVFIReader::__destruct public function Destructor. Overrides FeedImportReader::__destruct
FeedImportConfigurable::$options protected property
FeedImportConfigurable::cleanLines public static function Helper function to get lines of a string
FeedImportConfigurable::setOptions public function Sets options for this instance 4
FeedImportReader::$items protected property
FeedImportReader::getStreamContext public function Returns a stream context
FeedImportReader::__construct final public function Constructor of reader. Constructor is final but you'll have to implement init() to init your reader.
FeedImportUniVectorReader::map public function Returns a value mapped from obj by path. Overrides FeedImportReader::map