Passed
Push — master ( c23fb3...f4d4ed )
by Oss
01:51
created

ColumnFilter::checkCompareColumnExpressions()   A

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 defined.
27
     */
28
    const FILTER_NONE = 0;
29
30
    /**
31
     * Comparison filter.
32
     */
33
    const FILTER_COMPARE_FILTER = 1;
34
35
    /**
36
     * A filter that determines whether the expression being tested is empty or not.
37
     */
38
    //const FILTER_IS_NULL_FILTER = 2;
39
40
    /**
41
     * A filter that checks if the expression being checked is in the range of expressions.
42
     */
43
    const FILTER_BETWEEN = 3;
44
45
    /**
46
     * A filter that checks whether the expression being tested is equal to one of the expressions.
47
     */
48
    //const FILTER_IN_FILTER = 4;
49
50
    /**
51
     * Filter existence by a given field.
52
     */
53
    //const FILTER_EXISTS = 5;
54
55
    /**
56
     * Filter group.
57
     */
58
    //const FILTER_FILTER_GROUP = 6;
59
60
    /* COMPARISON */
61
62
    /**
63
     * Value range.
64
     */
65
    const COMPARISON_BETWEEN = 0;
66
67
    /**
68
     * Equally.
69
     */
70
    const COMPARISON_EQUAL = 3;
71
72
    /**
73
     * Not equal.
74
     */
75
    const COMPARISON_NOT_EQUAL = 4;
76
77
    /**
78
     * Starts with an expression.
79
     */
80
    const COMPARISON_START_WITH = 9;
81
82
    /**
83
     * Does not start with an expression.
84
     */
85
    const COMPARISON_NO_START_WITH = 10;
86
87
    /**
88
     * Contains an expression.
89
     */
90
    const COMPARISON_CONTAIN = 11;
91
92
    /**
93
     * Does not contain an expression.
94
     */
95
    const COMPARISON_NOT_CONTAIN = 12;
96
97
    /**
98
     * Ends expression.
99
     */
100
    const COMPARISON_END_WITH = 13;
101
102
    /**
103
     * Does not end with an expression.
104
     */
105
    const COMPARISON_NO_END_WITH = 14;
106
107
    /**
108
     * More.
109
     */
110
    //const COMPARISON_GREATER = -1;
111
112
    /**
113
     * More or equal.
114
     */
115
    //const COMPARISON_GREATER_OR_EQUAL = -1;
116
117
    /**
118
     * Less.
119
     */
120
    //const COMPARISON_LESS = -1;
121
122
    /**
123
     * Less or equal.
124
     */
125
    //const COMPARISON_LESS_OR_EQUAL = -1;
126
127
128
129
    /**
130
     * List of supported fields.
131
     *
132
     * @var array
133
     */
134
    protected $fields = [
135
        'filterType' => null,
136
        'comparisonType' => null,
137
        'columnExpressions' => [],
138
    ];
139
140
    /**
141
     * Sets the input values.
142
     *
143
     * @param int                       $filterType         Filter type.
144
     * @param int                       $comparisonType     Comparison Type.
145
     * @param ColumnExpression|null     $_                  Column expressions.
146
     */
147 7
    public function __construct($filterType, $comparisonType, ColumnExpression $_ = null)
148
    {
149 7
        parent::__construct([
150 7
            'filterType' => $filterType,
151 7
            'comparisonType' => $comparisonType,
152 7
            'columnExpressions' => array_slice(func_get_args(), 2),
153
        ]);
154 7
    }
155
156
    /**
157
     * Returns data as an associative array.
158
     *
159
     * @return array
160
     */
161 2
    public function toArray()
162
    {
163
        $data = [
164 2
            'FilterType' => $this->getFilterType(),
165 2
            'ComparisonType' => $this->getComparisonType(),
166
        ];
167 2
        switch ($this->getFilterType()) {
168 2
            case self::FILTER_COMPARE_FILTER:
169 1
                $data['LeftExpression'] = $this->getColumnExpressions()[0]->toArray();
170 1
                $data['RightExpression'] = $this->getColumnExpressions()[1]->toArray();
171 1
                break;
172 1
            case self::FILTER_BETWEEN:
173 1
                $data['LeftExpression'] = $this->getColumnExpressions()[0]->toArray();
174 1
                $data['RightLessExpression'] = $this->getColumnExpressions()[1]->toArray();
175 1
                $data['RightGreaterExpression'] = $this->getColumnExpressions()[2]->toArray();
176 1
                break;
177
        }
178 2
        return $data;
179
    }
180
181
    /**
182
     * Validates filter data, throws an exception in case of an error.
183
     *
184
     * @throws ValidateException
185
     */
186 5
    public function validate()
187
    {
188 5
        $this->checkFilterType();
189 4
        $this->checkComparisonType();
190 3
        $this->checkCompareColumnExpressions();
191 2
        $this->checkBetweenColumnExpressions();
192 1
    }
193
194
    /**
195
     * Filter type check.
196
     *
197
     * @throws ValidateException
198
     */
199 5
    private function checkFilterType()
200
    {
201 5
        if (!in_array(
202 5
            $this->getFilterType(),
203
            [
204 5
                self::FILTER_COMPARE_FILTER,
205 5
                self::FILTER_BETWEEN
206
            ]
207
        )) {
208 1
            throw new ValidateException('Invalid filter.');
209
        }
210 4
    }
211
212
    /**
213
     * Verification of the comparison method.
214
     *
215
     * @throws ValidateException
216
     */
217 4
    private function checkComparisonType()
218
    {
219 4
        if (!in_array(
220 4
            $this->getComparisonType(),
221
            [
222 4
                self::COMPARISON_EQUAL,
223 4
                self::COMPARISON_BETWEEN
224
            ]
225
        )) {
226 1
            throw new ValidateException('Invalid filter compare arguments.');
227
        }
228 3
    }
229
230
    /**
231
     * Checking expressions for comparing values.
232
     *
233
     * @throws ValidateException
234
     */
235 3
    private function checkCompareColumnExpressions()
236
    {
237
        if (
238 3
            (self::FILTER_COMPARE_FILTER == $this->getFilterType()) &&
239 3
            (2 != count($this->getColumnExpressions()))
240
        ) {
241 1
            throw new ValidateException('Invalid filter_compare count column expressions.');
242
        }
243 2
    }
244
245
    /**
246
     * Checking expressions for between values.
247
     *
248
     * @throws ValidateException
249
     */
250 2
    private function checkBetweenColumnExpressions()
251
    {
252
        if (
253 2
            (self::FILTER_BETWEEN == $this->getFilterType()) &&
254 2
            (3 != count($this->getColumnExpressions()))
255
        ) {
256 1
            throw new ValidateException('Invalid filter_between count column expressions.');
257
        }
258 1
    }
259
}
260