From 51a7b688036d72d1d19512371798663a95ec2893 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 23 Feb 2023 11:44:13 +0100 Subject: [PATCH] PropertyModifierRegexReplace: w/o match allow null fixes #2705 --- doc/82-Changelog.md | 3 ++ .../PropertyModifierRegexReplace.php | 40 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index c67055e4..74d878e2 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -19,6 +19,9 @@ This version hasn't been released yet ### Icinga Configuration * FIX: render Set Services to individual zones where required (#1589, #2356) +### Import and Sync +* FEATURE: regular expression based modifier allows explicit NULL on no match (#2705) + ### Configuration Branches * FEATURE: with this release, directorbranches v1.3 supports a "default branch" (#2688) * FEATURE: users with default branches get warnings in the main branch (#2689) diff --git a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php index 59cb245e..eeae69e8 100644 --- a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php +++ b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php @@ -9,25 +9,36 @@ class PropertyModifierRegexReplace extends PropertyModifierHook { public static function addSettingsFormFields(QuickForm $form) { - $form->addElement('text', 'pattern', array( - 'label' => 'Regex pattern', + $form->addElement('text', 'pattern', [ + 'label' => $form->translate('Regex pattern'), 'description' => $form->translate( 'The pattern you want to search for. This can be a regular expression like /^www\d+\./' ), 'required' => true, - )); + ]); - $form->addElement('text', 'replacement', array( - 'label' => 'Replacement', + $form->addElement('text', 'replacement', [ + 'label' => $form->translate('Replacement'), 'description' => $form->translate( - 'The string that should be used as a preplacement' + 'The string that should be used as a replacement' ), - )); + ]); + $form->addElement('select', 'when_not_matched', [ + 'label' => $form->translate('When not matched'), + 'description' => $form->translate( + "What should happen, if the given pattern doesn't match" + ), + 'value' => 'keep', + 'multiOptions' => [ + 'keep' => $form->translate('Keep the given string'), + 'set_null' => $form->translate('Set the value to NULL') + ] + ]); } public function getName() { - return 'Regular expression based replacement'; + return mt('director', 'Regular expression based replacement'); } public function transform($value) @@ -36,10 +47,13 @@ class PropertyModifierRegexReplace extends PropertyModifierHook return null; } - return preg_replace( - $this->getSetting('pattern'), - $this->getSetting('replacement'), - $value - ); + $result = preg_replace($this->getSetting('pattern'), $this->getSetting('replacement'), $value); + if ($result === $value && $this->getSetting('when_not_matched', 'keep') === 'set_null') { + if (!preg_match($this->getSetting('pattern'), $value)) { + return null; + } + } + + return $result; } }