-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCondition.php
274 lines (234 loc) · 6.28 KB
/
Condition.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* This file is part of the Grido (http://grido.bugyik.cz)
*
* Copyright (c) 2011 Petr Bugyík (http://petr.bugyik.cz)
*
* For the full copyright and license information, please view
* the file LICENSE.md that was distributed with this source code.
*/
namespace Grido\Components\Filters;
/**
* Builds filter condition.
*
* @package Grido
* @subpackage Components\Filters
* @author Petr Bugyík
*
* @property array $column
* @property array $condition
* @property mixed $value
* @property-read callable $callback
*/
class Condition extends \Nette\Object
{
const OPERATOR_OR = 'OR';
const OPERATOR_AND = 'AND';
/** @var array */
protected $column;
/** @var array */
protected $condition;
/** @var mixed */
protected $value;
/** @var callable */
protected $callback;
/**
* @param mixed $column
* @param mixed $condition
* @param mixed $value
*/
public function __construct($column, $condition, $value = NULL)
{
$this->setColumn($column);
$this->setCondition($condition);
$this->setValue($value);
}
/**
* @param mixed $column
* @throws \InvalidArgumentException
* @return Condition
*/
public function setColumn($column)
{
if (is_array($column)) {
$count = count($column);
//check validity
if ($count % 2 === 0) {
throw new \InvalidArgumentException('Count of column must be odd.');
}
for ($i = 0; $i < $count; $i++) {
$item = $column[$i];
if ($i & 1 && !self::isOperator($item)) {
$msg = "The even values of column must be 'AND' or 'OR', '$item' given.";
throw new \InvalidArgumentException($msg);
}
}
} else {
$column = (array) $column;
}
$this->column = $column;
return $this;
}
/**
* @param mixed $condition
* @return Condition
*/
public function setCondition($condition)
{
$this->condition = (array) $condition;
return $this;
}
/**
* @param mixed $value
* @return Condition
*/
public function setValue($value)
{
$this->value = (array) $value;
return $this;
}
/**********************************************************************************************/
/**
* @return array
*/
public function getColumn()
{
return $this->column;
}
/**
* @return array
*/
public function getCondition()
{
return $this->condition;
}
/**
* @return array
*/
public function getValue()
{
return $this->value;
}
/**
* @return array
*/
public function getValueForColumn()
{
if (count($this->condition) > 1) {
return $this->value;
}
$values = array();
foreach ($this->getColumn() as $column) {
if (!self::isOperator($column)) {
foreach ($this->getValue() as $val) {
$values[] = $val;
}
}
}
return $values;
}
/**
* @return array
*/
public function getColumnWithoutOperator()
{
$columns = array();
foreach ($this->column as $column) {
if (!self::isOperator($column)) {
$columns[] = $column;
}
}
return $columns;
}
/**
* @return callable
*/
public function getCallback()
{
return $this->callback;
}
/**********************************************************************************************/
/**
* Returns TRUE if $item is Condition:OPERATOR_AND or Condition:OPERATOR_OR else FALSE.
* @param string $item
* @return bool
*/
public static function isOperator($item)
{
return in_array(strtoupper($item), array(self::OPERATOR_AND, self::OPERATOR_OR));
}
/**
* @param mixed $column
* @param string $condition
* @param mixed $value
* @return Condition
*/
public static function setup($column, $condition, $value)
{
return new self($column, $condition, $value);
}
/**
* @return Condition
*/
public static function setupEmpty()
{
return new self(NULL, '0 = 1');
}
/**
* @param array $condition
* @throws \InvalidArgumentException
* @return Condition
*/
public static function setupFromArray(array $condition)
{
if (count($condition) !== 3) {
throw new \InvalidArgumentException("Condition array must contain 3 items.");
}
return new self($condition[0], $condition[1], $condition[2]);
}
/**
* @param callable $callback
* @param mixed $value
* @return Condition
*/
public static function setupFromCallback($callback, $value)
{
$self = new self(NULL, NULL);
$self->value = $value;
$self->callback = $callback;
return $self;
}
/**********************************************************************************************/
/**
* @param string $prefix - column prefix
* @param string $suffix - column suffix
* @param bool $brackets - add brackets when multiple where
* @throws \InvalidArgumentException
* @return array
*/
public function __toArray($prefix = NULL, $suffix = NULL, $brackets = TRUE)
{
$condition = array();
$addBrackets = $brackets && count($this->column) > 1;
if ($addBrackets) {
$condition[] = '(';
}
$i = 0;
foreach ($this->column as $column) {
if (self::isOperator($column)) {
$operator = strtoupper($column);
$condition[] = " $operator ";
} else {
$i = count($this->condition) > 1 ? $i : 0;
$condition[] = "{$prefix}$column{$suffix} {$this->condition[$i]}";
$i++;
}
}
if ($addBrackets) {
$condition[] = ')';
}
return $condition
? array_values(array_merge(array(implode('', $condition)), $this->getValueForColumn()))
: $this->condition;
}
}