Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Explode.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\MigrateExecutableInterface;
8 use Drupal\migrate\Row;
9
10 /**
11  * Splits the source string into an array of strings, using a delimiter.
12  *
13  * This plugin creates an array of strings by splitting the source parameter on
14  * boundaries formed by the delimiter.
15  *
16  * Available configuration keys:
17  * - source: The source string.
18  * - limit: (optional)
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
22  *     returned.
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.
30  *
31  * Example:
32  *
33  * @code
34  * process:
35  *   bar:
36  *     plugin: explode
37  *     source: foo
38  *     delimiter: /
39  * @endcode
40  *
41  * If foo is "node/1", then bar will be ['node', '1']. The PHP equivalent of
42  * this would be:
43  *
44  * @code
45  *   $bar = explode('/', $foo);
46  * @endcode
47  *
48  * @code
49  * process:
50  *   bar:
51  *     plugin: explode
52  *     source: foo
53  *     limit: 1
54  *     delimiter: /
55  * @endcode
56  *
57  * If foo is "node/1/edit", then bar will be ['node', '1/edit']. The PHP
58  * equivalent of this would be:
59  *
60  * @code
61  *   $bar = explode('/', $foo, 1);
62  * @endcode
63  *
64  * If the 'strict' configuration is set to FALSE, the input value is casted to a
65  * string before being spilt:
66  *
67  * @code
68  * process:
69  *   bar:
70  *     plugin: explode
71  *     source: foo
72  *     delimiter: /
73  *     strict: false
74  * @endcode
75  *
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:
78  *
79  * @code
80  *   $bar = explode('/', (string) 123);
81  *   $bar = explode('/', (string) TRUE);
82  * @endcode
83  *
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 [].
87  *
88  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
89  *
90  * @MigrateProcessPlugin(
91  *   id = "explode"
92  * )
93  */
94 class Explode extends ProcessPluginBase {
95
96   /**
97    * {@inheritdoc}
98    */
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');
102     }
103
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)));
107     }
108     elseif (!$strict) {
109       // Check if the incoming value can cast to a string.
110       $original = $value;
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)));
113       }
114       // Empty strings should be exploded to empty arrays.
115       if ($value === '') {
116         return [];
117       }
118     }
119
120     $limit = isset($this->configuration['limit']) ? $this->configuration['limit'] : PHP_INT_MAX;
121
122     return explode($this->configuration['delimiter'], $value, $limit);
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function multiple() {
129     return TRUE;
130   }
131
132 }