-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filter.php
327 lines (300 loc) · 9.29 KB
/
Filter.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* filter factory class
*
* @author mE
*/
abstract class Filter{
public static function key($name,$filter){
return new Filter_Keyed(name,$filter);
}
public static function __callStatic($function,$args){
if($args && substr($function,0,3) === 'key'){
$function = substr($function,3);
if(is_callable(array('Filter',$function))){
$name = array_shift($args);
$filter = call_user_func_array(array('Filter',$function),$args);
return new Filter_Keyed($name,$filter);
}
}
throw new Filter_Exception('Invalid bla');
}
/**
* create new filter checking for equality
*
* @param mixed,... $value single value to match field or match either of multiple values (array of multiple arguments)
* @return Filter_Named|Filter_Array|Filter_Null depending on value(s) given
*/
public static function eq($value){
if(func_num_args() > 1){
$value = func_get_args();
return new Filter_Array($name,$value);
}
if($value === NULL){
return new Filter_Null();
}
return new Filter_Eq($value);
}
/**
* create new filter checking for UNequality (NOT equal)
*
* @param mixed,... $value
* @return Filter
* @uses Filter::eq()
* @uses Filter::negate()
*/
public static function neq($value){
if(func_num_args() === 1){
return self::negate(self::eq($value));
}
$value = func_get_args();
return self::negate(call_user_func_array(array('Filter','eq'),$value));
}
/**
* create new filter using wildcard search for given value
*
* @param string $value value to search for (may include widcards such as '*' and '?')
* @return Filter_search
*/
public static function search($value){
return new Filter_Search($value);
}
public static function gt($value){
return new Filter_Gt($value);
}
public static function ge($value){
return new Filter_Ge($value);
}
public static function lt($value){
return new Filter_Lt($value);
}
public static function le($value){
return new Filter_Le($value);
}
/**
* compare to given number (may include a comparator)
*
* possible formats include "0" (equals 0), ">=1", ">2", "<3", "<=4",
* "!5" (not 5), "6+" (same as ">=6") and "7-" (same as "<=7")
*
* @param string $value value to compare to
* @return Filter
* @throws Filter_Exception if comparator or value is invalid
*/
public static function compareNumber($value){
$v = $value;
$c = '=';
$t = substr($value,0,2);
if($t === '<=' || $t === '>='){ // two character comparator
$c = $t;
$v = substr($value,2);
}else{
$t = substr($t,0,1);
if($t === '!' || $t === '<' || $t === '>'){ // single character comparator
$c = $t;
$v = substr($value,1);
}else{
$t = substr($value,-1);
if($t === '+' || $t === ' '){ // ends with '+' (non-urlencoded '+' results in a space)
$c = '>=';
$v = substr($value,0,-1);
}else if($t === '-'){ // ends with '-'
$c = '<=';
$v = substr($value,0,-1);
}
}
}
if(!preg_match('/^\d+(?:\.\d+)?$/',$v)){
throw new Filter_Exception('Has to be a valid number');
}
$v = (float)$v;
if($c === '='){
return new Filter_Eq($v);
}else if($c === '!'){
return self::negate(new Filter_Eq($v));
}else if($c === '<='){
return new Filter_Le($v);
}else if($c === '<'){
return new Filter_Lt($v);
}else if($c === '>='){
return new Filter_Ge($v);
}else if($c === '>'){
return new Filter_Gt($v);
}
throw new Filter_Exception('Invalid comparator');
}
/**
* make sure any (some / at least one / logical OR) filter of the given filters match
*
* may be called as either any(Filter,Filter) or any(array(Filter,Filter))
*
* @param array|Filter,... $filter
* @return Filter_Multi
*/
public static function any($filter){
if(func_num_args() > 1){
$filter = func_get_args();
}
return new Filter_Multi_Or($filter);
}
/**
* make sure all (logical AND) of the given filters match
*
* @param array|Filter,... $filter
* @return Filter_Multi
*/
public static function all($filter){
if(func_num_args() > 1){
$filter = func_get_args();
}
return new Filter_Multi_And($filter);
}
/**
* make sure neither of the given filters match
*
* @param array|Filter,... $filter
* @return Filter
* @uses Filter::any()
* @uses Filter::negate()
*/
public static function neither($filter){
if(func_num_args() > 1){
$filter = func_get_args();
}
return self::negate(self::any($filter));
}
/**
* negate the given filter (i.e. return a filter that filters the opposite of the given filter)
*
* @param Filter $filter
* @return Filter
* @uses Filter_Interface_Negate::toNegate() where applicable
*/
public static function negate(Filter $filter){
if($filter instanceof Filter_Interface_Negate){
$filter = clone $filter;
return $filter->toNegate();
}
return new Filter_Negate($filter);
}
/**
* get simplified filter
*
* @param Filter $filter
* @return Filter
*/
public static function simplify(Filter $filter){
if($filter instanceof Filter_Interface_Simplify){
$filter = clone $filter;
$filter = $filter->toSimplify();
if($filter === true){
return self::success();
}else if($filter === false){
return self::fail();
}
}
return $filter;
}
/**
* return a filter which always filters everything
*
* @return Filter_Fail
*/
public static function fail(){
return new Filter_Fail();
}
/**
* return a filter which never filters anything
*
* @return Filter_Success
*/
public static function success(){
return new Filter_Success();
}
/**
* make sure key with name starts with value
*
* @param string $name
* @param string $value
* @return Filter_Begins
*/
public static function begins($name,$value){
return new Filter_Begins($name,$value);
}
////////////////////////////////////////////////////////////////////////////
/**
* convert filter to sql where clause
*
* @return string
* @uses Filter::toSql()
*/
public function __toString(){
if($this instanceof Filter_Interface_Sql){
try{
return $this->toSql(Db::singleton());
}
catch(Exception $e){
var_dump($e);
}
}
return var_export($this,true);
//throw new Filter_Exception('Unable to convert to string');
}
protected function escapeDbName($name,$db){
return $db->escape($name,Db::ESCAPE_NAME);
return '`'.$name.'`';
}
protected function escapeDbValue($value,$db){
if($value instanceof Filter_Interface_Sql){
return $value->toSql($db);
}
return $db->escape($value);
if(is_int($value) || is_float($value)){
return $value;
}
if($value === true || $value === false){
return (int)$value;
}
if($value === NULL){
return 'NULL';
}
return Db::singleton()->quote($value);
}
/**
* check whether this filter matches the given value
*
* @param mixed $row
* @return boolean
*/
abstract public function matches($row);
/**
* apply filter to given data/collection/iterator
*
* @param array $data complete input data
* @return array filtered result
* @uses Filter::matches()
*/
public function apply($data){
$ret = array();
foreach($data as $key=>$row){
if($this->matches($row)){
$ret[$key] = $row;
}
}
return $ret;
}
/**
* get filtered SPL iterator for given data/iterator
*
* @param Iterator|Traversable|array $data complete input data
* @return Iterator iterator with only filtered values left
* @uses Filter_Adapter_Iterator
* @link http://www.php.net/manual/en/class.filteriterator.php
*/
public function toIterator($data){
if(!($data instanceof Iterator)){
$data = new IteratorIterator($data);
}
return new Filter_Adapter_Iterator($data,$this);
}
}