diff --git a/core/modules/migrate/config/schema/migrate.process.schema.yml b/core/modules/migrate/config/schema/migrate.process.schema.yml index 249f69d8a8afb612118d4e29bb5816a1811bf573..a82ff635d2d78fc1cea32ad668fc6271303a3047 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 0000000000000000000000000000000000000000..2fb4d0039dd48ff03ea4060c809ec44bd7d951bb --- /dev/null +++ b/core/modules/migrate/src/Plugin/migrate/process/Explode.php @@ -0,0 +1,49 @@ +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 0000000000000000000000000000000000000000..5c8354cb18426918dbd14c40ea7e3f020a1f7745 --- /dev/null +++ b/core/modules/migrate/tests/src/Unit/process/ExplodeTest.php @@ -0,0 +1,82 @@ + ',', + ]; + $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'); + } + +}