90dc33dc7484a7b4473038ce788581f5c5efb024
[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 class SkipOnValue extends ProcessPluginBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
25     if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
26       throw new MigrateException('Skip on value plugin is missing value configuration.');
27     }
28
29     if (is_array($this->configuration['value'])) {
30       foreach ($this->configuration['value'] as $skipValue) {
31         if ($this->compareValue($value, $skipValue, !isset($this->configuration['not_equals']))) {
32           throw new MigrateSkipRowException();
33         }
34       }
35     }
36     elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
37       throw new MigrateSkipRowException();
38     }
39
40     return $value;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
47     if (empty($this->configuration['value']) && !array_key_exists('value', $this->configuration)) {
48       throw new MigrateException('Skip on value plugin is missing value configuration.');
49     }
50
51     if (is_array($this->configuration['value'])) {
52       foreach ($this->configuration['value'] as $skipValue) {
53         if ($this->compareValue($value, $skipValue, !isset($this->configuration['not_equals']))) {
54           throw new MigrateSkipProcessException();
55         }
56       }
57     }
58     elseif ($this->compareValue($value, $this->configuration['value'], !isset($this->configuration['not_equals']))) {
59       throw new MigrateSkipProcessException();
60     }
61
62     return $value;
63   }
64
65   /**
66    * Compare values to see if they are equal.
67    *
68    * @param $value
69    *   Actual value
70    * @param $skipValue
71    *   Value to compare against.
72    * @param $equal
73    *   Compare as equal or not equal.
74    *
75    * @return bool
76    *   True if the compare successfully, FALSE otherwise.
77    */
78   protected function compareValue($value, $skipValue, $equal = TRUE) {
79     if ($equal) {
80       return (string) $value == (string) $skipValue;
81     }
82
83     return (string) $value != (string) $skipValue;
84
85   }
86
87 }