forked from RRZE-Webteam/fau-cris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_Filter.php
More file actions
89 lines (82 loc) · 2.98 KB
/
class_Filter.php
File metadata and controls
89 lines (82 loc) · 2.98 KB
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
<?php
class CRIS_filter {
/*
* This class provides filter options for any CRIS data.
*/
public function __construct($definitions) {
/*
* Parse filter operators
*
* Currently supported: eq (equal), gt (greater), ge (greater equal),
* lt (lower), le (lower equal)
*
* Operators are concatenated using __ (two underscores) to the
* attribute name in order to denote the filter, e.g. publyear__eq
*
* All filters are expected as array. Array key is the filter, value is
* the reference, e.g. array("publyear__eq" => 2015).
*
* If more than one filter is set, all filters are combined using "AND"
*/
$filterlist = array();
foreach ($definitions as $_k => $_v) {
// force lower case statements
$_op = explode('__', strtolower($_k));
if (count($_op) != 2)
throw new Exception('invalid filter operator: '. $_k);
if (!array_key_exists($_op[0], $filterlist))
$filterlist[$_op[0]] = array();
$filterlist[$_op[0]][$_op[1]] = $_v;
}
$this->filters = $filterlist;
// list for attributes to be skipped once on evaluation
$this->skip = array();
}
public function evaluate($data) {
/*
* Test "AND"-combined filters against data attributes.
*/
// get and clear skip list
$skip = $this->skip;
$this->skip = array();
foreach ($this->filters as $attr => $_f) {
// skip marked attributes
if (in_array($attr, $skip))
continue;
if (empty($data->attributes[$attr]))
/*
* If attribute is not present, skip filter silently. This makes
* the test successful and may be therefore a bad idea.
*/
//continue;
return false;
foreach ($_f as $operator => $reference) {
if($operator == 'eq' && !is_array($reference))
$reference = (array)$reference;
if ($this->compare($data->attributes[$attr], $operator, $reference) == false)
return false;
}
}
return true;
}
private function compare($value, $operator, $reference) {
/*
* Check attribute value. The comparision is done non-strict so we
* don't have to care for value types.
*/
switch ($operator) {
case "eq":
return (in_array($value,$reference));
case "le":
return ($value <= $reference);
case "lt":
return ($value < $reference);
case "ge":
return ($value >= $reference);
case "gt":
return ($value > $reference);
default:
throw new Exception('invalid compare operator: '. $operator);
}
}
}