Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / Substr.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\process;
4
5 use Drupal\migrate\ProcessPluginBase;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\Row;
8 use Drupal\migrate\MigrateException;
9 use Drupal\Component\Utility\Unicode;
10
11 /**
12  * Returns a substring of the input value.
13  *
14  * The substr process plugin returns the portion of the input value specified by
15  * the start and length parameters. This is a wrapper around
16  * \Drupal\Component\Utility\Unicode::substr().
17  *
18  * Available configuration keys:
19  * - start: (optional) The returned string will start this many characters after
20  *   the beginning of the string, defaults to 0.
21  * - length: (optional) The maximum number of characters in the returned
22  *   string, defaults to NULL.
23  *
24  * If start is 0 and length is an integer, the start position is the
25  * beginning of the string. If start is an integer and length is NULL, the
26  * substring starting from the start position until the end of the string will
27  * be returned. If start is 0 and length is NULL the entire string is returned.
28  *
29  * Example:
30  *
31  * @code
32  * process:
33  *   new_text_field:
34  *     plugin: substr
35  *     source: some_text_field
36  *     start: 6
37  *     length: 10
38  * @endcode
39  * If some_text_field was 'Marie SkÅ‚odowska Curie' then
40  * $destination['new_text_field'] would be 'SkÅ‚odowska'.
41  *
42  * The PHP equivalent of this is:
43  * @code
44  * $destination['new_text_field'] = substr($source['some_text_field'], 6, 10);
45  * @endcode
46  *
47  * The substr plugin requires that the source value is not empty. If empty
48  * values are expected, combine skip_on_empty process plugin to the pipeline:
49  * @code
50  * process:
51  *   new_text_field:
52  *    -
53  *      plugin: skip_on_empty
54  *      method: process
55  *      source: some_text_field
56  *    -
57  *      plugin: substr
58  *      source: some_text_field
59  *      start: 6
60  *      length: 10
61  * @endcode
62  *
63  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
64  *
65  * @MigrateProcessPlugin(
66  *   id = "substr"
67  * )
68  */
69 class Substr extends ProcessPluginBase {
70
71   /**
72    * {@inheritdoc}
73    */
74   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
75     $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
76     if (!is_int($start)) {
77       throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
78     }
79     $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
80     if (!is_null($length) && !is_int($length)) {
81       throw new MigrateException('The character length configuration value should be an integer. Omit this key to capture from the start position to the end of the string.');
82     }
83     if (!is_string($value)) {
84       throw new MigrateException('The input value must be a string.');
85     }
86
87     // Use optional start or length to return a portion of $value.
88     $new_value = Unicode::substr($value, $start, $length);
89     return $new_value;
90   }
91
92 }