summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierSubstring.php
blob: 37a2f927060c615026f8f31eeb60b3aef3e5fcc9 (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;

class PropertyModifierSubstring extends PropertyModifierHook
{
    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('text', 'start', array(
            'label'       => 'Start',
            'required'    => true,
            'description' => sprintf(
                $form->translate(
                    'Please see %s for detailled instructions of how start and end work'
                ),
                'http://php.net/manual/en/function.substr.php'
            )
        ));

        $form->addElement('text', 'length', array(
            'label'    => 'End',
            'description' => sprintf(
                $form->translate(
                    'Please see %s for detailled instructions of how start and end work'
                ),
                'http://php.net/manual/en/function.substr.php'
            )
        ));
    }

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

        $length = $this->getSetting('length');
        if (is_numeric($length)) {
            return substr(
                $value,
                (int) $this->getSetting('start'),
                (int) $length
            );
        } else {
            return substr(
                $value,
                (int) $this->getSetting('start')
            );
        }
    }
}