2b033bebc3a9b5274415db8e69f26f84c83c7560
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / process / StrReplace.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\ProcessPluginBase;
8 use Drupal\migrate\Row;
9
10 /**
11  * Uses the str_replace() method on a source string.
12  *
13  * @MigrateProcessPlugin(
14  *   id = "str_replace"
15  * )
16  *
17  * To do a simple hardcoded string replace use the following:
18  *
19  * @code
20  * field_text:
21  *   plugin: str_replace
22  *   source: text
23  *   search: foo
24  *   replace: bar
25  * @endcode
26  *
27  * If the value of text is "vero eos et accusam et justo vero" in source, foo is
28  * "et" in search and bar is "that" in replace, field_text will be "vero eos
29  * that accusam that justo vero".
30  *
31  * Case insensitive searches can be achieved using the following:
32  * @code
33  * field_text:
34  *   plugin: str_replace
35  *   case_insensitive: true
36  *   source: text
37  *   search: foo
38  *   replace: bar
39  * @endcode
40  *
41  * If the value of text is "VERO eos et accusam et justo vero" in source, foo is
42  * "vero" in search and bar is "that" in replace, field_text will be "that eos
43  * et accusam et justo that".
44  *
45  * Also regular expressions can be matched using:
46  * @code
47  * field_text:
48  *   plugin: str_replace
49  *   regex: true
50  *   source: text
51  *   search: foo
52  *   replace: bar
53  * @endcode
54  *
55  * If the value of text is "vero eos et 123 accusam et justo 123 duo" in source,
56  * foo is "/[0-9]{3}/" in search and bar is "the" in replace, field_text will be
57  * "vero eos et the accusam et justo the duo".
58  *
59  * All the rules for
60  * @link http://php.net/manual/function.str-replace.php str_replace @endlink
61  * apply. This means that you can provide arrays as values.
62  */
63 class StrReplace extends ProcessPluginBase {
64
65   /**
66    * Flag indicating whether there are multiple values.
67    *
68    * @var bool
69    */
70   protected $multiple;
71
72   /**
73    * {@inheritdoc}
74    */
75   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
76     if (!isset($this->configuration['search'])) {
77       throw new MigrateException('"search" must be configured.');
78     }
79     if (!isset($this->configuration['replace'])) {
80       throw new MigrateException('"replace" must be configured.');
81     }
82     $this->multiple = is_array($value);
83     $this->configuration += [
84       'case_insensitive' => FALSE,
85       'regex' => FALSE,
86     ];
87     $function = "str_replace";
88     if ($this->configuration['case_insensitive']) {
89       $function = 'str_ireplace';
90     }
91     if ($this->configuration['regex']) {
92       $function = 'preg_replace';
93     }
94     return $function($this->configuration['search'], $this->configuration['replace'], $value);
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function multiple() {
101     return $this->multiple;
102   }
103
104 }