PropertyModifierArrayToRow: allow to specify...

...how to deal with empty values

refs #2192
This commit is contained in:
Thomas Gelf 2020-10-09 11:38:24 +02:00
parent 2462fbe18f
commit b961f16dfd
1 changed files with 38 additions and 2 deletions

View File

@ -3,6 +3,9 @@
namespace Icinga\Module\Director\PropertyModifier;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use InvalidArgumentException;
use ipl\Html\Error;
class PropertyModifierArrayToRow extends PropertyModifierHook
{
@ -11,6 +14,21 @@ class PropertyModifierArrayToRow extends PropertyModifierHook
return 'Clone the row for every entry of an Array';
}
public static function addSettingsFormFields(QuickForm $form)
{
$form->addElement('select', 'on_empty', [
'label' => 'When empty',
'description' => $form->translate('What should we do in case the given value is empty?'),
'multiOptions' => $form->optionalEnum([
'reject' => $form->translate('Drop the current row'),
'fail' => $form->translate('Let the whole import run fail'),
'keep' => $form->translate('Keep the row, set the column value to null'),
]),
'value' => 'reject',
'required' => true,
]);
}
public function hasArraySupport()
{
return true;
@ -24,8 +42,26 @@ class PropertyModifierArrayToRow extends PropertyModifierHook
public function transform($value)
{
if (empty($value)) {
$this->rejectRow();
return null;
$onDuplicate = $this->getSetting('on_duplicate', 'reject');
switch ($onDuplicate) {
case 'reject':
$this->rejectRow();
return null;
case 'keep':
return null;
case 'fail':
throw new InvalidArgumentException('Failed to clone row, value is empty');
default:
throw new InvalidArgumentException(
"'$onDuplicate' is not a valid 'on_duplicate' setting"
);
}
}
if (! \is_array($value)) {
throw new InvalidArgumentException(
"Array required to clone this row, got " . Error::getPhpTypeName($value)
);
}
return $value;