summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierStripDomain.php
blob: 34fb6bafb1d43b260e96fd099dd1f4fb1c919c1a (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
<?php

namespace Icinga\Module\Director\PropertyModifier;

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

class PropertyModifierStripDomain extends PropertyModifierHook
{
    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('text', 'domain', array(
            'label'       => 'Domain name',
            'description' => $form->translate('The domain name you want to be stripped'),
            'required'    => true,
        ));
    }

    public function getName()
    {
        return 'Strip a domain name';
    }

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

        $domain = preg_quote(ltrim($this->getSetting('domain'), '.'), '/');

        return preg_replace(
            '/\.' . $domain . '$/',
            '',
            $value
        );
    }
}