Trim: new PropertyModifier

fixes #1660
This commit is contained in:
Thomas Gelf 2020-11-18 20:28:05 +01:00
parent 29258aab31
commit f94c20084c
3 changed files with 53 additions and 0 deletions

View File

@ -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)

View File

@ -0,0 +1,50 @@
<?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('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");
}
}

View File

@ -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,