diff --git a/src/Plugin/migrate/process/StrReplace.php b/src/Plugin/migrate/process/StrReplace.php new file mode 100644 index 0000000000000000000000000000000000000000..43d772c909341c865ce7da32f6063e2f4cc5c5b1 --- /dev/null +++ b/src/Plugin/migrate/process/StrReplace.php @@ -0,0 +1,104 @@ +<?php + +namespace Drupal\migrate_plus\Plugin\migrate\process; + +use Drupal\migrate\MigrateException; +use Drupal\migrate\MigrateExecutableInterface; +use Drupal\migrate\ProcessPluginBase; +use Drupal\migrate\Row; + +/** + * Uses the str_replace() method on a source string. + * + * @MigrateProcessPlugin( + * id = "str_replace" + * ) + * + * To do a simple hardcoded string replace use the following: + * + * @code + * field_text: + * plugin: str_replace + * source: text + * search: foo + * replace: bar + * @endcode + * + * If the value of text is "vero eos et accusam et justo vero" + * in source, foo is "et" in search and bar is "that" in replace, + * field_text will be "vero eos that accusam that justo vero". + * + * Case insensitive searches can be achieved using the following: + * @code + * field_text: + * plugin: str_replace + * case_insensitive: true + * source: text + * search: foo + * replace: bar + * @endcode + * + * If the value of text is "VERO eos et accusam et justo vero" + * in source, foo is "vero" in search and bar is "that" in replace, + * field_text will be "that eos et accusam et justo that". + * + * Also regular expressions can be matched using: + * @code + * field_text: + * plugin: str_replace + * regex: true + * source: text + * search: foo + * replace: bar + * @endcode + * + * If the value of text is "vero eos et 123 accusam et justo 123 duo" + * in source, foo is "/[0-9]{3}/" in search and bar is "the" in replace, + * field_text will be "vero eos et the accusam et justo the duo". + * + * All the rules for + * @link http://php.net/manual/function.str-replace.php str_replace @endlink + * apply. This means that you can provide arrays as values. + */ +class StrReplace extends ProcessPluginBase { + + /** + * Flag indicating whether there are multiple values. + * + * @var bool + */ + protected $multiple; + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { + if (!isset($this->configuration['search'])) { + throw new MigrateException('"search" must be configured.'); + } + if (!isset($this->configuration['replace'])) { + throw new MigrateException('"replace" must be configured.'); + } + $this->multiple = is_array($value); + $this->configuration += [ + 'case_insensitive' => FALSE, + 'regex' => FALSE, + ]; + $function = "str_replace"; + if ($this->configuration['case_insensitive']) { + $function = 'str_ireplace'; + } + if ($this->configuration['regex']) { + $function = 'preg_replace'; + } + return $function($this->configuration['search'], $this->configuration['replace'], $value); + } + + /** + * {@inheritdoc} + */ + public function multiple() { + return $this->multiple; + } + +} diff --git a/tests/src/Unit/process/StrReplaceTest.php b/tests/src/Unit/process/StrReplaceTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f1e0120b37d7f3c76c18967bcf6c97442c9d70e6 --- /dev/null +++ b/tests/src/Unit/process/StrReplaceTest.php @@ -0,0 +1,102 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Unit\process; + +use Drupal\migrate_plus\Plugin\migrate\process\StrReplace; +use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase; + + +/** + * Tests the str replace process plugin. + * + * @group migrate + * @coversDefaultClass \Drupal\migrate_plus\Plugin\migrate\process\StrReplace + */ +class StrReplaceTest extends MigrateProcessTestCase { + + /** + * Test for a simple str_replace string. + */ + public function testStrReplace() { + $value = 'vero eos et accusam et justo vero'; + $configuration['search'] = 'et'; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('vero eos that accusam that justo vero', $actual); + + } + + /** + * Test for case insensitive searches. + */ + public function testStrIreplace() { + $value = 'VERO eos et accusam et justo vero'; + $configuration['search'] = 'vero'; + $configuration['replace'] = 'that'; + $configuration['case_insensitive'] = TRUE; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('that eos et accusam et justo that', $actual); + + } + + /** + * Test for regular expressions. + */ + public function testPregReplace() { + $value = 'vero eos et 123 accusam et justo 123 duo'; + $configuration['search'] = '/[0-9]{3}/'; + $configuration['replace'] = 'the'; + $configuration['regex'] = TRUE; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertSame('vero eos et the accusam et justo the duo', $actual); + } + + /** + * Test for MigrateException for "search" configuration. + * + */ + public function testSearchMigrateException() { + $value = 'vero eos et accusam et justo vero'; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $this->setExpectedException('\Drupal\migrate\MigrateException', '"search" must be configured.'); + $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Test for MigrateException for "replace" configuration. + */ + public function testReplaceMigrateException() { + $value = 'vero eos et accusam et justo vero'; + $configuration['search'] = 'et'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $this->setExpectedException('\Drupal\migrate\MigrateException', '"replace" must be configured.'); + $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + } + + /** + * Test for multiple. + */ + public function testIsMultiple() { + $value = [ + 'vero eos et accusam et justo vero', + 'et eos vero accusam vero justo et', + ]; + + $expected = [ + 'vero eos that accusam that justo vero', + 'that eos vero accusam vero justo that', + ]; + $configuration['search'] = 'et'; + $configuration['replace'] = 'that'; + $plugin = new StrReplace($configuration, 'str_replace', []); + $actual = $plugin->transform($value, $this->migrateExecutable, $this->row, 'destinationproperty'); + $this->assertArrayEquals($expected, $actual); + + $this->assertTrue($plugin->multiple()); + } + +}