From f94c20084c86ac2e03df686ebe616827e8902f60 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Wed, 18 Nov 2020 20:28:05 +0100 Subject: [PATCH] Trim: new PropertyModifier fixes #1660 --- doc/82-Changelog.md | 1 + .../PropertyModifier/PropertyModifierTrim.php | 50 +++++++++++++++++++ register-hooks.php | 2 + 3 files changed, 53 insertions(+) create mode 100644 library/Director/PropertyModifier/PropertyModifierTrim.php diff --git a/doc/82-Changelog.md b/doc/82-Changelog.md index 78323e83..fc970009 100644 --- a/doc/82-Changelog.md +++ b/doc/82-Changelog.md @@ -29,6 +29,7 @@ next (will be 1.8.0) * FEATURE: Property Modifier: convert binary UUID to HEX presentation (#2138) * FEATURE: Property Modifier: get Host by Address (#2210) * FEATURE: Property Modifier: skip duplicates (#2215) +* FEATURE: Property Modifier: trim strings (#1660) * FEATURE: Import Sources now allow to download previewed data as JSON (#2096) * FEATURE: UTF8 validation for failed imports gives better error message (#2143) * FIX: LDAP Import is now able to paginate limited results (#2019) diff --git a/library/Director/PropertyModifier/PropertyModifierTrim.php b/library/Director/PropertyModifier/PropertyModifierTrim.php new file mode 100644 index 00000000..ed04f3f4 --- /dev/null +++ b/library/Director/PropertyModifier/PropertyModifierTrim.php @@ -0,0 +1,50 @@ +addElement('select', 'trim_method', [ + 'label' => $form->translate('Trim Method'), + 'description' => $form->translate('Please where to trim this string'), + '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) + { + $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"); + } +} diff --git a/register-hooks.php b/register-hooks.php index db393827..93c3c9ed 100644 --- a/register-hooks.php +++ b/register-hooks.php @@ -46,6 +46,7 @@ use Icinga\Module\Director\PropertyModifier\PropertyModifierSplit; use Icinga\Module\Director\PropertyModifier\PropertyModifierStripDomain; use Icinga\Module\Director\PropertyModifier\PropertyModifierSubstring; use Icinga\Module\Director\PropertyModifier\PropertyModifierToInt; +use Icinga\Module\Director\PropertyModifier\PropertyModifierTrim; use Icinga\Module\Director\PropertyModifier\PropertyModifierUppercase; use Icinga\Module\Director\PropertyModifier\PropertyModifierUpperCaseFirst; use Icinga\Module\Director\PropertyModifier\PropertyModifierURLEncode; @@ -111,6 +112,7 @@ $directorHooks = [ PropertyModifierStripDomain::class, PropertyModifierSubstring::class, PropertyModifierToInt::class, + PropertyModifierTrim::class, PropertyModifierUppercase::class, PropertyModifierUpperCaseFirst::class, PropertyModifierURLEncode::class,