Commit e740b706 authored by alexpott's avatar alexpott

Issue #2575101 by quietone, jcnventura, jgrubb, hussainweb, chr.fritsch,...

Issue #2575101 by quietone, jcnventura, jgrubb, hussainweb, chr.fritsch, heddn, benjy: Add an explode/separator process plugin
parent 0edf8153
......@@ -40,6 +40,17 @@ migrate.process.dedupe_entity:
type: integer
label: 'Length'
migrate.process.explode:
type: migrate_process
label: 'Explode process'
mapping:
delimiter:
type: string
label: 'Delimiter'
limit:
type: integer
label: 'Limit'
migrate.process.extract:
type: migrate_process
label: 'Extract process'
......
<?php
/**
* @file
* Contains \Drupal\migrate\Plugin\migrate\process\Explode.
*/
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* This plugin explodes a delimited string into an array of values.
*
* @MigrateProcessPlugin(
* id = "explode"
* )
*/
class Explode extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_string($value)) {
if (!empty($this->configuration['delimiter'])) {
$limit = isset($this->configuration['limit']) ? $this->configuration['limit'] : PHP_INT_MAX;
return explode($this->configuration['delimiter'], $value, $limit);
}
else {
throw new MigrateException('delimiter is empty');
}
}
else {
throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
}
}
/**
* {@inheritdoc}
*/
public function multiple() {
return TRUE;
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\migrate\Unit\process\ExplodeTest.
*/
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\Explode;
use Drupal\migrate\Plugin\migrate\process\Concat;
/**
* Tests the Explode process plugin.
*
* @group migrate
*/
class ExplodeTest extends MigrateProcessTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
$configuration = [
'delimiter' => ',',
];
$this->plugin = new Explode($configuration, 'map', []);
parent::setUp();
}
/**
* Test explode transform process works.
*/
public function testTransform() {
$value = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, ['foo', 'bar', 'tik']);
}
/**
* Test explode transform process works with a limit.
*/
public function testTransformLimit() {
$plugin = new Explode(['delimiter' => '_', 'limit' => 2], 'map', []);
$value = $plugin->transform('foo_bar_tik', $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($value, ['foo', 'bar_tik']);
}
/**
* Test if the explode process can be chained with a handles_multiple process.
*/
public function testChainedTransform() {
$exploded = $this->plugin->transform('foo,bar,tik', $this->migrateExecutable, $this->row, 'destinationproperty');
$concat = new Concat([], 'map', []);
$concatenated = $concat->transform($exploded, $this->migrateExecutable, $this->row, 'destinationproperty');
$this->assertSame($concatenated, 'foobartik');
}
/**
* Test explode fails properly on non-strings.
*
* @expectedException \Drupal\migrate\MigrateException
*
* @expectedExceptionMessage is not a string
*/
public function testExplodeWithNonString() {
$this->plugin->transform(['foo'], $this->migrateExecutable, $this->row, 'destinationproperty');
}
/**
* Test explode fails with empty delimiter.
*
* @expectedException \Drupal\migrate\MigrateException
*
* @expectedExceptionMessage delimiter is empty
*/
public function testExplodeWithEmptyDelimiter() {
$plugin = new Explode(['delimiter' => ''], 'map', []);
$plugin->transform('foo,bar', $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