summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierTrim.php
blob: 64a655bdd6e48880c010786b9ee9f07726c13e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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");
    }
}