Commit 7080c759 authored by webchick's avatar webchick

Issue #2152735 by chx: Migrate process plugin: extract.

parent 8ff41086
<?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;
}
}
<?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');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment