summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierTrim.php
diff options
context:
space:
mode:
Diffstat (limited to 'library/Director/PropertyModifier/PropertyModifierTrim.php')
-rw-r--r--library/Director/PropertyModifier/PropertyModifierTrim.php54
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");
+ }
+}