diff --git a/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php b/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php new file mode 100644 index 0000000..dfb9a73 --- /dev/null +++ b/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php @@ -0,0 +1,76 @@ + '', + 'callback' => NULL, + 'parameters' => [], + ]; + } + + /** + * {@inheritdoc} + */ + protected function doTransformField(string $field_name, DatasetData $record): DatasetData { + $record = parent::doTransformRecord($record); + if ($record->offsetExists($field_name) && !empty($record[$field_name])) { + $separator = $this->configuration['separator']; + $callback = $this->configuration['callback']; + $parameters = $this->configuration['parameters']; + $parts = explode($separator, $record[$field_name]); + + $cleaned_parts = array_values(array_filter(array_map('trim', $parts), function($part) { + return $part !== ''; + })); + + // Process the parts if a callback is provided. + if (is_callable($callback)) { + $cleaned_parts = array_map(function($value) use ($callback, $parameters) { + $typed_parameters = array_map([$this, 'convertParameter'], $parameters); + return call_user_func_array($callback, array_merge([$value], $typed_parameters)); + }, $cleaned_parts); + } + + $record[$field_name] = $cleaned_parts; + } + return $record; + } + + /** + * Converts a parameter to its appropriate type. + */ + private function convertParameter($parameter) { + if (is_numeric($parameter)) { + return $parameter + 0; + } + if ($parameter === 'true' || $parameter === 'false') { + return $parameter === 'true'; + } + if (defined($parameter)) { + return constant($parameter); + } + return $parameter; + } + +}