From f149156e398f7c708fb551332341293223a7482a Mon Sep 17 00:00:00 2001 From: Vincent Gao Date: Thu, 18 Jul 2024 17:02:46 +1000 Subject: [PATCH] adds mutiple_value_processor plugin for datapipe --- .../MutipleValueProcessor.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php 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..a7205bb --- /dev/null +++ b/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php @@ -0,0 +1,60 @@ + '', + 'callback' => NULL, + 'parameters' => NULL, + ]; + } + + /** + * {@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) { + return $parameters !== NULL + ? call_user_func($callback, $value, $parameters) + : call_user_func($callback, $value); + }, $cleaned_parts); + } + + $record[$field_name] = $cleaned_parts; + } + return $record; + } + +}