ColumnFilter::checkCompareColumnExpressions()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
crap 3
1
<?php
2
/**
3
 * @category    Brownie/BpmOnline
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\BpmOnline\DataService\Column;
9
10
use Brownie\BpmOnline\Exception\ValidateException;
11
use Brownie\Util\StorageArray;
12
13
/**
14
 * Filter for contracts.
15
 *
16
 * @method int      getFilterType()             Returns the type of filter.
17
 * @method int      getComparisonType()         Returns the type of comparison.
18
 * @method array    getColumnExpressions()      Returns column expressions.
19
 */
20
class ColumnFilter extends StorageArray
21
{
22
23
    /* FILTER */
24
25
    /**
26
     * Filter type not set.
27
     */
28
    const FILTER_NONE = 0;
29
30
    /**
31
     * Comparison filter.
32
     */
33
    const FILTER_COMPARE = 1;
34
35
    /**
36
     * A filter that checks if the test expression is empty or not empty.
37
     */
38
    const FILTER_IS_NULL = 2;
39
40
    /**
41
     * A filter that checks if the expression to be tested falls within the range of expressions.
42
     */
43
    const FILTER_BETWEEN = 3;
44
45
    /**
46
     * A filter that checks if the test expression is equal to one of the expressions.
47
     */
48
    const FILTER_IN = 4;
49
50
    /**
51
     * The filter "Exists according to the set condition".
52
     */
53
    const FILTER_EXISTS = 5;
54
55
    /**
56
     * Filter ser.
57
     */
58
    const FILTER_FILTER_GROUP = 6;
59
60
    /* COMPARISON */
61
62
    /**
63
     * Checks if the value falls within the range of values.
64
     */
65
    const COMPARISON_BETWEEN = 0;
66
67
    /**
68
     * Checks whether the value is empty.
69
     */
70
    const COMPARISON_IS_NULL = 1;
71
72
    /**
73
     * Checks that the value is not empty.
74
     */
75
    const COMPARISON_IS_NOT_NULL = 2;
76
77
    /**
78
     * Checks for equality of values.
79
     */
80
    const COMPARISON_EQUAL = 3;
81
82
    /**
83
     * Checks for inequality of values.
84
     */
85
    const COMPARISON_NOT_EQUAL = 4;
86
87
    /**
88
     * Checks that the value is less.
89
     */
90
    const COMPARISON_LESS = 5;
91
92
    /**
93
     * Checks that the value is less than or equal to.
94
     */
95
    const COMPARISON_LESS_OR_EQUAL = 6;
96
97
    /**
98
     * Checks that the value is greater.
99
     */
100
    const COMPARISON_GREATER = 7;
101
102
    /**
103
     * Verifying that the value is greater than or equal to.
104
     */
105
    const COMPARISON_GREATER_OR_EQUAL = 8;
106
107
    /**
108
     * Checks if the value starts with the search string.
109
     */
110
    const COMPARISON_START_WITH = 9;
111
112
    /**
113
     * Checks if the value does not start with the search string.
114
     */
115
    const COMPARISON_NO_START_WITH = 10;
116
117
    /**
118
     * Checks if the value includes the search string.
119
     */
120
    const COMPARISON_CONTAIN = 11;
121
122
    /**
123
     * Checks if the value does not include the search string.
124
     */
125
    const COMPARISON_NOT_CONTAIN = 12;
126
127
    /**
128
     * Checks if the value ends with the search string.
129
     */
130
    const COMPARISON_END_WITH = 13;
131
132
    /**
133
     * Checks if the value does not end with the search string.
134
     */
135
    const COMPARISON_NO_END_WITH = 14;
136
137
    /**
138
     * Exists according to the given condition.
139
     */
140
    const COMPARISON_EXISTS = 15;
141
142
    /**
143
     * Does not exist according to the given condition.
144
     */
145
    const COMPARISON_NOT_EXISTS = 16;
146
147
    /**
148
     * List of supported fields.
149
     *
150
     * @var array
151
     */
152
    protected $fields = [
153
        'filterType' => null,
154
        'comparisonType' => null,
155
        'columnExpressions' => [],
156
    ];
157
158
    /**
159
     * Sets the input values.
160
     *
161
     * @param int           $filterType         Filter type.
162
     * @param int           $comparisonType     Comparison Type.
163
     * @param Column|null   $_                  Column expressions.
164
     */
165 8
    public function __construct($filterType, $comparisonType, Column $_ = null)
166
    {
167 8
        parent::__construct([
168 8
            'filterType' => $filterType,
169 8
            'comparisonType' => $comparisonType,
170 8
            'columnExpressions' => array_slice(func_get_args(), 2),
171
        ]);
172 8
    }
173
174
    /**
175
     * Returns data as an associative array.
176
     *
177
     * @return array
178
     */
179 3
    public function toArray()
180
    {
181
        $data = [
182 3
            'FilterType' => $this->getFilterType(),
183 3
            'ComparisonType' => $this->getComparisonType(),
184
        ];
185 3
        switch ($this->getFilterType()) {
186 3
            case self::FILTER_IN:
187 1
                $data['LeftExpression'] = $this->getColumnExpressions()[0]->toArray();
188 1
                $data['RightExpressions'] = $this->getColumnExpressions()[1]->toArray();
189 1
                break;
190 2
            case self::FILTER_COMPARE:
191 1
                $data['LeftExpression'] = $this->getColumnExpressions()[0]->toArray();
192 1
                $data['RightExpression'] = $this->getColumnExpressions()[1]->toArray();
193 1
                break;
194 1
            case self::FILTER_BETWEEN:
195 1
                $data['LeftExpression'] = $this->getColumnExpressions()[0]->toArray();
196 1
                $data['RightLessExpression'] = $this->getColumnExpressions()[1]->toArray();
197 1
                $data['RightGreaterExpression'] = $this->getColumnExpressions()[2]->toArray();
198 1
                break;
199
        }
200 3
        return $data;
201
    }
202
203
    /**
204
     * Validates filter data, throws an exception in case of an error.
205
     *
206
     * @throws ValidateException
207
     */
208 5
    public function validate()
209
    {
210 5
        $this->checkFilterType();
211 4
        $this->checkComparisonType();
212 3
        $this->checkCompareColumnExpressions();
213 2
        $this->checkBetweenColumnExpressions();
214 1
    }
215
216
    /**
217
     * Filter type check.
218
     *
219
     * @throws ValidateException
220
     */
221 5
    private function checkFilterType()
222
    {
223
        if (
224 5
            ($this->getFilterType() < self::FILTER_NONE) ||
225 5
            ($this->getFilterType() > self::FILTER_FILTER_GROUP)
226
        ) {
227 1
            throw new ValidateException('Invalid filter.');
228
        }
229 4
    }
230
231
    /**
232
     * Verification of the comparison method.
233
     *
234
     * @throws ValidateException
235
     */
236 4
    private function checkComparisonType()
237
    {
238
        if (
239 4
            ($this->getComparisonType() < self::COMPARISON_BETWEEN) ||
240 4
            ($this->getComparisonType() > self::COMPARISON_NOT_EXISTS)
241
        ) {
242 1
            throw new ValidateException('Invalid filter compare arguments.');
243
        }
244 3
    }
245
246
    /**
247
     * Checking expressions for comparing values.
248
     *
249
     * @throws ValidateException
250
     */
251 3
    private function checkCompareColumnExpressions()
252
    {
253
        if (
254 3
            (self::FILTER_BETWEEN != $this->getFilterType()) &&
255 3
            (2 != count($this->getColumnExpressions()))
256
        ) {
257 1
            throw new ValidateException('Invalid filter_compare count column expressions.');
258
        }
259 2
    }
260
261
    /**
262
     * Checking expressions for between values.
263
     *
264
     * @throws ValidateException
265
     */
266 2
    private function checkBetweenColumnExpressions()
267
    {
268
        if (
269 2
            (self::FILTER_BETWEEN == $this->getFilterType()) &&
270 2
            (3 != count($this->getColumnExpressions()))
271
        ) {
272 1
            throw new ValidateException('Invalid filter_between count column expressions.');
273
        }
274 1
    }
275
}
276