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 79ec01d
Showing
5 changed files
with
151 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 (defined($parameter)) { | ||
return constant($parameter); | ||
} | ||
return $parameter; | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
...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,12 @@ | ||
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 |
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 |
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
<?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']); | ||
} | ||
} |
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 |