Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Fixes a typo issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Jul 22, 2024
1 parent 2886a8c commit 9cebc0b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -26,6 +26,8 @@ public function defaultConfiguration() {
'separator' => '',
'callback' => NULL,
'parameters' => [],
// Default to first argument.
'value_position' => 0,
];
}

Expand All @@ -38,16 +40,19 @@ 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 !== '';
}));

// 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
/**
* Defines a class for testing transform functionality.
*
* @coversDefaultClass \Drupal\tide_data_pipeline\Transform\MultiValueProcessor
* @group data_pipelines
*
* @covers \Drupal\tide_data_pipeline\Transform\MutipleValueProcessor
*/
class TideSearchTransformTest extends TransformTest {

Expand All @@ -35,13 +34,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());
Expand All @@ -53,8 +52,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(),
Expand All @@ -71,8 +70,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(),
Expand All @@ -89,8 +88,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(),
Expand All @@ -104,4 +103,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']);
}

}

0 comments on commit 9cebc0b

Please sign in to comment.