You are here

public function SpreadsheetIterator::getHeaders in Migrate Spreadsheet 8

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

Retrieves a full list of headers.

Return value

string[] An associative array having the header name as key and header column index as value. If there is no header row defined, the key is the same as the value. The column index has a letter representation (A, B, C, ...).

Throws

\RuntimeException If a header cell is duplicated.

Overrides SpreadsheetIteratorInterface::getHeaders

3 calls to SpreadsheetIterator::getHeaders()
SpreadsheetIterator::getColumns in src/SpreadsheetIterator.php
Gets the list of columns.
SpreadsheetIterator::getKeys in src/SpreadsheetIterator.php
Gets the list of columns that are composing the primary key.
SpreadsheetIterator::getRowIndexColumn in src/SpreadsheetIterator.php
Gets the name of the row index column.

File

src/SpreadsheetIterator.php, line 251

Class

SpreadsheetIterator
Provides a spreadsheet iterator.

Namespace

Drupal\migrate_spreadsheet

Code

public function getHeaders() {
  if (!isset($this->cache['headers'])) {

    // Get the first column index (one based, A is 1).
    $first_col_index = Coordinate::columnIndexFromString(Coordinate::coordinateFromString($this
      ->getOrigin())[0]);
    for ($col_index = $first_col_index; $col_index <= $this
      ->getColumnsCount(); ++$col_index) {
      $col_letter = Coordinate::stringFromColumnIndex($col_index);
      if ($header_row = $this
        ->getHeaderRow()) {
        $value = '';
        if ($cell = $this
          ->getWorksheet()
          ->getCell("{$col_letter}{$header_row}", FALSE)) {
          $value = trim($cell
            ->getValue());
          if (isset($this->cache['headers'][$value])) {
            throw new \RuntimeException("Table header '{$value}' is duplicated.");
          }
        }
      }
      else {
        $value = $col_letter;
      }
      if (!empty($value)) {

        // Only non-empty cells can act as header.
        $this->cache['headers'][$value] = $col_letter;
      }
    }
  }
  return $this->cache['headers'];
}