You are here

MigrateSourceYaml.inc in Migrate Source YAML 7

Contains MigrateSourceYaml.

File

includes/MigrateSourceYaml.inc
View source
<?php

/**
 * @file
 * Contains MigrateSourceYaml.
 */
use Symfony\Component\Yaml\Parser;

/**
 * Migrate source class to import from a YAML file.
 */
class MigrateSourceYaml extends MigrateSource {

  /**
   * Construct a new YAML source object.
   *
   * @param string $path
   *   The system path to the YAML file.
   * @param string $list
   *   The name of the list to import from. This list must be at the base of the
   *   YAML file.
   * @param array $fields
   *   Optionally provide an array of fields where each key is the field name
   *   (as found in each item of the YAML list being imported) and the value
   *   being a description of the field. Use this to customise the description
   *   of any fields or to specify additional fields to be added to in
   *   prepareRow(). This may additionally be used where the first row of data
   *   doesn't contain all fields as this plugin only extracts names from the
   *   first row of data.
   * @param array $options
   *   Optionally provide options as described in MigrateSource.
   */
  public function __construct($path, $list, array $fields = array(), array $options = array()) {
    parent::__construct($options);
    $this->file = $path;
    $this->list = $list;
    $this->yamlParser = new Parser();
    $this
      ->parse();
    $this->fields = $fields;
    foreach (reset($this->data) as $key => $field) {
      if (!isset($this->fields[$key])) {
        $this->fields[$key] = ucfirst($key);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return $this->fields;
  }

  /**
   * Return a count of all available source records.
   *
   * @return int
   *   The number of source records.
   */
  public function computeCount() {
    return count($this->data);
  }

  /**
   * Perform a rewind, setting the current record back to the first item.
   *
   * Use PHP native array functionality, setting the internal pointer of the
   * data array to the first item.
   */
  public function performRewind() {
    reset($this->data);
  }

  /**
   * Get the next record.
   *
   * Use PHP native array functionality to return the item that the internal
   * array pointer is currently at and then advance the pointer to the next
   * record.
   *
   * @return NULL|object
   *   NULL if the last item has been reached otherwise an object representing
   *   the current row.
   */
  public function getNextRow() {
    if ($item = current($this->data)) {

      // Advance the internal pointer of the array.
      next($this->data);

      // Cast the item from an array into an StdClass object.
      return (object) $item;
    }
    return NULL;
  }

  /**
   * Parse the source YAML file.
   *
   * @throws Exception
   *   On not finding the provided list in the source data.
   */
  protected function parse() {
    $yaml = file_get_contents($this->file);
    $parsed = $this->yamlParser
      ->parse($yaml);
    if (isset($parsed[$this->list])) {
      $this->data = $parsed[$this->list];
    }
    else {
      throw new Exception('The list provided to MigrateSourceYaml does not exist in the YAML file specified.');
    }
  }

  /**
   * String representation of Migration Source.
   *
   * @return string
   *   String representation of the Migration Source file.
   */
  public function __toString() {
    return (string) $this->file;
  }

}

Classes

Namesort descending Description
MigrateSourceYaml Migrate source class to import from a YAML file.