summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Data/Filter/Filter.php
blob: f5d8bdf139da491dec3b1c29982fbcbd7b670d7f (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Data\Filter;

use Icinga\Web\UrlParams;
use Icinga\Exception\ProgrammingError;

/**
 * Filter
 *
 * Base class for filters (why?) and factory for the different FilterOperators
 */
abstract class Filter
{
    protected $id = '1';

    public function setId($id)
    {
        $this->id = (string) $id;
        return $this;
    }

    abstract public function isExpression();

    abstract public function isChain();

    abstract public function isEmpty();

    abstract public function toQueryString();

    abstract public function andFilter(Filter $filter);

    abstract public function orFilter(Filter $filter);

    /**
     * Whether the give row matches this Filter
     *
     * @param mixed $row Preferrably an stdClass instance
     * @return bool
     */
    abstract public function matches($row);

    public function getUrlParams()
    {
        return UrlParams::fromQueryString($this->toQueryString());
    }

    public function getById($id)
    {
        if ((string) $id === $this->getId()) {
            return $this;
        }
        throw new ProgrammingError(
            'Trying to get invalid filter index "%s" from "%s" ("%s")',
            $id,
            $this,
            $this->id
        );
    }

    public function getId()
    {
        return $this->id;
    }

    public function isRootNode()
    {
        return false === strpos($this->id, '-');
    }

    abstract public function listFilteredColumns();

    public function applyChanges($changes)
    {
        $filter = $this;
        $pairs = array();
        foreach ($changes as $k => $v) {
            if (preg_match('/^(column|value|sign|operator)_([\d-]+)$/', $k, $m)) {
                $pairs[$m[2]][$m[1]] = $v;
            }
        }
        $operators = array();
        foreach ($pairs as $id => $fs) {
            if (array_key_exists('operator', $fs)) {
                $operators[$id] = $fs['operator'];
            } else {
                $f = $filter->getById($id);
                $f->setColumn($fs['column']);
                if ($f->getSign() !== $fs['sign']) {
                    if ($f->isRootNode()) {
                        $filter = $f->setSign($fs['sign']);
                    } else {
                        $filter->replaceById($id, $f->setSign($fs['sign']));
                    }
                }
                $f->setExpression($fs['value']);
            }
        }

        krsort($operators, SORT_NATURAL);
        foreach ($operators as $id => $operator) {
            $f = $filter->getById($id);
            if ($f->getOperatorName() !== $operator) {
                if ($f->isRootNode()) {
                    $filter = $f->setOperatorName($operator);
                } else {
                    $filter->replaceById($id, $f->setOperatorName($operator));
                }
            }
        }

        return $filter;
    }

    public function getParentId()
    {
        if ($this->isRootNode()) {
            throw new ProgrammingError('Filter root nodes have no parent');
        }
        return substr($this->id, 0, strrpos($this->id, '-'));
    }

    public function getParent()
    {
        return $this->getById($this->getParentId());
    }

    public function hasId($id)
    {
        if ($id === $this->getId()) {
            return true;
        }
        return false;
    }

    /**
     * Where Filter factory
     *
     * @param string $col     Column to be filtered
     * @param string $filter  Filter expression
     *
     * @throws FilterException
     * @return FilterExpression
     */
    public static function where($col, $filter)
    {
        return new FilterExpression($col, '=', $filter);
    }

    public static function expression($col, $op, $expression)
    {
        switch ($op) {
            case '=':
                return new FilterMatch($col, $op, $expression);
            case '<':
                return new FilterLessThan($col, $op, $expression);
            case '>':
                return new FilterGreaterThan($col, $op, $expression);
            case '>=':
                return new FilterEqualOrGreaterThan($col, $op, $expression);
            case '<=':
                return new FilterEqualOrLessThan($col, $op, $expression);
            case '!=':
                return new FilterMatchNot($col, $op, $expression);
            default:
                throw new ProgrammingError(
                    'There is no such filter sign: %s',
                    $op
                );
        }
    }

    /**
     * Or FilterOperator factory
     *
     * @param Filter $filter,...  Unlimited optional list of Filters
     *
     * @return FilterOr
     */
    public static function matchAny()
    {
        $args = func_get_args();
        if (count($args) === 1 && is_array($args[0])) {
            $args = $args[0];
        }
        return new FilterOr($args);
    }

    /**
     * Or FilterOperator factory
     *
     * @param Filter $filter,...  Unlimited optional list of Filters
     *
     * @return FilterAnd
     */
    public static function matchAll()
    {
        $args = func_get_args();
        if (count($args) === 1 && is_array($args[0])) {
            $args = $args[0];
        }
        return new FilterAnd($args);
    }

    /**
     * FilterNot factory, negates the given filter
     *
     * @param Filter $filter Filter to be negated
     *
     * @return FilterNot
     */
    public static function not()
    {
        $args = func_get_args();
        if (count($args) === 1) {
            if (is_array($args[0])) {
                $args = $args[0];
            }
        }
        if (count($args) > 1) {
            return new FilterNot(array(new FilterAnd($args)));
        } else {
            return new FilterNot($args);
        }
    }

    public static function chain($operator, $filters = array())
    {
        switch ($operator) {
            case 'AND':
                return self::matchAll($filters);
            case 'OR':
                return self::matchAny($filters);
            case 'NOT':
                return self::not($filters);
        }
        throw new ProgrammingError(
            '"%s" is not a valid filter chain operator',
            $operator
        );
    }

    /**
     * Create filter from queryString
     *
     * This is still pretty basic, need improvement
     *
     * @return static
     */
    public static function fromQueryString($query)
    {
        return FilterQueryString::parse($query);
    }
}