summaryrefslogtreecommitdiffstats
path: root/library/Director/PropertyModifier/PropertyModifierArrayFilter.php
blob: a8fcbf71a3425f4c8356bc4a886623ca45b0bfb2 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php

namespace Icinga\Module\Director\PropertyModifier;

use Icinga\Data\Filter\FilterExpression;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\InvalidPropertyException;
use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Web\Form\QuickForm;

class PropertyModifierArrayFilter extends PropertyModifierHook
{
    /** @var FilterExpression */
    private $filterExpression;

    public function getName()
    {
        return 'Filter Array Values';
    }

    public function hasArraySupport()
    {
        return true;
    }

    public static function addSettingsFormFields(QuickForm $form)
    {
        $form->addElement('select', 'filter_method', array(
            'label'       => $form->translate('Filter method'),
            'required'    => true,
            'value'        => 'wildcard',
            'multiOptions' => $form->optionalEnum(array(
                'wildcard' => $form->translate('Simple match with wildcards (*)'),
                'regex'    => $form->translate('Regular Expression'),
            )),
        ));

        $form->addElement('text', 'filter_string', array(
            'label'       => 'Filter',
            'description' => $form->translate(
                'The string/pattern you want to search for. Depends on the'
                . ' chosen method, use www.* or *linux* for wildcard matches'
                . ' and expression like /^www\d+\./ in case you opted for a'
                . ' regular expression'
            ),
            'required'    => true,
        ));

        $form->addElement('select', 'policy', array(
            'label'       => $form->translate('Policy'),
            'required'    => true,
            'description' => $form->translate(
                'What should happen with matching elements?'
            ),
            'value'        => 'keep',
            'multiOptions' => array(
                'keep'   => $form->translate('Keep matching elements'),
                'reject' => $form->translate('Reject matching elements'),
            ),
        ));

        $form->addElement('select', 'when_empty', array(
            'label'       => $form->translate('When empty'),
            'required'    => true,
            'description' => $form->translate(
                'What should happen when the result array is empty?'
            ),
            'value'        => 'empty_array',
            'multiOptions' => $form->optionalEnum(array(
                'empty_array' => $form->translate('return an empty array'),
                'null'        => $form->translate('return NULL'),
            ))
        ));
    }

    public function matchesRegexp($string, $expression)
    {
        return preg_match($expression, $string);
    }

    public function matchesWildcard($string, $expression)
    {
        return $this->filterExpression->matches(
            (object) array('value' => $string)
        );
    }

    public function transform($value)
    {
        if (empty($value)) {
            return $this->emptyValue();
        }

        if (is_string($value)) {
            $value = [$value];
        }

        if (! is_array($value)) {
            throw new InvalidPropertyException(
                'The ArrayFilter property modifier be applied to arrays only'
            );
        }

        $method = $this->getSetting('filter_method');
        $filter = $this->getSetting('filter_string');
        $policy = $this->getSetting('policy');

        switch ($method) {
            case 'wildcard':
                $func = 'matchesWildcard';
                $this->filterExpression = new FilterExpression('value', '=', $filter);
                break;
            case 'regex':
                $func = 'matchesRegexp';
                break;
            default:
                throw new ConfigurationError(
                    '%s is not a valid value for an ArrayFilter filter_method',
                    var_export($method, true)
                );
        }

        $result = array();

        foreach ($value as $val) {
            if ($this->$func($val, $filter)) {
                if ($policy === 'keep') {
                    $result[] = $val;
                }
            } else {
                if ($policy === 'reject') {
                    $result[] = $val;
                }
            }
        }

        if (empty($result)) {
            return $this->emptyValue();
        }

        return $result;
    }

    protected function emptyValue()
    {
        if ($this->getSetting('when_empty', 'empty_array') === 'empty_array') {
            return array();
        } else {
            return null;
        }
    }
}