CSVFIReader.php in Feed Import 8
File
feed_import_base/src/CSVFIReader.php
View source
<?php
namespace Drupal\feed_import_base;
class CSVFIReader extends FeedImportUniVectorReader {
protected $fh;
protected $columns = FALSE;
public function init() {
if (empty($this->options['url'])) {
return FALSE;
}
$this->options += array(
'length' => 0,
'delimiter' => ',',
'enclosure' => '"',
'escape' => '\\',
'use_column_names' => FALSE,
'stream' => NULL,
);
if ($ctx = $this
->getStreamContext($this->options['stream'])) {
$this->fh = fopen($this->options['url'], 'rb', FALSE, $ctx);
}
else {
$this->fh = fopen($this->options['url'], 'rb');
}
if (!$this->fh) {
return FALSE;
}
if ($this->options['use_column_names']) {
if ($this->columns = $this
->get()) {
$this->columns = array_flip($this->columns);
}
else {
return FALSE;
}
}
return TRUE;
}
public function get() {
return fgetcsv($this->fh, $this->options['length'], $this->options['delimiter'], $this->options['enclosure'], $this->options['escape']);
}
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;
}
public function __destruct() {
if ($this->fh) {
try {
fclose($this->fh);
} catch (Exception $e) {
}
}
}
}