-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.ODataFilterClause.php
107 lines (103 loc) · 3.21 KB
/
class.ODataFilterClause.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
<?php
class ODataFilterClause
{
public $var1;
public $var2;
public $op;
function __construct($string=false)
{
if($string !== false) $this->process_filter_string($string);
}
static function str_startswith($haystack, $needle)
{
return substr($haystack, 0, strlen($needle)) === $needle;
}
protected function process_filter_string($string)
{
if(self::str_startswith($string, 'substringof'))
{
$this->op = strtok($string, '(');
$this->var1 = strtok(',');
$this->var2 = strtok(')');
return;
}
$field = strtok($string, ' ');
$op = strtok(' ');
$rest = strtok("\0");
switch($op)
{
case 'ne':
$op = '!=';
break;
case 'eq':
$op = '=';
break;
case 'lt':
$op = '<';
break;
case 'le':
$op = '<=';
break;
case 'gt':
$op = '>';
break;
case 'ge':
$op = '>=';
break;
}
$this->var1 = $field;
$this->op = $op;
$this->var2 = $rest;
if($this->var2 === 'null')
{
$this->var2 = null;
}
}
public function filterArray(&$array)
{
$res = array();
foreach($array as $member)
{
if(is_object($member))
{
$member = json_decode(json_encode($member), true);
}
switch($this->op)
{
case '=':
if($this->var2 === null)
{
if(strchr($this->var1, '.') !== false && strchr($this->var1, '@') === false)
{
$parts = explode('.', $this->var1);
if(!isset($member[$parts[0]]) || !isset($member[$parts[0]][$parts[1]]) || $member[$parts[0]][$parts[1]] === null)
{
array_push($res, $member);
}
}
else if(!isset($member[$this->var1]) || $member[$this->var1] === null)
{
array_push($res, $member);
}
}
else
{
if(strchr($this->var1, '.') !== false && strchr($this->var1, '@') === false)
{
$parts = explode('.', $this->var1);
if(isset($member[$parts[0]]) && isset($member[$parts[0]][$parts[1]]) && $member[$parts[0]][$parts[1]] == $this->var2)
{
array_push($res, $member);
}
}
else if(isset($member[$this->var1]) && $member[$this->var1] == $this->var2)
{
array_push($res, $member);
}
}
}
}
$array = $res;
}
}
?>