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