CSV.php 4.09 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
/**
 * @file
 * Contains \Drupal\migrate_source_csv\Plugin\migrate\source\CSV.
 */

namespace Drupal\migrate_source_csv\Plugin\migrate\source;

9
use Drupal\migrate\Plugin\MigrationInterface;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate_source_csv\CSVFileObject;

/**
 * Source for CSV.
 *
 * If the CSV file contains non-ASCII characters, make sure it includes a
 * UTF BOM (Byte Order Marker) so they are interpreted correctly.
 *
 * @MigrateSource(
 *   id = "csv"
 * )
 */
class CSV extends SourcePluginBase {

  /**
   * List of available source fields.
   *
   * Keys are the field machine names as used in field mappings, values are
   * descriptions.
   *
   * @var array
   */
34
  protected $fields = [];
35 36 37 38 39 40

  /**
   * List of key fields, as indexes.
   *
   * @var array
   */
41
  protected $keys = [];
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56
  /**
   * The file class to read the file.
   *
   * @var string
   */
  protected $fileClass = '';

  /**
   * The file object that reads the CSV file.
   *
   * @var \SplFileObject
   */
  protected $file = NULL;

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);

    // Path is required.
    if (empty($this->configuration['path'])) {
      throw new MigrateException('You must declare the "path" to the source CSV file in your source settings.');
    }

    // Key field(s) are required.
    if (empty($this->configuration['keys'])) {
      throw new MigrateException('You must declare "keys" as a unique array of fields in your source settings.');
    }

73
    $this->fileClass = empty($configuration['file_class']) ? CSVFileObject::class : $configuration['file_class'];
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  }

  /**
   * Return a string representing the source query.
   *
   * @return string
   *   The file path.
   */
  public function __toString() {
    return $this->configuration['path'];
  }

  /**
   * {@inheritdoc}
   */
  public function initializeIterator() {
    // File handler using header-rows-respecting extension of SPLFileObject.
91
    $this->file = new $this->fileClass($this->configuration['path']);
92 93 94 95 96

    // Set basics of CSV behavior based on configuration.
    $delimiter = !empty($this->configuration['delimiter']) ? $this->configuration['delimiter'] : ',';
    $enclosure = !empty($this->configuration['enclosure']) ? $this->configuration['enclosure'] : '"';
    $escape = !empty($this->configuration['escape']) ? $this->configuration['escape'] : '\\';
97
    $this->file->setCsvControl($delimiter, $enclosure, $escape);
98 99 100 101

    // Figure out what CSV column(s) to use. Use either the header row(s) or
    // explicitly provided column name(s).
    if (!empty($this->configuration['header_row_count'])) {
102
      $this->file->setHeaderRowCount($this->configuration['header_row_count']);
103 104

      // Find the last header line.
105 106
      $this->file->rewind();
      $this->file->seek($this->file->getHeaderRowCount() - 1);
107

108
      $row = $this->file->current();
109 110
      foreach ($row as $header) {
        $header = trim($header);
111
        $column_names[] = [$header => $header];
112
      }
113
      $this->file->setColumnNames($column_names);
114 115 116
    }
    // An explicit list of column name(s) will override any header row(s).
    if (!empty($this->configuration['column_names'])) {
117
      $this->file->setColumnNames($this->configuration['column_names']);
118 119
    }

120
    return $this->file;
121 122 123 124 125 126
  }

  /**
   * {@inheritdoc}
   */
  public function getIDs() {
127
    $ids = [];
128 129 130 131 132 133 134 135 136 137
    foreach ($this->configuration['keys'] as $key) {
      $ids[$key]['type'] = 'string';
    }
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
138
    $fields = [];
139
    foreach ($this->getIterator()->getColumnNames() as $column) {
140
      $fields[key($column)] = reset($column);
141 142 143 144 145 146 147 148 149 150 151 152
    }

    // Any caller-specified fields with the same names as extracted fields will
    // override them; any others will be added.
    if (!empty($this->configuration['fields'])) {
      $fields = $this->configuration['fields'] + $fields;
    }

    return $fields;
  }

}