forked from contentful/contentful.php
/
Query.php
352 lines (315 loc) · 8.08 KB
/
Query.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* @copyright 2015 Contentful GmbH
* @license MIT
*/
namespace Contentful;
use Contentful\Delivery\ContentType;
/**
* A Query is used to filter and order collections when making API requests.
*
* @api
*/
class Query
{
/**
* ISO8601 but with the seconds forced to 0.
*/
const DATE_FORMAT = 'Y-m-d\TH:i:00P';
/**
* Maximum number of results to retrieve
*
* @var int|null
*/
private $limit;
/**
* The first result to retrieve
*
* @var int|null
*/
private $skip;
/**
* The field to order the retrived results by
*
* @var string|null
*/
private $order;
/**
* For entries, limit results to this content type
*
* @var string|null
*/
private $contentType;
/**
* Assets only. Limit to a group of MIME-types.
*
* @var string|null
*/
private $mimeTypeGroup;
/**
* Whether the order of the retrieved results should be reversed.
*
* @var bool
*/
private $orderReversed = false;
/**
* List of fields for filters
*
* @var array
*/
private $whereConditions = [];
/**
* Query constructor.
*
* Empty for now, included for forward compatibility.
*
* @api
*/
public function __construct()
{
}
/**
* Returns the parameters to execute this query.
*
* @return array
*
* @api
*/
public function getQueryData()
{
$data = [
'limit' => $this->limit,
'skip' => $this->skip,
'content_type' => $this->contentType,
'mimetype_group' => $this->mimeTypeGroup
];
if ($this->order !== null) {
$dir = '';
if ($this->orderReversed) {
$dir .= '-';
}
$data['order'] = $dir . $this->order;
}
foreach ($this->whereConditions as $whereCondition) {
$key = $whereCondition->field;
if ($whereCondition->operator !== null) {
$key .= '[' . $whereCondition->operator . ']';
}
$data[$key] = $whereCondition->value;
}
return $data;
}
/**
* The urlencoded query string for this query.
*
* @return string
*
* @internal
*/
public function getQueryString()
{
return http_build_query($this->getQueryData(), '', '&', PHP_QUERY_RFC3986);
}
/**
* Sets the index of the first result to retrieve. To reset set to NULL.
*
* @param int|null $skip The index of the first result that will be retrieved. Must be >= 0.
*
* @return $this
*
* @throws \RangeException If $skip is not within the specified range
*
* @api
*/
public function setSkip($skip)
{
if ($skip !== null && $skip < 0) {
throw new \RangeException('$skip must be 0 or larget, ' . $skip . ' given.');
}
$this->skip = $skip;
return $this;
}
/**
* Returns the index of the first result to retrieve. Can return NULL if no index is specified.
*
* @return int|null
*
* @api
*/
public function getSkip()
{
return $this->skip;
}
/**
* Set the maximum number of results to retrieve. To reset set to NULL;
*
* @param int|null $limit The maximum number of results to retrieve, must be between 1 and 1000 or null
*
* @return $this
*
* @throws \RangeException If $maxArguments is not withing the specified range
*
* @api
*/
public function setLimit($limit)
{
if ($limit !== null && $limit < 1 || $limit > 1000) {
throw new \RangeException('$maxResults must be between 0 and 1000, ' . $limit . ' given.');
}
$this->limit = $limit;
return $this;
}
/**
* Returns the maximum number of results to retrieve. Can return NULL if no maximum is defined.
*
* @return int|null
*
* @api
*/
public function getLimit()
{
return $this->limit;
}
/**
* Set the order of the items retrieved by this query.
*
* Note that when ordering Entries by fields you must set the content_type URI query parameter to the ID of
* the Content Type you want to filter by.
*
* @param string|null $field
* @param bool $reverse
*
* @return $this
*
* @api
*/
public function orderBy($field, $reverse = false)
{
$this->order = $field;
$this->orderReversed = $reverse;
return $this;
}
/**
* Set the content type to which results should be limited. Set to NULL to not filter for a content type.
*
* Only works when querying entries.
*
* @param ContentType|string|null $contentType
*
* @return $this
*
* @api
*/
public function setContentType($contentType)
{
if ($contentType instanceof ContentType) {
$contentType = $contentType->getId();
}
$this->contentType = $contentType;
return $this;
}
/**
* Returns the name of the content type for which results will be retrieved. Can be NULL if no content type is set
*
* @return string|null The name of the content type results will be limited to
*
* @api
*/
public function getContentType()
{
return $this->contentType;
}
/**
* @param string|null $group
*
* @return $this
*
* @api
*/
public function setMimeTypeGroup($group)
{
$validGroups = [
'attachment',
'plaintext',
'image',
'audio',
'video',
'richtext',
'presentation',
'spreadsheet',
'pdfdocument',
'archive',
'code',
'markup'
];
if ($group !== null && !in_array($group, $validGroups, true)) {
throw new \InvalidArgumentException('Unknown MIMI-type group \'' . $group . '\'');
}
$this->mimeTypeGroup = $group;
return $this;
}
/**
* @return null|string
*/
public function getMimeTypeGroup()
{
return $this->mimeTypeGroup;
}
/**
* Add a filter condition to this query.
*
* Valid operators are
* - ne
* - in
* - nin
* - exists
* - lt
* - lte
* - gt
* - gte
* - match
* - near
* - within
*
* @param string $field
* @param string|\DateTimeInterface|\Contentful\Location $value
* @param string|null $operator The operator to use for this condition.
* Default is strict equality.
* @return $this
*
* @throws \InvalidArgumentException If $operator is not one of the valid values
*
* @api
*/
public function where($field, $value, $operator = null)
{
$validOperators = [
'ne', // No equal
'in', // Includes
'nin', // Excludes
'exists', // Exists
'lt', // Less than
'lte', // Less than or equal to
'gt', // Greater than
'gte', // Greater than or equal to,
'match', // Full text search
'near', // Nearby (for locations)
'within', // Within an rectangle (for locations)
];
if ($operator !== null && !in_array($operator, $validOperators, true)) {
throw new \InvalidArgumentException('Unknown operator \'' . $operator . '\'');
}
if ($value instanceof \DateTimeInterface) {
$value = $value->format(self::DATE_FORMAT);
}
if ($value instanceof Location) {
$value = $value->queryStringFormatted();
}
array_push($this->whereConditions, (object) [
'field' => $field,
'value' => $value,
'operator' => $operator
]);
return $this;
}
}