Skip to content

Commit

Permalink
[Improvement]: Make As Numeric operator return nullable (#431)
Browse files Browse the repository at this point in the history
* Added return null option

* Adapted php doc
  • Loading branch information
mcop1 authored Oct 31, 2024
1 parent 52d22ac commit ed89486
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/Mapping/Operator/Factory/Numeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,24 @@

class Numeric extends AbstractOperator
{
private bool $returnNullIfEmpty = false;

/**
* @param mixed $inputData
* @param bool $dryRun
*
* @return float
* @return float|null
*/
public function process($inputData, bool $dryRun = false)
{
if (is_array($inputData)) {
$inputData = reset($inputData);
}

if ($this->returnNullIfEmpty && empty($inputData)) {
return null;
}

return floatval($inputData);
}

Expand Down Expand Up @@ -60,6 +66,15 @@ public function evaluateReturnType(string $inputType, int $index = null): string
*/
public function generateResultPreview($inputData)
{
if ($this->returnNullIfEmpty && empty($inputData)) {
return null;
}

return $inputData;
}

public function setSettings(array $settings): void
{
$this->returnNullIfEmpty = (bool) ($settings['returnNullIfEmpty'] ?? false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,18 @@ pimcore.plugin.pimcoreDataImporterBundle.configuration.components.mapping.operat
return "pimcore_icon_data_group_numeric";
},

getFormItems: function() {
return [
{
xtype: 'checkbox',
fieldLabel: t('plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_numeric_return_null'),
value: this.data.settings ? this.data.settings.returnNullIfEmpty : ' ',
listeners: {
change: this.inputChangePreviewUpdate.bind(this)
},
name: 'settings.returnNullIfEmpty'
}
];
}

});
3 changes: 2 additions & 1 deletion src/Resources/translations/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,5 @@ plugin_pimcore_datahub_data_importer_configpanel_logs: Import Logs
plugin_pimcore_datahub_data_importer_configpanel_mtm_relation_type_error: Type not supported for Many To Many Relation assignment.
plugin_pimcore_datahub_data_importer_configpanel_mtm_relation_type: Make sure transformation results in a 'dataObjectArray' or an 'assetArray'.
plugin_pimcore_datahub_data_importer_configpanel_type_sql: SQL
plugin_pimcore_datahub_data_importer_configpanel_sql_connection: Connection
plugin_pimcore_datahub_data_importer_configpanel_sql_connection: Connection
plugin_pimcore_datahub_data_importer_configpanel_transformation_pipeline_numeric_return_null: Return null if empty

0 comments on commit ed89486

Please sign in to comment.