Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / process / MakeUniqueBase.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
10 /**
11  * This plugin ensures the source value is unique.
12  *
13  * The MakeUniqueBase process plugin is used to avoid duplication at the
14  * destination. For example, when creating filter format names, the source
15  * value is checked against the existing filter format names and if it exists,
16  * a numeric postfix is added and incremented until a unique value is created.
17  * An optional postfix string can be insert before the numeric postfix.
18  *
19  * Available configuration keys
20  *   - start: (optional) The position at which to start reading.
21  *   - length: (optional) The number of characters to read.
22  *   - postfix: (optional) A string to insert before the numeric postfix.
23  *
24  * @see \Drupal\migrate\Plugin\MigrateProcessInterface
25  */
26 abstract class MakeUniqueBase extends ProcessPluginBase {
27
28   /**
29    * Creates a unique value based on the source value.
30    *
31    * @param string $value
32    *   The input string.
33    * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
34    *   The migration in which this process is being executed.
35    * @param \Drupal\migrate\Row $row
36    *   The row from the source to process.
37    * @param string $destination_property
38    *   The destination property currently worked on. This is only used together
39    *   with the $row above.
40    *
41    * @return string
42    *   The unique version of the input value.
43    *
44    * @throws \Drupal\migrate\MigrateException
45    */
46   public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
47     $i = 1;
48     $postfix = isset($this->configuration['postfix']) ? $this->configuration['postfix'] : '';
49     $start = isset($this->configuration['start']) ? $this->configuration['start'] : 0;
50     if (!is_int($start)) {
51       throw new MigrateException('The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
52     }
53     $length = isset($this->configuration['length']) ? $this->configuration['length'] : NULL;
54     if (!is_null($length) && !is_int($length)) {
55       throw new MigrateException('The character length configuration key should be an integer. Omit this key to capture the entire string.');
56     }
57     // Use optional start or length to return a portion of the unique value.
58     $value = mb_substr($value, $start, $length);
59     $new_value = $value;
60     while ($this->exists($new_value)) {
61       $new_value = $value . $postfix . $i++;
62     }
63     return $new_value;
64   }
65
66   /**
67    * This is a query checking the existence of some value.
68    *
69    * @param mixed $value
70    *   The value to check.
71    *
72    * @return bool
73    *   TRUE if the value exists.
74    */
75   abstract protected function exists($value);
76
77 }