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