Skip to content
Snippets Groups Projects
Commit 3c2c48ef authored by Lucas Hedding's avatar Lucas Hedding
Browse files

Issue #2830512 by davidparedes21, Adita, Charlotte17, quiron, edysmp,...

Issue #2830512 by davidparedes21, Adita, Charlotte17, quiron, edysmp, generalredneck, rodrigoaguilera: String Replace Process plugin
parent 25244364
No related branches found
Tags 8.x-4.0-beta2
No related merge requests found
<?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;
}
}
<?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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment