From 7080c75945ea95aaae06f6f661591935bb5a4145 Mon Sep 17 00:00:00 2001
From: webchick <webchick@24967.no-reply.drupal.org>
Date: Wed, 18 Dec 2013 14:13:07 -0800
Subject: [PATCH] Issue #2152735 by chx: Migrate process plugin: extract.

---
 .../Plugin/migrate/process/Extract.php        | 42 ++++++++++++
 .../migrate/Tests/process/ExtractTest.php     | 68 +++++++++++++++++++
 2 files changed, 110 insertions(+)
 create mode 100644 core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php
 create mode 100644 core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php

diff --git a/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php
new file mode 100644
index 000000000000..3c825b133d39
--- /dev/null
+++ b/core/modules/migrate/lib/Drupal/migrate/Plugin/migrate/process/Extract.php
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\migrate\Plugin\migrate\process\Extract.
+ */
+
+namespace Drupal\migrate\Plugin\migrate\process;
+
+use Drupal\Component\Utility\NestedArray;
+use Drupal\migrate\ProcessPluginBase;
+use Drupal\migrate\MigrateException;
+use Drupal\migrate\MigrateExecutable;
+use Drupal\migrate\Row;
+
+/**
+ * This plugin extracts a value from an array.
+ *
+ * @see https://drupal.org/node/2152731
+ *
+ * @MigrateProcessPlugin(
+ *   id = "extract"
+ * )
+ */
+class Extract extends ProcessPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function transform($value, MigrateExecutable $migrate_executable, Row $row, $destination_property) {
+    if (!is_array($value)) {
+      throw new MigrateException('Input should be an array.');
+    }
+    $new_value = NestedArray::getValue($value, $this->configuration['index'], $key_exists);
+    if (!$key_exists) {
+      throw new MigrateException('Array index missing, extraction failed.');
+    }
+    return $new_value;
+  }
+
+}
+
diff --git a/core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php b/core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php
new file mode 100644
index 000000000000..5e1e7d999638
--- /dev/null
+++ b/core/modules/migrate/tests/Drupal/migrate/Tests/process/ExtractTest.php
@@ -0,0 +1,68 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\migrate\Tests\process\ExtractTest.
+ */
+
+namespace Drupal\migrate\Tests\process;
+
+use Drupal\migrate\Plugin\migrate\process\Extract;
+
+/**
+ * Tests the extract plugin.
+ *
+ * @see \Drupal\migrate\Plugin\migrate\process\Extract
+ * @group Drupal
+ * @group migrate
+ */
+class ExtractTest extends MigrateProcessTestCase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getInfo() {
+    return array(
+      'name' => 'Extract process plugin',
+      'description' => 'Tests the extract process plugin.',
+      'group' => 'Migrate',
+    );
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function setUp() {
+    $configuration['index'] = array('foo');
+    $this->plugin = new Extract($configuration, 'map', array());
+    parent::setUp();
+  }
+
+  /**
+   * Tests successful extraction.
+   */
+  public function testExtract() {
+    $value = $this->plugin->transform(array('foo' => 'bar'), $this->migrateExecutable, $this->row, 'destinationproperty');
+    $this->assertSame($value, 'bar');
+  }
+
+  /**
+   * Tests invalid input.
+   *
+   * @expectedException \Drupal\migrate\MigrateException
+   * @expectedExceptionMessage Input should be an array.
+   */
+  public function testExtractFromString() {
+    $this->plugin->transform('bar', $this->migrateExecutable, $this->row, 'destinationproperty');
+  }
+
+  /**
+   * Tests unsuccessful extraction.
+   *
+   * @expectedException \Drupal\migrate\MigrateException
+   * @expectedExceptionMessage Array index missing, extraction failed.
+   */
+  public function testExtractFail() {
+    $this->plugin->transform(array('bar' => 'foo'), $this->migrateExecutable, $this->row, 'destinationproperty');
+  }
+
+}
-- 
GitLab