1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Grido (https://github.com/o5/grido) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the file LICENSE.md that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Grido\Components\Filters; |
13
|
|
|
|
14
|
|
|
use Grido\Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Builds filter condition. |
18
|
|
|
* |
19
|
|
|
* @package Grido |
20
|
|
|
* @subpackage Components\Filters |
21
|
|
|
* @author Petr Bugyík |
22
|
|
|
* |
23
|
|
|
* @property array $column |
24
|
|
|
* @property array $condition |
25
|
|
|
* @property mixed $value |
26
|
|
|
* @property-read callable $callback |
27
|
|
|
*/ |
28
|
|
|
class Condition |
29
|
1 |
|
{ |
30
|
|
|
use \Nette\SmartObject; |
31
|
|
|
|
32
|
|
|
const OPERATOR_OR = 'OR'; |
33
|
|
|
const OPERATOR_AND = 'AND'; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
protected $column; |
37
|
|
|
|
38
|
|
|
/** @var array */ |
39
|
|
|
protected $condition; |
40
|
|
|
|
41
|
|
|
/** @var mixed */ |
42
|
|
|
protected $value; |
43
|
|
|
|
44
|
|
|
/** @var callable */ |
45
|
|
|
protected $callback; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $column |
49
|
|
|
* @param mixed $condition |
50
|
|
|
* @param mixed $value |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct($column, $condition, $value = NULL) |
53
|
1 |
|
{ |
54
|
1 |
|
$this->setColumn($column); |
55
|
1 |
|
$this->setCondition($condition); |
56
|
|
|
$this->setValue($value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $column |
61
|
|
|
* @throws Exception |
62
|
|
|
* @return Condition |
63
|
|
|
*/ |
64
|
1 |
|
public function setColumn($column) |
65
|
1 |
|
{ |
66
|
|
|
if (is_array($column)) { |
67
|
|
|
$count = count($column); |
68
|
1 |
|
|
69
|
1 |
|
//check validity |
70
|
|
|
if ($count % 2 === 0) { |
71
|
|
|
throw new Exception('Count of column must be odd.'); |
72
|
1 |
|
} |
73
|
1 |
|
|
74
|
1 |
|
for ($i = 0; $i < $count; $i++) { |
75
|
1 |
|
$item = $column[$i]; |
76
|
1 |
|
if ($i & 1 && !self::isOperator($item)) { |
77
|
|
|
$msg = "The even values of column must be 'AND' or 'OR', '$item' given."; |
78
|
1 |
|
throw new Exception($msg); |
79
|
1 |
|
} |
80
|
1 |
|
} |
81
|
|
|
} else { |
82
|
|
|
$column = (array) $column; |
83
|
1 |
|
} |
84
|
1 |
|
|
85
|
|
|
$this->column = $column; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param mixed $condition |
91
|
|
|
* @return Condition |
92
|
|
|
*/ |
93
|
1 |
|
public function setCondition($condition) |
94
|
1 |
|
{ |
95
|
|
|
$this->condition = (array) $condition; |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param mixed $value |
101
|
|
|
* @return Condition |
102
|
|
|
*/ |
103
|
1 |
|
public function setValue($value) |
104
|
1 |
|
{ |
105
|
|
|
$this->value = (array) $value; |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/**********************************************************************************************/ |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
1 |
|
public function getColumn() |
115
|
|
|
{ |
116
|
|
|
return $this->column; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
1 |
|
public function getCondition() |
123
|
|
|
{ |
124
|
|
|
return $this->condition; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
1 |
|
public function getValue() |
131
|
|
|
{ |
132
|
|
|
return $this->value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
1 |
|
public function getValueForColumn() |
139
|
1 |
|
{ |
140
|
|
|
if (count($this->condition) > 1) { |
141
|
|
|
return $this->value; |
142
|
1 |
|
} |
143
|
1 |
|
|
144
|
1 |
|
$values = []; |
145
|
1 |
|
foreach ($this->getColumn() as $column) { |
146
|
1 |
|
if (!self::isOperator($column)) { |
147
|
1 |
|
foreach ($this->getValue() as $val) { |
148
|
1 |
|
$values[] = $val; |
149
|
1 |
|
} |
150
|
|
|
} |
151
|
1 |
|
} |
152
|
|
|
|
153
|
|
|
return $values; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
1 |
|
public function getColumnWithoutOperator() |
160
|
1 |
|
{ |
161
|
1 |
|
$columns = []; |
162
|
1 |
|
foreach ($this->column as $column) { |
163
|
1 |
|
if (!self::isOperator($column)) { |
164
|
1 |
|
$columns[] = $column; |
165
|
|
|
} |
166
|
1 |
|
} |
167
|
|
|
|
168
|
|
|
return $columns; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return callable |
173
|
|
|
*/ |
174
|
1 |
|
public function getCallback() |
175
|
|
|
{ |
176
|
|
|
return $this->callback; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/**********************************************************************************************/ |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns TRUE if $item is Condition:OPERATOR_AND or Condition:OPERATOR_OR else FALSE. |
183
|
|
|
* @param string $item |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
1 |
|
public static function isOperator($item) |
187
|
|
|
{ |
188
|
|
|
return in_array(strtoupper($item), [self::OPERATOR_AND, self::OPERATOR_OR]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param mixed $column |
193
|
|
|
* @param string $condition |
194
|
|
|
* @param mixed $value |
195
|
|
|
* @return Condition |
196
|
|
|
*/ |
197
|
1 |
|
public static function setup($column, $condition, $value) |
198
|
|
|
{ |
199
|
|
|
return new self($column, $condition, $value); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return Condition |
204
|
|
|
*/ |
205
|
1 |
|
public static function setupEmpty() |
206
|
|
|
{ |
207
|
|
|
return new self(NULL, '0 = 1'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $condition |
212
|
|
|
* @throws Exception |
213
|
|
|
* @return Condition |
214
|
|
|
*/ |
215
|
1 |
|
public static function setupFromArray(array $condition) |
216
|
1 |
|
{ |
217
|
|
|
if (count($condition) !== 3) { |
218
|
|
|
throw new Exception("Condition array must contain 3 items."); |
219
|
1 |
|
} |
220
|
|
|
|
221
|
|
|
return new self($condition[0], $condition[1], $condition[2]); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param callable $callback |
226
|
|
|
* @param mixed $value |
227
|
|
|
* @return Condition |
228
|
|
|
*/ |
229
|
1 |
|
public static function setupFromCallback($callback, $value) |
230
|
1 |
|
{ |
231
|
1 |
|
$self = new self(NULL, NULL); |
232
|
|
|
$self->value = $value; |
233
|
1 |
|
$self->callback = $callback; |
234
|
|
|
|
235
|
|
|
return $self; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/**********************************************************************************************/ |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param string $prefix - column prefix |
242
|
|
|
* @param string $suffix - column suffix |
243
|
|
|
* @param bool $brackets - add brackets when multiple where |
244
|
|
|
* @throws Exception |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
1 |
|
public function __toArray($prefix = NULL, $suffix = NULL, $brackets = TRUE) |
248
|
1 |
|
{ |
249
|
|
|
$condition = []; |
250
|
1 |
|
$addBrackets = $brackets && count($this->column) > 1; |
251
|
1 |
|
|
252
|
1 |
|
if ($addBrackets) { |
253
|
|
|
$condition[] = '('; |
254
|
1 |
|
} |
255
|
1 |
|
|
256
|
1 |
|
$i = 0; |
257
|
1 |
|
foreach ($this->column as $column) { |
258
|
1 |
|
if (self::isOperator($column)) { |
259
|
|
|
$operator = strtoupper($column); |
260
|
1 |
|
$condition[] = " $operator "; |
261
|
1 |
|
|
262
|
1 |
|
} else { |
263
|
|
|
$i = count($this->condition) > 1 ? $i : 0; |
264
|
1 |
|
$condition[] = "{$prefix}$column{$suffix} {$this->condition[$i]}"; |
265
|
|
|
|
266
|
1 |
|
$i++; |
267
|
|
|
} |
268
|
1 |
|
} |
269
|
1 |
|
|
270
|
1 |
|
if ($addBrackets) { |
271
|
|
|
$condition[] = ')'; |
272
|
|
|
} |
273
|
1 |
|
|
274
|
1 |
|
return $condition |
275
|
|
|
? array_values(array_merge([implode('', $condition)], $this->getValueForColumn())) |
276
|
|
|
: $this->condition; |
277
|
1 |
|
} |
278
|
|
|
} |
279
|
|
|
|