diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:47 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:47 +0000 |
commit | 5419d4428c86c488a43124f85e5407d7cbae6541 (patch) | |
tree | 772c4221a20fd7d1b3e7e67c6e21755a50e80fd7 /library/Director/PropertyModifier/PropertyModifierRegexReplace.php | |
parent | Adding upstream version 1.10.2. (diff) | |
download | icingaweb2-module-director-upstream.tar.xz icingaweb2-module-director-upstream.zip |
Adding upstream version 1.11.1.upstream/1.11.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/PropertyModifier/PropertyModifierRegexReplace.php')
-rw-r--r-- | library/Director/PropertyModifier/PropertyModifierRegexReplace.php | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/library/Director/PropertyModifier/PropertyModifierRegexReplace.php b/library/Director/PropertyModifier/PropertyModifierRegexReplace.php index 59cb245..eeae69e 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; } } |