Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / migrate_plus / src / Plugin / migrate / process / SkipOnValue.php
1 <?php
2
3 namespace Drupal\migrate_plus\Plugin\migrate\process;
4
5 use Drupal\migrate\MigrateException;
6 use Drupal\migrate\MigrateExecutableInterface;
7 use Drupal\migrate\MigrateSkipProcessException;
8 use Drupal\migrate\MigrateSkipRowException;
9 use Drupal\migrate\ProcessPluginBase;
10 use Drupal\migrate\Row;
11
12 /**
13  * If the source evaluates to a configured value, skip processing or whole row.
14  *
15  * @MigrateProcessPlugin(
16  *   id = "skip_on_value"
17  * )
18  *
19  * Available configuration keys:
20  * - value: An single value or array of values against which the source value
21  *   should be compared.
22  * - not_equals: (optional) If set, skipping occurs when values are not equal.
23  * - method: What to do if the input value is empty. Possible values:
24  *   - row: Skips the entire row when an empty value is encountered.
25  *   - process: Prevents further processing of the input property when the value
26  *     is empty.
27  *
28  * Examples:
29  *
30  * Example usage with minimal configuration:
31  * @code
32  *   type:
33  *     plugin: skip_on_value
34  *     source: content_type
35  *     method: row
36  *     value: blog
37  * @endcode
38  *
39  * The above example will skip processing the input property if the content_type
40  * source field equals "blog".
41  *
42  * Example usage with full configuration:
43  * @code
44  *   type:
45  *     plugin: skip_on_value
46  *     not_equals: true
47  *     source: content_type
48  *     method: row
49  *     value:
50  *       - article
51  *       - testimonial
52  * @endcode
53  *
54  * The above example will skip processing any row for which the source row's
55  * content type field is not "article" or "testimonial".
56  */
57 class SkipOnValue extends ProcessPluginBase {
58
59   /**
60    * {@inheritdoc}
61    */
62   public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
63     if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
64       throw new MigrateException('Skip on value plugin is missing value configuration.');
65     }
66
67     if (is_array($this->configuration['value'])) {
68       $value_in_array = FALSE;
69       $not_equals = isset($this->configuration['not_equals']);
70
71       foreach ($this->configuration['value'] as $skipValue) {
72         $value_in_array |= $this->compareValue($value, $skipValue);
73       }
74
75       if (($not_equals && !$value_in_array) || (!$not_equals && $value_in_array)) {
76         throw new MigrateSkipRowException();
77       }
78     }
79     elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
80       throw new MigrateSkipRowException();
81     }
82
83     return $value;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
90     if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
91       throw new MigrateException('Skip on value plugin is missing value configuration.');
92     }
93
94     if (is_array($this->configuration['value'])) {
95       $value_in_array = FALSE;
96       $not_equals = isset($this->configuration['not_equals']);
97
98       foreach ($this->configuration['value'] as $skipValue) {
99         $value_in_array |= $this->compareValue($value, $skipValue);
100       }
101
102       if (($not_equals && !$value_in_array) || (!$not_equals && $value_in_array)) {
103         throw new MigrateSkipProcessException();
104       }
105     }
106     elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
107       throw new MigrateSkipProcessException();
108     }
109
110     return $value;
111   }
112
113   /**
114    * Compare values to see if they are equal.
115    *
116    * @param mixed $value
117    *   Actual value.
118    * @param mixed $skipValue
119    *   Value to compare against.
120    * @param bool $equal
121    *   Compare as equal or not equal.
122    *
123    * @return bool
124    *   True if the compare successfully, FALSE otherwise.
125    */
126   protected function compareValue($value, $skipValue, $equal = TRUE) {
127     if ($equal) {
128       return (string) $value == (string) $skipValue;
129     }
130
131     return (string) $value != (string) $skipValue;
132
133   }
134
135 }