1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace duxet\Rethinkdb\RQL; |
4
|
|
|
|
5
|
|
|
use r; |
6
|
|
|
|
7
|
|
|
class FilterBuilder |
8
|
|
|
{ |
9
|
|
|
public function __construct($document) |
10
|
|
|
{ |
11
|
|
|
$this->document = $document; |
|
|
|
|
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function compileWheres($wheres) |
15
|
|
|
{ |
16
|
|
|
$chain = null; |
17
|
|
|
|
18
|
|
|
foreach ($wheres as $i => &$where) { |
19
|
|
|
$method = 'build'.$where['type'].'filter'; |
20
|
|
|
$filter = self::{$method} |
21
|
|
|
($where); |
22
|
|
|
|
23
|
|
|
if (!$chain) { |
24
|
|
|
$chain = $filter; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// Wrap the where with an $or operator. |
28
|
|
|
if ($where['boolean'] == 'or') { |
29
|
|
|
$chain = $chain->rOr($filter); |
30
|
|
|
} // If there are more wheres, then wrap existing filters with and |
31
|
|
|
elseif ($chain && count($wheres) > 1) { |
32
|
|
|
$chain = $chain->rAnd($filter); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $chain; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function buildBasicFilter($where) |
40
|
|
|
{ |
41
|
|
|
$operator = isset($where['operator']) ? $where['operator'] : '='; |
42
|
|
|
$operator = strtolower($operator); |
43
|
|
|
|
44
|
|
|
// != is same as <>, so just use <> |
45
|
|
|
if ($operator == '!=') { |
46
|
|
|
$operator = '<>'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$value = isset($where['value']) ? $where['value'] : null; |
50
|
|
|
$field = $this->getField($where['column']); |
51
|
|
|
|
52
|
|
|
switch ($operator) { |
53
|
|
|
case '>': |
54
|
|
|
return $field->gt($value); |
55
|
|
|
case '>=': |
56
|
|
|
return $field->ge($value); |
57
|
|
|
case '<': |
58
|
|
|
return $field->lt($value); |
59
|
|
|
case '<=': |
60
|
|
|
return $field->le($value); |
61
|
|
|
case '<>': |
62
|
|
|
return $field->ne($value); |
63
|
|
|
case 'contains': |
64
|
|
|
return $field->contains($value); |
65
|
|
|
case 'exists': |
66
|
|
|
$field = $field->rDefault(null); |
67
|
|
|
|
68
|
|
|
return ($value) ? $field : $field->not(); |
69
|
|
|
case 'type': |
70
|
|
|
return $field->typeOf()->eq(strtoupper($value)); |
71
|
|
|
case 'mod': |
72
|
|
|
$mod = $field->mod((int) $value[0])->eq((int) $value[1]); |
73
|
|
|
|
74
|
|
|
return $field->typeOf()->eq('NUMBER')->rAnd($mod); |
75
|
|
|
case 'size': |
76
|
|
|
$size = $field->count()->eq((int) $value); |
77
|
|
|
|
78
|
|
|
return $field->typeOf()->eq('ARRAY')->rAnd($size); |
79
|
|
|
case 'regexp': |
80
|
|
|
$match = $field->match($value); |
81
|
|
|
|
82
|
|
|
return $field->typeOf()->eq('STRING')->rAnd($match); |
83
|
|
|
case 'not regexp': |
84
|
|
|
$match = $field->match($value)->not(); |
85
|
|
|
|
86
|
|
|
return $field->typeOf()->eq('STRING')->rAnd($match); |
87
|
|
|
case 'like': |
88
|
|
|
$regex = str_replace('%', '', $value); |
89
|
|
|
// Convert like to regular expression. |
90
|
|
|
if (!starts_with($value, '%')) { |
91
|
|
|
$regex = '^'.$regex; |
92
|
|
|
} |
93
|
|
|
if (!ends_with($value, '%')) { |
94
|
|
|
$regex = $regex.'$'; |
95
|
|
|
} |
96
|
|
|
$match = $field->match('(?i)'.$regex); |
97
|
|
|
|
98
|
|
|
return $field->typeOf()->eq('STRING')->rAnd($match); |
99
|
|
|
default: |
100
|
|
|
return $field->eq($value); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function buildBetweenFilter($where) |
105
|
|
|
{ |
106
|
|
|
$row = $this->getField($where['column']); |
107
|
|
|
$values = $where['values']; |
108
|
|
|
if ($where['not']) { |
109
|
|
|
$or = $row->ge($values[1]); |
110
|
|
|
|
111
|
|
|
return $row->le($values[0])->rOr($or); |
112
|
|
|
} else { |
113
|
|
|
$and = $row->le($values[1]); |
114
|
|
|
|
115
|
|
|
return $row->ge($values[0])->rAnd($and); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function buildNullFilter($where) |
120
|
|
|
{ |
121
|
|
|
$where['operator'] = '='; |
122
|
|
|
$where['value'] = null; |
123
|
|
|
|
124
|
|
|
return $this->buildBasicFilter($where); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function buildNotNullFilter($where) |
128
|
|
|
{ |
129
|
|
|
return $this->buildNullFilter($where)->not(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function buildInFilter($where) |
133
|
|
|
{ |
134
|
|
|
$field = $this->getField($where['column']); |
135
|
|
|
$values = array_values($where['values']); |
136
|
|
|
|
137
|
|
|
return r\expr($values)->contains($field); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function buildNotInFilter($where) |
141
|
|
|
{ |
142
|
|
|
return $this->buildInFilter($where)->not(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
protected function buildNestedFilter($where) |
146
|
|
|
{ |
147
|
|
|
return $where['query']->buildFilter($this->document); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function getField($name) |
151
|
|
|
{ |
152
|
|
|
$document = $this->document; |
153
|
|
|
|
154
|
|
|
return $document($name); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: