summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Data/Filter/FilterNot.php
blob: b61f497eab2d6126b35f4ed0063e314753f16790 (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Data\Filter;

class FilterNot extends FilterChain
{
    protected $operatorName = 'NOT';

    protected $operatorSymbol = '!'; // BULLSHIT

// TODO: Max count 1 or autocreate sub-and?

    public function matches($row)
    {
        foreach ($this->filters() as $filter) {
            if ($filter->matches($row)) {
                return false;
            }
        }
        return true;
    }

    public function andFilter(Filter $filter)
    {
        return Filter::matchAll($this, $filter);
    }

    public function orFilter(Filter $filter)
    {
        return Filter::matchAny($filter);
    }

    public function toQueryString()
    {
        $parts = array();
        if (empty($this->filters)) {
            return '';
        }

        foreach ($this->filters() as $filter) {
            $parts[] = $filter->toQueryString();
        }
        if (count($parts) === 1) {
            return '!' . $parts[0];
        } else {
            return '!(' . implode('&', $parts) . ')';
        }
    }

    public function __toString()
    {
        if (count($this->filters) === 1) {
            return '! ' . $this->filters[0];
        }
        return '! (' . implode('&', $this->filters) . ')';
    }
}