1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\Wufoo; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* An EntryQuery is used to specify how the results will be fetched and what entry filters will be used. |
7
|
|
|
* |
8
|
|
|
* @api |
9
|
|
|
* @since 0.1.0 |
10
|
|
|
*/ |
11
|
|
|
class EntryQuery extends ApiObject |
12
|
|
|
{ |
13
|
|
|
private $filters; |
14
|
|
|
private $booleanAnd; |
15
|
|
|
private $ascending; |
16
|
|
|
private $sortField; |
17
|
|
|
private $pageStart; |
18
|
|
|
private $pageSize; |
19
|
|
|
private $system; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @api |
23
|
|
|
* |
24
|
|
|
* @since 0.1.0 |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->filters = []; |
29
|
|
|
$this->ascending = true; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __toString() |
33
|
|
|
{ |
34
|
|
|
$query = []; |
35
|
|
|
|
36
|
|
|
self::setIfNotNull($query, 'sort', $this->sortField); |
37
|
|
|
|
38
|
|
|
// 'ASC' is the default sorting option, so only set descending order if needed |
39
|
|
|
if (!$this->ascending) |
40
|
|
|
{ |
41
|
|
|
$query['sortDirection'] = 'DESC'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
self::setIfNotNull($query, 'pageStart', $this->pageStart); |
45
|
|
|
self::setIfNotNull($query, 'pageSize', $this->pageSize); |
46
|
|
|
|
47
|
|
|
$filterCounter = 1; |
48
|
|
|
|
49
|
|
|
foreach ($this->filters as $filter) |
50
|
|
|
{ |
51
|
|
|
$query[sprintf('Filter%d', $filterCounter)] = (string)$filter; |
52
|
|
|
$filterCounter++; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// 'AND' is the default sorting option, so only explicitly set it when it's an OR |
56
|
|
|
if (!empty($this->filters) && !$this->booleanAnd) |
57
|
|
|
{ |
58
|
|
|
$query['match'] = 'OR'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
self::setIfNotNull($query, 'system', $this->system); |
62
|
|
|
|
63
|
|
|
// http_build_query() encodes special characters |
64
|
|
|
return self::buildQuery($query); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Filter the results based on entry filters. |
69
|
|
|
* |
70
|
|
|
* @api |
71
|
|
|
* |
72
|
|
|
* @param EntryFilter|EntryFilter[] $filters |
73
|
|
|
* @param bool $booleanAnd |
74
|
|
|
* |
75
|
|
|
* @since 0.1.0 |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function where($filters, $booleanAnd = true) |
80
|
|
|
{ |
81
|
|
|
$this->booleanAnd = (bool)$booleanAnd; |
82
|
|
|
|
83
|
|
|
if (!is_array($filters)) |
84
|
|
|
{ |
85
|
|
|
array_push($this->filters, $filters); |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
foreach ($filters as $filter) |
90
|
|
|
{ |
91
|
|
|
array_push($this->filters, $filter); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* When paginating results of a query, set the offset. |
99
|
|
|
* |
100
|
|
|
* **Warning:** This function should _not_ be used in conjunction with EntryQuery::paginate(). |
101
|
|
|
* |
102
|
|
|
* @api |
103
|
|
|
* |
104
|
|
|
* @param int $offset |
105
|
|
|
* |
106
|
|
|
* @throws \InvalidArgumentException if $offset is not an integer |
107
|
|
|
* |
108
|
|
|
* @since 0.1.0 |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function offset($offset) |
113
|
|
|
{ |
114
|
|
|
if (!is_int($offset)) |
115
|
|
|
{ |
116
|
|
|
throw new \InvalidArgumentException('$offset must be an integer.'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->pageStart = $offset; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the number of results to be returned in a query. |
126
|
|
|
* |
127
|
|
|
* **Warnings** |
128
|
|
|
* |
129
|
|
|
* - The API restricts this value to a maximum of 100. However, this function will not impose any restrictions should |
130
|
|
|
* the API change this restriction |
131
|
|
|
* - This function should _not_ bt used in conjunction with EntryQuery::paginate() |
132
|
|
|
* |
133
|
|
|
* @api |
134
|
|
|
* |
135
|
|
|
* @param int $limit |
136
|
|
|
* |
137
|
|
|
* @throws \InvalidArgumentException if $limit is not an integer |
138
|
|
|
* |
139
|
|
|
* @since 0.1.0 |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function limit($limit) |
144
|
|
|
{ |
145
|
|
|
if (!is_int($limit)) |
146
|
|
|
{ |
147
|
|
|
throw new \InvalidArgumentException('$limit must be an integer.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->pageSize = $limit; |
151
|
|
|
|
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Define the pagination for this query. |
157
|
|
|
* |
158
|
|
|
* **Warning:** This function is provided as a convenience function that will set both the offset and limit in one |
159
|
|
|
* function call. This should be instead of calling EntryQuery::offset() and EntryQuery::limit() separately. |
160
|
|
|
* |
161
|
|
|
* @api |
162
|
|
|
* |
163
|
|
|
* @param int $offset |
164
|
|
|
* @param int $limit |
165
|
|
|
* |
166
|
|
|
* @throws \InvalidArgumentException if $offset or $limit are not integers |
167
|
|
|
* |
168
|
|
|
* @since 0.1.0 |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
|
|
public function paginate($offset, $limit) |
173
|
|
|
{ |
174
|
|
|
$this->limit($limit); |
175
|
|
|
$this->offset($offset); |
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Sort the results based on a field. |
182
|
|
|
* |
183
|
|
|
* @api |
184
|
|
|
* |
185
|
|
|
* @param string $field The API Field ID to sort by |
186
|
|
|
* @param bool $ascending Set to true to sort in ascending order |
187
|
|
|
* |
188
|
|
|
* @since 0.1.0 |
189
|
|
|
* |
190
|
|
|
* @return $this |
191
|
|
|
*/ |
192
|
|
|
public function sortBy($field, $ascending = true) |
193
|
|
|
{ |
194
|
|
|
$this->ascending = $ascending; |
195
|
|
|
$this->sortField = $field; |
196
|
|
|
|
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Whether or not to receive system fields for the entries. |
202
|
|
|
* |
203
|
|
|
* @api |
204
|
|
|
* |
205
|
|
|
* @param bool $system |
206
|
|
|
* |
207
|
|
|
* @since 0.1.0 |
208
|
|
|
* |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function getSystemFields($system = true) |
212
|
|
|
{ |
213
|
|
|
// Per the API documentation, if 'system' is set to _anything_, then it'll return these fields. This is why we |
214
|
|
|
// need to explicitly check for a true value |
215
|
|
|
if ($system === true) |
216
|
|
|
{ |
217
|
|
|
$this->system = 'true'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Convenience function to create an EntryQuery that be used for immediate chaining. |
225
|
|
|
* |
226
|
|
|
* @return EntryQuery |
227
|
|
|
*/ |
228
|
|
|
public static function create() |
229
|
|
|
{ |
230
|
|
|
$query = new self(); |
231
|
|
|
|
232
|
|
|
return $query; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|