summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierRegexReplace.php
blob: 59cb245ede3d338bd29b4a2f37161dce554307a1 (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
<?php

namespace Icinga\Module\Director\PropertyModifier;

use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;

class PropertyModifierRegexReplace extends PropertyModifierHook
{
    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('text', 'pattern', array(
            'label'       => 'Regex pattern',
            'description' => $form->translate(
                'The pattern you want to search for. This can be a regular expression like /^www\d+\./'
            ),
            'required'    => true,
        ));

        $form->addElement('text', 'replacement', array(
            'label'       => 'Replacement',
            'description' => $form->translate(
                'The string that should be used as a preplacement'
            ),
        ));
    }

    public function getName()
    {
        return 'Regular expression based replacement';
    }

    public function transform($value)
    {
        if ($value === null) {
            return null;
        }

        return preg_replace(
            $this->getSetting('pattern'),
            $this->getSetting('replacement'),
            $value
        );
    }
}