annotate modules/contrib/migrate_plus/src/Plugin/migrate/process/SkipOnValue.php @ 5:12f9dff5fda9 tip

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