You are here

public function SpreadsheetIterator::key in Migrate Spreadsheet 2.0.x

Same name and namespace in other branches
  1. 8 src/SpreadsheetIterator.php \Drupal\migrate_spreadsheet\SpreadsheetIterator::key()

File

src/SpreadsheetIterator.php, line 46

Class

SpreadsheetIterator
Provides a spreadsheet iterator.

Namespace

Drupal\migrate_spreadsheet

Code

public function key() : array {
  if (empty($keys = $this
    ->getKeys())) {

    // If no keys were passed, use the spreadsheet current row position.
    if (!$this
      ->getRowIndexColumn()) {
      throw new \RuntimeException("Row index should act as key but no name has been provided. Use SpreadsheetIterator::setRowIndexColumn() to provide a name for this column.");
    }
    return [
      $this
        ->getAbsoluteRowIndex(),
    ];
  }
  return array_values(array_map(function (string $col_letter) {
    $cell_reference = "{$col_letter}{$this->getAbsoluteRowIndex()}";
    if ($cell = $this
      ->getWorksheet()
      ->getCell($cell_reference, FALSE)) {
      return $cell
        ->getValue();
    }
    $key = array_search($col_letter, $this
      ->getKeys());
    throw new \RuntimeException("Key column '{$key}' contains a null value at {$cell_reference}.");
  }, $keys));
}