This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds mutiple_value_processor plugin for datapipe
- Loading branch information
1 parent
396efb2
commit 2886a8c
Showing
5 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
modules/tide_data_pipeline/src/Plugin/DatasetTransform/MutipleValueProcessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\tide_data_pipeline\Plugin\DatasetTransform; | ||
|
||
use Drupal\data_pipelines\DatasetData; | ||
use Drupal\data_pipelines\Transform\TransformPluginBase; | ||
|
||
/** | ||
* Splits a string into multiple values and optionally processes them. | ||
* | ||
* @DatasetTransform( | ||
* id="mutiple_value_processor", | ||
* fields=TRUE, | ||
* records=FALSE | ||
* ) | ||
*/ | ||
class MutipleValueProcessor extends TransformPluginBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return parent::defaultConfiguration() + [ | ||
'separator' => '', | ||
'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 (is_string($parameter) && defined($parameter)) { | ||
return constant($parameter); | ||
} | ||
return $parameter; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...peline/tests/modules/tide_data_pipelines_test/tide_data_pipelines_test.data_pipelines.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
pipeline_with_mutiple_value_processor_transform: | ||
label: 'Mutiple Value Processor transform' | ||
transforms: | ||
field: | ||
Suburbs: | ||
- plugin: mutiple_value_processor | ||
separator: ';' | ||
callback: str_pad | ||
parameters: | ||
- 10 | ||
- '0' | ||
- STR_PAD_LEFT | ||
|
||
pipeline_with_strtoupper: | ||
label: 'Mutiple Value Processor with strtoupper' | ||
transforms: | ||
field: | ||
Suburbs: | ||
- plugin: mutiple_value_processor | ||
separator: ';' | ||
callback: strtoupper | ||
|
||
pipeline_with_mb_convert_case: | ||
label: 'Mutiple Value Processor with mb_convert_case' | ||
transforms: | ||
field: | ||
Suburbs: | ||
- plugin: mutiple_value_processor | ||
separator: ';' | ||
callback: mb_convert_case | ||
parameters: | ||
- 'MB_CASE_UPPER' | ||
|
||
pipeline_with_substr: | ||
label: 'Mutiple Value Processor with substr' | ||
transforms: | ||
field: | ||
Suburbs: | ||
- plugin: mutiple_value_processor | ||
separator: ';' | ||
callback: substr | ||
parameters: | ||
- '0' | ||
- 'true' |
10 changes: 10 additions & 0 deletions
10
...de_data_pipeline/tests/modules/tide_data_pipelines_test/tide_data_pipelines_test.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name: Data Pipelines - Test Pipelines | ||
package: Testing | ||
description: 'Provides tests data set plugins' | ||
core_version_requirement: ^10.1 | ||
php: 8.0 | ||
type: module | ||
dependencies: | ||
- data_pipelines:data_pipelines | ||
- data_pipelines_test:data_pipelines_test | ||
- tide_data_pipeline:tide_data_pipeline |
107 changes: 107 additions & 0 deletions
107
modules/tide_data_pipeline/tests/src/Kernel/Transform/TideSearchTransformTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\tide_data_pipeline\Kernel\Transform; | ||
|
||
use Drupal\data_pipelines\Entity\Dataset; | ||
use Drupal\Tests\data_pipelines\Kernel\Transform\TransformTest; | ||
|
||
/** | ||
* Defines a class for testing transform functionality. | ||
* | ||
* @group data_pipelines | ||
* | ||
* @covers \Drupal\tide_data_pipeline\Transform\MutipleValueProcessor | ||
*/ | ||
class TideSearchTransformTest extends TransformTest { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'options', | ||
'link', | ||
'file', | ||
'entity', | ||
'data_pipelines', | ||
'data_pipelines_test', | ||
'user', | ||
'system', | ||
'tide_data_pipeline', | ||
'tide_data_pipelines_test', | ||
]; | ||
|
||
/** | ||
* Test mutiple_value_processor transform. | ||
*/ | ||
public function testMutipleValueProcessorTransform(): void { | ||
$file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-mutiple_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', | ||
'csv_file' => $file, | ||
]); | ||
$data = iterator_to_array($dataset->getDataIterator()); | ||
$this->assertCount(2, $data); | ||
$this->assertEquals(['0Dandenong', 'Dandenong North'], $data[0]['Suburbs']); | ||
$this->assertEquals(['00000Boneo', '000Outtrim'], $data[1]['Suburbs']); | ||
} | ||
|
||
/** | ||
* 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'); | ||
$dataset = Dataset::create([ | ||
'source' => 'csv:file', | ||
'name' => $this->randomMachineName(), | ||
'machine_name' => mb_strtolower($this->randomMachineName()), | ||
'pipeline' => 'pipeline_with_strtoupper', | ||
'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']); | ||
} | ||
|
||
/** | ||
* 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'); | ||
$dataset = Dataset::create([ | ||
'source' => 'csv:file', | ||
'name' => $this->randomMachineName(), | ||
'machine_name' => mb_strtolower($this->randomMachineName()), | ||
'pipeline' => 'pipeline_with_mb_convert_case', | ||
'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']); | ||
} | ||
|
||
/** | ||
* Test multiple_value_processor transform with substr. | ||
*/ | ||
public function testMutipleValueProcessorTransformWithSubstr(): void { | ||
$file = $this->getTestFile(dirname(__DIR__, 2) . '/fixtures/test-pipeline-mutiple_value_processor.csv'); | ||
$dataset = Dataset::create([ | ||
'source' => 'csv:file', | ||
'name' => $this->randomMachineName(), | ||
'machine_name' => mb_strtolower($this->randomMachineName()), | ||
'pipeline' => 'pipeline_with_substr', | ||
'csv_file' => $file, | ||
]); | ||
$data = iterator_to_array($dataset->getDataIterator()); | ||
$this->assertCount(2, $data); | ||
$this->assertEquals(['D', 'D'], $data[0]['Suburbs']); | ||
$this->assertEquals(['B', 'O'], $data[1]['Suburbs']); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
modules/tide_data_pipeline/tests/src/fixtures/test-pipeline-mutiple_value_processor.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Suburbs | ||
Dandenong; Dandenong North | ||
Boneo;Outtrim |