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

Commit

Permalink
adds mutiple_value_processor plugin for datapipe
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-gao committed Jul 19, 2024
1 parent 396efb2 commit 79ec01d
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 0 deletions.
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;
}

}
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
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
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']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Suburbs
Dandenong; Dandenong North
Boneo;Outtrim

0 comments on commit 79ec01d

Please sign in to comment.