3 namespace Drupal\migrate\Plugin\migrate\process;
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Row;
11 * Splits the source string into an array of strings, using a delimiter.
13 * This plugin creates an array of strings by splitting the source parameter on
14 * boundaries formed by the delimiter.
16 * Available configuration keys:
17 * - source: The source string.
19 * - If limit is set and positive, the returned array will contain a maximum
20 * of limit elements with the last element containing the rest of string.
21 * - If limit is set and negative, all components except the last -limit are
23 * - If the limit parameter is zero, then this is treated as 1.
24 * - delimiter: The boundary string.
25 * - strict: (optional) When this boolean is TRUE, the source should be strictly
26 * a string. If FALSE is passed, the source value is casted to a string before
27 * being split. Also, in this case, the values casting to empty strings are
28 * converted to empty arrays, instead of an array with a single empty string
29 * item ['']. Defaults to TRUE.
41 * If foo is "node/1", then bar will be ['node', '1']. The PHP equivalent of
45 * $bar = explode('/', $foo);
57 * If foo is "node/1/edit", then bar will be ['node', '1/edit']. The PHP
58 * equivalent of this would be:
61 * $bar = explode('/', $foo, 1);
64 * If the 'strict' configuration is set to FALSE, the input value is casted to a
65 * string before being spilt:
76 * If foo is 123 (as integer), then bar will be ['123']. If foo is TRUE, then
77 * bar will be ['1']. The PHP equivalent of this would be:
80 * $bar = explode('/', (string) 123);
81 * $bar = explode('/', (string) TRUE);
84 * If the 'strict' configuration is set to FALSE, the source value casting to
85 * an empty string are converted to an empty array. For example, with the last
86 * configuration, if foo is '', NULL or FALSE, then bar will be [].
88 * @see \Drupal\migrate\Plugin\MigrateProcessInterface
90 * @MigrateProcessPlugin(
94 class Explode extends ProcessPluginBase {
99 public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
100 if (empty($this->configuration['delimiter'])) {
101 throw new MigrateException('delimiter is empty');
104 $strict = array_key_exists('strict', $this->configuration) ? $this->configuration['strict'] : TRUE;
105 if ($strict && !is_string($value)) {
106 throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
109 // Check if the incoming value can cast to a string.
111 if (!is_string($original) && ($original != ($value = @strval($value)))) {
112 throw new MigrateException(sprintf('%s cannot be casted to a string', var_export($original, TRUE)));
114 // Empty strings should be exploded to empty arrays.
120 $limit = isset($this->configuration['limit']) ? $this->configuration['limit'] : PHP_INT_MAX;
122 return explode($this->configuration['delimiter'], $value, $limit);
128 public function multiple() {