You are here

public function CommerceAdjustments::transform in Commerce Migrate 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/migrate/process/CommerceAdjustments.php \Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments::transform()
  2. 3.1.x src/Plugin/migrate/process/CommerceAdjustments.php \Drupal\commerce_migrate\Plugin\migrate\process\CommerceAdjustments::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

src/Plugin/migrate/process/CommerceAdjustments.php, line 48

Class

CommerceAdjustments
Converts a nested array into Commerce Adjustments.

Namespace

Drupal\commerce_migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  if (is_array($value) && !empty($value)) {
    $adjustments = [];
    $i = 0;
    foreach ($value as $data) {
      if ($data) {
        $adjustment = [];

        // Thrown an exception if amount and currency_code are not set. Let
        // Price validate the amount.
        $not_set = [];
        foreach ([
          'amount',
          'currency_code',
        ] as $property) {
          if (!isset($data[$property])) {
            $not_set[] = $property;
          }
        }
        if ($not_set) {
          if (count($not_set) > 1) {
            throw new MigrateSkipRowException(sprintf("Properties 'amount' and 'currency_code' are not set for adjustment '%s'", var_export($data, TRUE)));
          }
          else {
            throw new MigrateSkipRowException(sprintf("Property '%s' is not set for adjustment '%s'", reset($not_set), var_export($data, TRUE)));
          }
        }

        // Skip the row if the price can't be created.
        try {
          $adjustment['amount'] = new Price(Calculator::trim($data['amount']), $data['currency_code']);
        } catch (\InvalidArgumentException $e) {
          throw new MigrateSkipRowException(sprintf('Failed creating price for adjustment %s', var_export($data, TRUE)));
        }
        $adjustment['delta'] = $i++;
        $adjustment['type'] = !empty($data['type']) ? $data['type'] : 'custom';

        // Allow the label to be in a title or label property.
        $adjustment['label'] = !empty($data['label']) ? $data['label'] : '';
        if (empty($adjustment['label'])) {
          $adjustment['label'] = !empty($data['title']) ? $data['title'] : 'Custom';
        }
        $adjustment['percentage'] = !empty($data['percentage']) ? $data['percentage'] : NULL;
        $adjustment['source_id'] = !empty($data['source_id']) ? $data['source_id'] : 'custom';
        $adjustment['included'] = !empty($data['included']) ? $data['included'] : FALSE;
        $adjustment['locked'] = !empty($data['locked']) ? $data['locked'] : TRUE;
        $adjustments[] = new Adjustment($adjustment);
      }
    }
    return $adjustments;
  }
  return $value;
}