summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/web/src/Filter/Renderer.php
blob: 513470ecc360b0a41dcadf9b34e86f84b6993434 (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
<?php

namespace ipl\Web\Filter;

use ipl\Stdlib\Filter;

class Renderer
{
    /** @var Filter\Rule */
    protected $filter;

    /** @var string */
    protected $string;

    /** @var bool Whether strict mode is enabled */
    protected $strict = false;

    /**
     * Create a new filter Renderer
     *
     * @param Filter\Rule $filter
     */
    public function __construct(Filter\Rule $filter)
    {
        $this->filter = $filter;
    }

    /**
     * Set whether strict mode is enabled
     *
     * @param bool $strict
     *
     * @return $this
     */
    public function setStrict($strict = true)
    {
        $this->strict = (bool) $strict;

        return $this;
    }

    /**
     * Assemble and return the filter as query string
     *
     * @return string
     */
    public function render()
    {
        if ($this->string !== null) {
            return $this->string;
        }

        $this->string = '';
        $filter = $this->filter;

        if ($filter instanceof Filter\Chain) {
            $this->renderChain($filter, $this->strict);
        } else {
            /** @var Filter\Condition $filter */
            $this->renderCondition($filter);
        }

        return $this->string;
    }

    /**
     * Assemble the given filter Chain
     *
     * @param Filter\Chain $chain
     * @param bool $wrap
     *
     * @return void
     */
    protected function renderChain(Filter\Chain $chain, $wrap = false)
    {
        if (! $this->strict && $chain->isEmpty()) {
            return;
        }

        $chainOperator = null;
        switch (true) {
            case $chain instanceof Filter\All:
                $chainOperator = '&';
                break;
            case $chain instanceof Filter\None:
                $this->string .= '!';

                // Force wrap, it may be the root node
                if (! $wrap) {
                    if ($chain->count() > 1) {
                        $wrap = true;
                    } else {
                        $iterator = $chain->getIterator();
                        $wrap = $iterator->current() instanceof Filter\None;
                    }
                }

                // None shares the operator with Any
            case $chain instanceof Filter\Any:
                $chainOperator = '|';
                break;
        }

        if ($wrap) {
            $this->string .= '(';
        }

        foreach ($chain as $rule) {
            if ($rule instanceof Filter\Chain) {
                $this->renderChain($rule, $this->strict || $rule->count() > 1);
            } else {
                /** @var Filter\Condition $rule */
                $this->renderCondition($rule);
            }

            $this->string .= $chainOperator;
        }

        if (! $chain->isEmpty() && (! $this->strict || ! ($chain instanceof Filter\Any && $chain->count() === 1))) {
            // Remove redundant chain operator added last
            $this->string = substr($this->string, 0, -1);
        } elseif ($chain->isEmpty() && $chain instanceof Filter\Any) {
            // If the chain is empty and strict mode is on, we need a
            // chain operator to designate it's an OR, not an AND
            $this->string .= $chainOperator;
        }

        if ($wrap) {
            $this->string .= ')';
        }
    }

    /**
     * Assemble the given filter Condition
     *
     * @param Filter\Condition $condition
     *
     * @return void
     */
    protected function renderCondition(Filter\Condition $condition)
    {
        $value = $condition->getValue();
        if (is_bool($value) && ! $value) {
            $this->string .= '!';
        }

        $this->string .= rawurlencode($condition->getColumn());

        if (is_bool($value)) {
            return;
        }

        switch (true) {
            case $condition instanceof Filter\Unlike:
                $this->string .= '!~';
                break;
            case $condition instanceof Filter\Unequal:
                $this->string .= '!=';
                break;
            case $condition instanceof Filter\Like:
                $this->string .= '~';
                break;
            case $condition instanceof Filter\Equal:
                $this->string .= '=';
                break;
            case $condition instanceof Filter\GreaterThan:
                $this->string .= rawurlencode('>');
                break;
            case $condition instanceof Filter\LessThan:
                $this->string .= rawurlencode('<');
                break;
            case $condition instanceof Filter\GreaterThanOrEqual:
                $this->string .= rawurlencode('>') . '=';
                break;
            case $condition instanceof Filter\LessThanOrEqual:
                $this->string .= rawurlencode('<') . '=';
                break;
        }

        if (is_array($value)) {
            $this->string .= '(' . join('|', array_map('rawurlencode', $value)) . ')';
        } elseif ($value !== null) {
            $this->string .= rawurlencode($value);
        }
    }
}