Issues (109)

src/EoC/Opt/ViewQueryOpts.php (6 issues)

1
<?php
2
3
/**
4
 * @file ViewQueryOpts.php
5
 * @brief This file contains the ViewQueryOpts class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace EoC\Opt;
12
13
14
/**
15
 * @brief To set the query arguments you must create an instance of this class. Use it when you query a CouchDB View with
16
 * the methods Couch::queryAllDocs(), Couch::queryView() and Couch::queryTempView().
17
 * @nosubgrouping
18
 * @todo Add 'list' property
19
 * @todo Add 'callback' property.
20
 */
21
class ViewQueryOpts extends AbstractOpts {
22
23
  private $includeMissingKeys = FALSE;
24
25
26
  public function reset() {
27
    $this->includeMissingKeys = FALSE;
28
    parent::reset();
29
  }
30
31
32
  /**
33
   * @brief Removes an option previously set.
34
   * @param[in] string $name The option name.
35
   * @retval ViewQueryOpts
36
   * @attention Chainable.
37
   */
38
  public function unsetOpt($name) {
39
    if (array_key_exists($name, $this->options))
40
      unset($this->options['name']);
41
42
    return $this;
43
  }
44
45
46
  /**
47
   * @brief Returns only documents that match the specified key.
48
   * @param[in] string $value The key.
49
   * @param[in] bool $encode (optional) JSON encodes `$value`.
50
   * @retval ViewQueryOpts
51
   * @attention Chainable.
52
   */
53
  public function setKey($value, $encode = TRUE) {
54
    $this->options["key"] = $encode ? json_encode($value) : $value;
55
    return $this;
56
  }
57
58
59
  /**
60
   * @name Key Range
61
   * @brief Those methods are used to return documents in a key range.
62
   */
63
  //!@{
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
65
  /**
66
   * @brief Defines the first key to be included in the range.
67
   * @param[in] string $value The key at which to start the range.
68
   * @param[in] bool $encode (optional) JSON encodes `$value`.
69
   * @retval ViewQueryOpts
70
   * @attention Chainable.
71
   */
72
  public function setStartKey($value, $encode = TRUE) {
73
    $this->options["startkey"] = $encode ? json_encode($value) : $value;
74
    return $this;
75
  }
76
77
78
  /**
79
   * @brief Defines the last key to be included in the range.
80
   * @param[in] string $value The key at which to end the range.
81
   * @param[in] bool $encode (optional) JSON encodes `$value`.
82
   * @retval ViewQueryOpts
83
   * @attention Chainable.
84
   */
85
  public function setEndKey($value, $encode = TRUE) {
86
    $this->options["endkey"] = $encode ? json_encode($value) : $value;
87
    return $this;
88
  }
89
90
  //!@}
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
92
93
  /**
94
   * @name First and Last Documents Identifiers
95
   * @brief First and last documents to be included in the output.
96
   * @details If you expect to have multiple documents emit identical keys, you'll need to use `startDocId` in
97
   * addition to `startKey` to paginate correctly. The reason is that `startKey` alone will no longer be
98
   * sufficient to uniquely identify a row. Those parameters are useless if you don't provide a `startKey`. In fact,
99
   * CouchDB will first look at the `startKey` parameter, then it will use the `startDocId` parameter to further
100
   * redefine the beginning of the range if multiple potential staring rows have the same key but different document IDs.
101
   * Same thing for the `endDocId`.
102
   */
103
  //!@{
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
105
106
  /**
107
   * @brief Sets the ID of the document with which to start the range.
108
   * @param[in] string $value The ID of the document with which to start the range.
109
   * @retval ViewQueryOpts
110
   * @attention Chainable.
111
   */
112
  public function setStartDocId($value) {
113
    $this->options["startkey_docid"] = $value;
114
    return $this;
115
  }
116
117
118
  /**
119
   * @brief Sets the ID of the document with which to end the range.
120
   * @param[in] string $value The ID of the document with which to end the range.
121
   * @retval ViewQueryOpts
122
   * @attention Chainable.
123
   */
124
  public function setEndDocId($value) {
125
    $this->options["endkey_docid"] = $value;
126
    return $this;
127
  }
128
129
  //!@}
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
130
131
132
  /**
133
   * @brief Restricts the number of results.
134
   * @details Allowed values: natural numbers (non-negative integers).
135
   * @param[in] int $value The maximum number of rows to include in the output.
136
   * @retval ViewQueryOpts
137
   * @attention Chainable.
138
   */
139
  public function setLimit($value) {
140
    if (is_int($value) && $value >= 0)
141
      $this->options["limit"] = $value;
142
    else
143
      throw new \Exception("\$value must be a non-negative integer.");
144
145
    return $this;
146
  }
147
148
149
  /**
150
   * @brief Results should be grouped.
151
   * @details The group option controls whether the reduce function reduces to a set of distinct keys or to a single
152
   * result row. This will run the rereduce procedure. This parameter makes sense only if a reduce function is defined
153
   * for the view.
154
   * @retval ViewQueryOpts
155
   * @attention Chainable.
156
   */
157
  public function groupResults() {
158
    $this->options["group"] = "true";
159
    return $this;
160
  }
161
162
163
  /**
164
   * @brief Level at which documents should be grouped.
165
   * @details If your keys are JSON arrays, this parameter will specify how many elements in those arrays to use for
166
   * grouping purposes. If your emitted keys are not JSON arrays this parameter's value will effectively be ignored.
167
   * Allowed values: positive integers.
168
   * @param[in] int $value The number of elements used for grouping purposes.
169
   * @retval ViewQueryOpts
170
   * @attention Chainable.
171
   */
172
  public function setGroupLevel($value) {
173
    if (is_int($value) && $value > 0) {
174
      $this->options["group"] = "false"; // This parameter is used only if 'group' is 'false'.
175
      $this->options["group_level"] = $value;
176
    }
177
    else
178
      throw new \Exception("\$value must be a positive integer.");
179
180
    return $this;
181
  }
182
183
184
  /**
185
   * @brief Calls the reduce function is defined.
186
   * @retval ViewQueryOpts
187
   * @attention Chainable.
188
   */
189
  public function reduce() {
190
    $this->options["reduce"] = "true";
191
    return $this;
192
  }
193
194
195
  /**
196
   * @brief Even if a reduce function is defined for the view, doesn't call it.
197
   * @details If a view contains both a map and reduce function, querying that view will by default return the result
198
   * of the reduce function. To avoid this behaviour you must call this method.
199
   * @retval ViewQueryOpts
200
   * @attention Chainable.
201
   */
202
  public function doNotReduce() {
203
    $this->options["reduce"] = "false";
204
    return $this;
205
  }
206
207
208
  /**
209
   * @brief Automatically fetches and includes full documents.
210
   * @details However, the user should keep in mind that there is a race condition when using this option. It is
211
   * possible that between reading the view data and fetching the corresponding document that the document has changed.
212
   * @warning You can call this method only if the view doesn't contain a reduce function.
213
   * @retval ViewQueryOpts
214
   * @attention Chainable.
215
   */
216
  public function includeDocs() {
217
    $this->options["include_docs"] = "true";
218
    return $this;
219
  }
220
221
222
  /**
223
   * @brief Don't get any data, but all meta-data for this View. The number of documents in this View for example.
224
   * @retval ViewQueryOpts
225
   * @attention Chainable.
226
   */
227
  public function excludeResults() {
228
    $this->options["limit"] = 0;
229
    return $this;
230
  }
231
232
233
  /**
234
   * @brief Tells CouchDB to not include end key in the result.
235
   * @retval ViewQueryOpts
236
   * @attention Chainable.
237
   */
238
  public function excludeEndKey() {
239
    $this->options["inclusive_end"] = "false";
240
    return $this;
241
  }
242
243
244
  /**
245
   * @name View Refresh Controls
246
   * @brief Don't refresh views for quicker results.
247
   * @details The stale option can be used for higher performance at the cost of possibly not seeing the all latest
248
   * documents.
249
   * CouchDB defaults to regenerating views the first time they are accessed. This behavior is preferable in most cases
250
   * as it optimizes the resource utilization on the database server. On the other hand, in some situations the benefit
251
   * of always having fast and updated views far outweigh the cost of regenerating them every time the database server
252
   * receives updates. You can chose CouchDB behaviour using one of the following methods.
253
   * Those methods essentially tell CouchDB that if a reference to the view index is available in memory (ie, if
254
   * the view has been queried at least once since couch was started), go ahead and use it, even if it may be out of
255
   * date. The result is that for a highly trafficked view, end users can see lower latency, although they may not get
256
   * the latest data. However, if there is no view index pointer in memory, the behavior with this option is that same
257
   * as the behavior without the option.
258
   * */
259
  //!@{
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
260
261
262
  /**
263
   * @brief CouchDB will not refresh the view, even if it's stalled.
264
   * @details This is useful in case you chose to not refresh a view when a query is performed on it, because you want
265
   * faster results. Remember, in case, to use an updater script that calls the views periodically, for example using a
266
   * cron. You can find the implementation in Ruby or Python in the document
267
   * [Update views on document save](http://wiki.apache.org/couchdb/Regenerating_views_on_update).
268
   * @retval ViewQueryOpts
269
   * @attention Chainable.
270
   */
271
  public function doNotRefreshView() {
272
    $this->options["stale"] = "ok";
273
    return $this;
274
  }
275
276
277
  /**
278
   * @brief CouchDB will update the view after the query's result is returned.
279
   * @retval ViewQueryOpts
280
   * @attention Chainable.
281
   */
282
  public function queryThenRefreshView() {
283
    $this->options["stale"] = "update_after";
284
    return $this;
285
  }
286
287
  //!@}
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
288
289
290
  /**
291
   * @brief Reverses order of results.
292
   * @details Note that the descending option is applied before any key filtering, so you may need to swap the values
293
   * of the start key and end key options to get the expected results.
294
   * @retval ViewQueryOpts
295
   * @attention Chainable.
296
   */
297
  public function reverseOrderOfResults() {
298
    $this->options["descending"] = "true";
299
    return $this;
300
  }
301
302
303
  /**
304
   * @brief Skips the defined number of documents.
305
   * @details The skip option should only be used with small values, as skipping a large range of documents this way is
306
   * inefficient (it scans the index from the start key and then skips N elements, but still needs to read all the index
307
   * values to do that). For efficient paging you'll need to use start key and limit.
308
   * Allowed values: positive integers.
309
   * @param[in] int $number The number of rows to skip.
310
   * @retval ViewQueryOpts
311
   * @attention Chainable.
312
   */
313
  public function skipDocs($number) {
314
    if (is_int($number) && $number > 0)
315
      $this->options["skip"] = $number;
316
    else
317
      throw new \Exception("\$number must be a positive integer.");
318
319
    return $this;
320
  }
321
322
323
  /**
324
   * @brief Includes conflict documents.
325
   * @retval ViewQueryOpts
326
   * @attention Chainable.
327
   */
328
  public function includeConflicts() {
329
    $this->options["conflicts"] = "true";
330
    return $this;
331
  }
332
333
334
  /**
335
   * @brief Includes an `update_seq` value indicating which sequence id of the database the view reflects.
336
   * @retval ViewQueryOpts
337
   * @attention Chainable.
338
   */
339
  public function includeUpdateSeq() {
340
    $this->options['update_seq'] = "true";
341
    return $this;
342
  }
343
344
345
  /**
346
   * @brief Includes all the rows, even if a match for a key is not found.
347
   * @retval ViewQueryOpts
348
   * @attention Chainable.
349
   */
350
  public function includeMissingKeys() {
351
    $this->includeMissingKeys = TRUE;
352
    return $this;
353
  }
354
355
356
  /**
357
   * @brief Returns `true` if includeMissingKeys() has been called before.
358
   * @retval ViewQueryOpts
359
   * @attention Chainable.
360
   */
361
  public function issetIncludeMissingKeys() {
362
    return $this->includeMissingKeys;
363
  }
364
365
}