summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-02-21 12:56:13 +0530
committerAlex Pott2016-02-21 12:56:13 +0530
commite740b7060f11ba21a3433099522f82f5bb26be01 (patch)
tree76defe88a6befa5784808d88d88940d967b5b87b
parent0edf8153dcf12557acd55e3790042fcfb67b03c3 (diff)
Issue #2575101 by quietone, jcnventura, jgrubb, hussainweb, chr.fritsch, heddn, benjy: Add an explode/separator process plugin
-rw-r--r--core/modules/migrate/config/schema/migrate.process.schema.yml11
-rw-r--r--core/modules/migrate/src/Plugin/migrate/process/Explode.php49
-rw-r--r--core/modules/migrate/tests/src/Unit/process/ExplodeTest.php82
3 files changed, 142 insertions, 0 deletions
diff --git a/core/modules/migrate/config/schema/migrate.process.schema.yml b/core/modules/migrate/config/schema/migrate.process.schema.yml
index 249f69d..a82ff63 100644
--- a/core/modules/migrate/config/schema/migrate.process.schema.yml
+++ b/core/modules/migrate/config/schema/migrate.process.schema.yml
@@ -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'
diff --git a/core/modules/migrate/src/Plugin/migrate/process/Explode.php b/core/modules/migrate/src/Plugin/migrate/process/Explode.php
new file mode 100644
index 0000000..2fb4d00
--- /dev/null
+++ b/core/modules/migrate/src/Plugin/migrate/process/Explode.php
@@ -0,0 +1,49 @@
+<?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;
+ }
+
+}
diff --git a/core/modules/migrate/tests/src/Unit/process/ExplodeTest.php b/core/modules/migrate/tests/src/Unit/process/ExplodeTest.php
new file mode 100644
index 0000000..5c8354c
--- /dev/null
+++ b/core/modules/migrate/tests/src/Unit/process/ExplodeTest.php
@@ -0,0 +1,82 @@
+<?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');
+ }
+
+}