-
Notifications
You must be signed in to change notification settings - Fork 27
/
FilterGroup.php
71 lines (61 loc) · 1.42 KB
/
FilterGroup.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
<?php
namespace Factual;
/**
* Represents a group of Filters as one Filter.
* 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 FilterGroup implements FactualFilter {
private $filters = array (); //array
private $op = "\$and"; //string
/**
* Constructor. Defaults logic to AND.
* @param array filter Filter objects. Filters can be passed as parameter or assigned later
*/
public function __construct($filters = null) {
if ($filters) {
$this->filters = $filters;
}
}
/**
* Sets this FilterGroup's logic, e.g., "$or".
*/
public function op($op) {
$this->op = $op;
return $this;
}
/**
* Sets this FilterGroup's logic to be OR.
*/
public function asOR() {
$this->op = "\$or";
}
public function add($filter) {
$this->filters[] = $filter;
}
/**
* Produces JSON representation for this FilterGroup
* <p>
* For example:
* <pre>
* {"$and":[{"first_name":{"$eq":"Bradley"}},{"region":{"$eq":"CA"}},{"locality":{"$eq":"Los Angeles"}}]}
* </pre>
* @return string
*/
public function toJsonStr() {
return "{\"" . $this->op . "\":[" . $this->logicJsonStr() . "]}";
}
/**
* @return string
*/
private function logicJsonStr() {
$logics = array ();
foreach ($this->filters as $filter) {
$logics[] = $filter->toJsonStr();
}
return implode(",", $logics);
}
}
?>