You are here

public function SpreadsheetIterator::current in Migrate Spreadsheet 8

Same name and namespace in other branches
  1. 2.0.x src/SpreadsheetIterator.php \Drupal\migrate_spreadsheet\SpreadsheetIterator::current()

File

src/SpreadsheetIterator.php, line 84

Class

SpreadsheetIterator
Provides a spreadsheet iterator.

Namespace

Drupal\migrate_spreadsheet

Code

public function current() {
  $keys = $this
    ->getKeys();
  $all_columns = $keys + $this
    ->getColumns();
  if ($row_index_column = $this
    ->getRowIndexColumn()) {

    // We set '@' here so that when it will be sorted, later, it will be the
    // first in the list. Ascii of '@' is lower than ascii of 'A'.
    $all_columns[$row_index_column] = '@';
  }
  elseif (empty($keys)) {
    throw new \InvalidArgumentException("Row index should act as key but no name has been provided. Pass a string in \$config['row_index_column'] key when setting the configuration in SpreadsheetIterator::setConfiguration(\$config), to provide a name for this column.");
  }

  // Arrange columns in their spreadsheet native order.
  asort($all_columns);
  return array_map(function ($col_letter) {
    if ($col_letter === '@') {
      return $this
        ->getAbsoluteRowIndex();
    }
    elseif ($cell = $this
      ->getWorksheet()
      ->getCell("{$col_letter}{$this->getAbsoluteRowIndex()}", FALSE)) {
      return $cell
        ->getCalculatedValue();
    }

    // Fall back to NULL.
    return NULL;
  }, $all_columns);
}