Upgraded drupal core with security updates
[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 the PHP substr()
16  * function.
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 NULL.
21  * - length: (optional) The maximum number of characters in the returned
22  *   string. Defaults to NULL.
23  *
24  * If start is NULL 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 both start and length are 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  *
40  * If some_text_field was 'Marie SkÅ‚odowska Curie' then
41  * $destination['new_text_field'] would be 'SkÅ‚odowska'.
42  *
43  * The PHP equivalent of this is:
44  *
45  * @code
46  * $destination['new_text_field'] = substr($source['some_text_field'], 6, 10);
47  * @endcode
48  *
49  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
50  *
51  * @MigrateProcessPlugin(
52  *   id = "substr"
53  * )
54  */
55 class Substr extends ProcessPluginBase {
56
57   /**
58    * {@inheritdoc}
59    */
60   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
61     $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
62     if (!is_int($start)) {
63       throw new MigrateException('The start position configuration value should be an integer. Omit this key to capture from the beginning of the string.');
64     }
65     $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
66     if (!is_null($length) && !is_int($length)) {
67       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.');
68     }
69     if (!is_string($value)) {
70       throw new MigrateException('The input value must be a string.');
71     }
72
73     // Use optional start or length to return a portion of $value.
74     $new_value = Unicode::substr($value, $start, $length);
75     return $new_value;
76   }
77
78 }