summaryrefslogtreecommitdiffstats
path: root/library/Director/Data/AssignFilterHelper.php
blob: b0253cf09b8cce7a43db164023e783bb6fd9cd20 (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
<?php

namespace Icinga\Module\Director\Data;

use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterAnd;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Data\Filter\FilterNot;
use Icinga\Data\Filter\FilterOr;
use Icinga\Exception\NotImplementedError;

/**
 * Class ApplyFilterMatches
 *
 * A wrapper for Icinga Filter to evaluate filters against Director's objects
 */
class AssignFilterHelper
{
    /** @var Filter */
    protected $filter;

    public function __construct(Filter $filter)
    {
        $this->filter = $filter;
    }

    /**
     * @param object $object
     *
     * @return bool
     * @throws NotImplementedError
     */
    public function matches($object)
    {
        return $this->matchesPart($this->filter, $object);
    }

    /**
     * @param Filter $filter
     * @param object $object
     *
     * @return bool
     */
    public static function matchesFilter(Filter $filter, $object)
    {
        $helper = new static($filter);
        return $helper->matches($object);
    }

    /**
     * @param Filter $filter
     * @param object $object
     *
     * @return bool
     * @throws NotImplementedError
     */
    protected function matchesPart(Filter $filter, $object)
    {
        if ($filter->isChain()) {
            return $this->matchesChain($filter, $object);
        } elseif ($filter->isExpression()) {
            /** @var FilterExpression $filter */
            return $this->matchesExpression($filter, $object);
        } else {
            return $filter->matches($object);
        }
    }

    /**
     * @param Filter $filter
     * @param object $object
     *
     * @return bool
     * @throws NotImplementedError
     */
    protected function matchesChain(Filter $filter, $object)
    {
        if ($filter instanceof FilterAnd) {
            foreach ($filter->filters() as $f) {
                if (! $this->matchesPart($f, $object)) {
                    return false;
                }
            }

            return true;
        } elseif ($filter instanceof FilterOr) {
            foreach ($filter->filters() as $f) {
                if ($this->matchesPart($f, $object)) {
                    return true;
                }
            }

            return false;
        } elseif ($filter instanceof FilterNot) {
            foreach ($filter->filters() as $f) {
                if ($this->matchesPart($f, $object)) {
                    return false;
                }
            }

            return true;
        } else {
            $class = \get_class($filter);
            $parts = \preg_split('/\\\/', $class);

            throw new NotImplementedError(
                'Matching for Filter of type "%s" is not implemented',
                \end($parts)
            );
        }
    }

    /**
     * @param FilterExpression $filter
     * @param object           $object
     *
     * @return bool
     */
    protected function matchesExpression(FilterExpression $filter, $object)
    {
        $column = $filter->getColumn();
        $sign = $filter->getSign();
        $expression = $filter->getExpression();

        if ($sign === '=') {
            if ($expression === true) {
                return property_exists($object, $column) && ! empty($object->{$column});
            } elseif ($expression === false) {
                return ! property_exists($object, $column) || empty($object->{$column});
            } elseif (is_string($expression) && strpos($expression, '*') !== false) {
                if (! property_exists($object, $column) || empty($object->{$column})) {
                    return false;
                }
                $value = $object->{$column};

                $parts = array();
                foreach (preg_split('~\*~', $expression) as $part) {
                    $parts[] = preg_quote($part);
                }
                // match() is case insensitive
                $pattern = '/^' . implode('.*', $parts) . '$/i';

                if (is_array($value)) {
                    foreach ($value as $candidate) {
                        if (preg_match($pattern, $candidate)) {
                            return true;
                        }
                    }

                    return false;
                }

                return (bool) preg_match($pattern, $value);
            }
        }

        // fallback to default behavior
        return $filter->matches($object);
    }
}