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 0000000000000000000000000000000000000000..3c825b133d39062246de4d85fc38e4222e4632a2 --- /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 0000000000000000000000000000000000000000..5e1e7d99963865934cce0d9eedd3139159e900e9 --- /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'); + } + +}