diff --git a/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php b/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MultiValueProcessor.php similarity index 82% rename from modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php rename to modules/tide_data_pipeline/src/Plugin/DatasetTransform/MultiValueProcessor.php index f075f2c..1bfba5e 100644 --- a/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php +++ b/modules/tide_data_pipeline/src/Plugin/DatasetTransform/MultiValueProcessor.php @@ -11,12 +11,12 @@ * Splits a string into multiple values and optionally processes them. * * @DatasetTransform( - * id="mutiple_value_processor", + * id="multi_value_processor", * fields=TRUE, * records=FALSE * ) */ -class MutipleValueProcessor extends TransformPluginBase { +class MultiValueProcessor extends TransformPluginBase { /** * {@inheritdoc} @@ -26,6 +26,8 @@ public function defaultConfiguration() { 'separator' => '', 'callback' => NULL, 'parameters' => [], + // Default to first argument. + 'value_position' => 0, ]; } @@ -38,6 +40,7 @@ protected function doTransformField(string $field_name, DatasetData $record): Da $separator = $this->configuration['separator']; $callback = $this->configuration['callback']; $parameters = $this->configuration['parameters']; + $value_position = $this->configuration['value_position']; $parts = explode($separator, $record[$field_name]); $cleaned_parts = array_values(array_filter(array_map('trim', $parts), function ($part) { return $part !== ''; @@ -45,9 +48,11 @@ protected function doTransformField(string $field_name, DatasetData $record): Da // Process the parts if a callback is provided. if (is_callable($callback)) { - $cleaned_parts = array_map(function ($value) use ($callback, $parameters) { + $cleaned_parts = array_map(function ($value) use ($callback, $parameters, $value_position) { $typed_parameters = array_map([$this, 'convertParameter'], $parameters); - return call_user_func_array($callback, array_merge([$value], $typed_parameters)); + $args = $typed_parameters; + array_splice($args, $value_position, 0, [$value]); + return call_user_func_array($callback, $args); }, $cleaned_parts); } $record[$field_name] = $cleaned_parts; diff --git a/modules/tide_data_pipeline/tests/modules/tide_data_pipelines_test/tide_data_pipelines_test.data_pipelines.yml b/modules/tide_data_pipeline/tests/modules/tide_data_pipelines_test/tide_data_pipelines_test.data_pipelines.yml index 1d65828..e6cd821 100644 --- a/modules/tide_data_pipeline/tests/modules/tide_data_pipelines_test/tide_data_pipelines_test.data_pipelines.yml +++ b/modules/tide_data_pipeline/tests/modules/tide_data_pipelines_test/tide_data_pipelines_test.data_pipelines.yml @@ -1,9 +1,9 @@ -pipeline_with_mutiple_value_processor_transform: - label: 'Mutiple Value Processor transform' +pipeline_with_multi_value_processor_transform: + label: 'Multiple Value Processor transform' transforms: field: Suburbs: - - plugin: mutiple_value_processor + - plugin: multi_value_processor separator: ';' callback: str_pad parameters: @@ -12,33 +12,46 @@ pipeline_with_mutiple_value_processor_transform: - STR_PAD_LEFT pipeline_with_strtoupper: - label: 'Mutiple Value Processor with strtoupper' + label: 'Multiple Value Processor with strtoupper' transforms: field: Suburbs: - - plugin: mutiple_value_processor + - plugin: multi_value_processor separator: ';' callback: strtoupper pipeline_with_mb_convert_case: - label: 'Mutiple Value Processor with mb_convert_case' + label: 'Multiple Value Processor with mb_convert_case' transforms: field: Suburbs: - - plugin: mutiple_value_processor + - plugin: multi_value_processor separator: ';' callback: mb_convert_case parameters: - 'MB_CASE_UPPER' pipeline_with_substr: - label: 'Mutiple Value Processor with substr' + label: 'Multiple Value Processor with substr' transforms: field: Suburbs: - - plugin: mutiple_value_processor + - plugin: multi_value_processor separator: ';' callback: substr parameters: - '0' - 'true' + +pipeline_with_str_replace: + label: 'Multiple Value Processor with str_replace' + transforms: + field: + Suburbs: + - plugin: multi_value_processor + separator: ';' + callback: str_replace + parameters: + - 'a' + - 'A' + value_position: 2 diff --git a/modules/tide_data_pipeline/tests/src/Kernel/Transform/TideSearchTransformTest.php b/modules/tide_data_pipeline/tests/src/Kernel/Transform/TideSearchTransformTest.php index 5406d86..f5d8d1d 100644 --- a/modules/tide_data_pipeline/tests/src/Kernel/Transform/TideSearchTransformTest.php +++ b/modules/tide_data_pipeline/tests/src/Kernel/Transform/TideSearchTransformTest.php @@ -35,13 +35,13 @@ class TideSearchTransformTest extends TransformTest { /** * Test mutiple_value_processor transform. */ - public function testMutipleValueProcessorTransform(): void { - $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-mutiple_value_processor.csv'); + public function testMultipleValueProcessorTransform(): void { + $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-multiple_value_processor.csv'); $dataset = Dataset::create([ 'source' => 'csv:file', 'name' => $this->randomMachineName(), 'machine_name' => mb_strtolower($this->randomMachineName()), - 'pipeline' => 'pipeline_with_mutiple_value_processor_transform', + 'pipeline' => 'pipeline_with_multi_value_processor_transform', 'csv_file' => $file, ]); $data = iterator_to_array($dataset->getDataIterator()); @@ -53,8 +53,8 @@ public function testMutipleValueProcessorTransform(): void { /** * Test multiple_value_processor transform with strtoupper callback. */ - public function testMutipleValueProcessorTransformWithStrtoupper(): void { - $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-mutiple_value_processor.csv'); + public function testMultipleValueProcessorTransformWithStrtoupper(): void { + $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-multiple_value_processor.csv'); $dataset = Dataset::create([ 'source' => 'csv:file', 'name' => $this->randomMachineName(), @@ -71,8 +71,8 @@ public function testMutipleValueProcessorTransformWithStrtoupper(): void { /** * Test multiple_value_processor transform with mb_convert_case. */ - public function testMutipleValueProcessorTransformWithConvertCase(): void { - $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-mutiple_value_processor.csv'); + public function testMultipleValueProcessorTransformWithConvertCase(): void { + $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-multiple_value_processor.csv'); $dataset = Dataset::create([ 'source' => 'csv:file', 'name' => $this->randomMachineName(), @@ -89,8 +89,8 @@ public function testMutipleValueProcessorTransformWithConvertCase(): void { /** * Test multiple_value_processor transform with substr. */ - public function testMutipleValueProcessorTransformWithSubstr(): void { - $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-mutiple_value_processor.csv'); + public function testMultipleValueProcessorTransformWithSubstr(): void { + $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-multiple_value_processor.csv'); $dataset = Dataset::create([ 'source' => 'csv:file', 'name' => $this->randomMachineName(), @@ -104,4 +104,22 @@ public function testMutipleValueProcessorTransformWithSubstr(): void { $this->assertEquals(['B', 'O'], $data[1]['Suburbs']); } + /** + * Test multiple_value_processor transform with replace. + */ + public function testMultipleValueProcessorTransformWithReplace(): void { + $file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-multiple_value_processor.csv'); + $dataset = Dataset::create([ + 'source' => 'csv:file', + 'name' => $this->randomMachineName(), + 'machine_name' => mb_strtolower($this->randomMachineName()), + 'pipeline' => 'pipeline_with_str_replace', + 'csv_file' => $file, + ]); + $data = iterator_to_array($dataset->getDataIterator()); + $this->assertCount(2, $data); + $this->assertEquals(['DAndenong', 'DAndenong North'], $data[0]['Suburbs']); + $this->assertEquals(['Boneo', 'Outtrim'], $data[1]['Suburbs']); + } + } diff --git a/modules/tide_data_pipeline/tests/src/fixtures/test-pipeline-mutiple_value_processor.csv b/modules/tide_data_pipeline/tests/src/fixtures/test-pipeline-multiple_value_processor.csv similarity index 100% rename from modules/tide_data_pipeline/tests/src/fixtures/test-pipeline-mutiple_value_processor.csv rename to modules/tide_data_pipeline/tests/src/fixtures/test-pipeline-multiple_value_processor.csv