diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:43:12 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:43:12 +0000 |
commit | cd989f9c3aff968e19a3aeabc4eb9085787a6673 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/PropertyModifier/PropertyModifierTrim.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.tar.xz icingaweb2-module-director-cd989f9c3aff968e19a3aeabc4eb9085787a6673.zip |
Adding upstream version 1.10.2.upstream/1.10.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/PropertyModifier/PropertyModifierTrim.php')
-rw-r--r-- | library/Director/PropertyModifier/PropertyModifierTrim.php | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/library/Director/PropertyModifier/PropertyModifierTrim.php b/library/Director/PropertyModifier/PropertyModifierTrim.php new file mode 100644 index 0000000..64a655b --- /dev/null +++ b/library/Director/PropertyModifier/PropertyModifierTrim.php @@ -0,0 +1,54 @@ +<?php + +namespace Icinga\Module\Director\PropertyModifier; + +use Icinga\Module\Director\Hook\PropertyModifierHook; +use Icinga\Module\Director\Web\Form\QuickForm; +use InvalidArgumentException; + +class PropertyModifierTrim extends PropertyModifierHook +{ + const VALID_METHODS = ['trim', 'ltrim', 'rtrim']; + + public static function addSettingsFormFields(QuickForm $form) + { + $form->addElement('select', 'trim_method', [ + 'label' => $form->translate('Trim Method'), + 'description' => $form->translate('Where to trim the string(s)'), + 'value' => 'trim', + 'multiOptions' => $form->optionalEnum([ + 'trim' => $form->translate('Beginning and Ending'), + 'ltrim' => $form->translate('Beginning only'), + 'rtrim' => $form->translate('Ending only'), + ]), + 'required' => true, + ]); + + $form->addElement('text', 'character_mask', [ + 'label' => $form->translate('Character Mask'), + 'description' => $form->translate( + 'Specify the characters that trim should remove.' + . 'Default is: " \t\n\r\0\x0B"' + ), + ]); + } + + public function transform($value) + { + if ($value === null) { + return null; + } + + $mask = $this->getSetting('character_mask'); + $method = $this->getSetting('trim_method'); + if (in_array($method, self::VALID_METHODS)) { + if ($mask) { + return $method($value, $mask); + } else { + return $method($value); + } + } + + throw new InvalidArgumentException("'$method' is not a valid trim method"); + } +} |