You are here

public function CsvFileObject::__construct in Commerce Pricelist 8.2

Constructs a new CsvFileObject object.

Parameters

string $file_name: The filename.

bool $has_header: Whether the loaded file has a header row.

array $header_mapping: The header mapping (real_column => mapped_column).

array $csv_options: The CSV options (delimiter, enclosure, escape).

File

src/CsvFileObject.php, line 48

Class

CsvFileObject
Defines a wrapper around CSV data in a file.

Namespace

Drupal\commerce_pricelist

Code

public function __construct($file_name, $has_header = FALSE, array $header_mapping = [], array $csv_options = []) {
  parent::__construct($file_name);
  $this
    ->setFlags(self::READ_CSV | self::READ_AHEAD | self::DROP_NEW_LINE | self::SKIP_EMPTY);
  $options = array_merge([
    'delimiter' => ',',
    'enclosure' => '"',
    'escape' => '\\',
  ], $csv_options);
  $this
    ->setCsvControl($options['delimiter'], $options['enclosure'], $options['escape']);
  $this->hasHeader = $has_header;
  $this->headerMapping = $header_mapping;
  if ($this->hasHeader) {
    $this
      ->seek(0);
    $this->header = $this
      ->current();
    $this
      ->seek(1);
  }
}