-
Notifications
You must be signed in to change notification settings - Fork 27
/
QueryBuilder.php
133 lines (113 loc) · 2.94 KB
/
QueryBuilder.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
<?php
namespace Factual;
/**
* Provides fluent interface to specifying row filter predicate logic
* Based on Factual Java Driver
* This is a refactoring of the Factual Driver by Aaron: https://github.com/Factual/factual-java-driver
* @author Tyler
* @package Factual
* @license Apache 2.0
*/
class QueryBuilder {
private $query;
private $fieldName;
/**
* Constructor. Specifies the name of the field for which to build filter
* logic. Instance methods are used to specify the desired logic.
*/
public function __construct($query, $fieldName) {
$this->query = $query;
$this->fieldName = $fieldName;
}
/**
* Specifies a full text search.
* @param string arg The term(s) for which to full text search against.
* @return the represented query, with the specified full text search added in.
*/
public function search($arg) {
return $this->addFilter("\$search", $arg);
}
public function equal($arg) {
return $this->addFilter("\$eq", $arg);
}
public function notEqual($arg) {
return $this->addFilter("\$neq", $arg);
}
/**
* @param mixed arg Array of arguments, or comma-delineated string thereof
*/
public function in($args) {
if (!is_array($args)) {
$args = explode(",",$args);
}
return $this->addFilter("\$in", $args);
}
/**
* @param mixed arg Array of arguments, or comma-delineated string thereof
*/
public function notIn($args) {
if (!is_array($args)) {
$args = explode(",",$args);
}
return $this->addFilter("\$nin", $args);
}
/**
* @param string arg
*/
public function beginsWith($arg) {
return $this->addFilter("\$bw", $arg);
}
/**
* @param string arg
*/
public function notBeginsWith($arg) {
return $this->addFilter("\$nbw", $arg);
}
/**
* @param mixed arg Array of arguments, or comma-delineated string thereof
*/
public function beginsWithAny($args) {
if (!is_array($args)) {
$args = explode(",",$args);
}
return $this->addFilter("\$bwin", $args);
}
/**
* @param mixed arg Array of arguments, or comma-delineated string thereof
*/
public function notBeginsWithAny($args) {
if (!is_array($args)) {
$args = explode(",",$args);
}
return $this->addFilter("\$nbwin", $args);
}
public function blank() {
return $this->addFilter("\$blank", true);
}
public function notBlank() {
return $this->addFilter("\$blank", false);
}
public function greaterThan($arg) {
return $this->addFilter("\$gt", $arg);
}
public function greaterThanOrEqual($arg) {
return $this->addFilter("\$gte", $arg);
}
public function lessThan($arg) {
return $this->addFilter("\$lt", $arg);
}
public function lessThanOrEqual($arg) {
return $this->addFilter("\$lte", $arg);
}
/**
* Adds filter to query object
* @param string op Operator name
* @param mixed arg Single arguement or array thereof
* @return obj Query object
*/
private function addFilter($op, $arg) {
$this->query->add(new FieldFilter($op, $this->fieldName, $arg));
return $this->query;
}
}
?>