You are here

CommercePrice.php in Commerce Migrate 3.1.x

File

modules/csv_example/src/Plugin/migrate/process/CommercePrice.php
View source
<?php

namespace Drupal\commerce_migrate_csv_example\Plugin\migrate\process;

use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;

/**
 * Creates a price array from the input value.
 *
 * Build a keyed array where price is the first value in the input array and the
 * currency code is the second. If there is no price value, an empty array is
 * returned.
 *
 * Example:
 * @code
 * price:
 *   plugin: csv_example_get_price
 *   source:
 *     - price
 *     - code
 * @endcode
 *
 * When price = 12.00 and code is 'CAD', a keyed array, where 'number' => 12.00
 * and 'currency_code => 'CAD'.
 *
 * @MigrateProcessPlugin(
 *   id = "csv_example_get_price"
 * )
 */
class CommercePrice extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (!is_array($value)) {
      throw new MigrateException(sprintf("Input should be an array, instead it was of type '%s'", gettype($value)));
    }
    $new_value = NULL;
    $number = $value[0];
    if ($number) {
      $new_value = [
        'number' => $number,
        'currency_code' => strtoupper($value[1]),
      ];
    }
    return $new_value;
  }

}

Classes

Namesort descending Description
CommercePrice Creates a price array from the input value.