|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file QueryResult.php |
|
5
|
|
|
* @brief This file contains the QueryResult class. |
|
6
|
|
|
* @details |
|
7
|
|
|
* @author Filippo F. Fadda |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
//! The CouchDB's results namespace. |
|
12
|
|
|
namespace EoC\Result; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @brief This class implements `IteratorAggregate`, `Countable`, and `ArrayAccess` and let you use the query's result |
|
17
|
|
|
* as an array. |
|
18
|
|
|
* @nosubgrouping |
|
19
|
|
|
*/ |
|
20
|
|
|
class QueryResult implements \IteratorAggregate, \Countable, \ArrayAccess { |
|
21
|
|
|
|
|
22
|
|
|
protected $result; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @brief Creates an instance of the class |
|
27
|
|
|
* @param array $result The result of a CouchDB's query converted from JSON to array. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($result) { |
|
30
|
|
|
$this->result = &$result; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @brief Gets the number of total rows in the view. |
|
36
|
|
|
* @return integer Number of the view rows. |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getTotalRows() { |
|
39
|
|
|
return $this->result['total_rows']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @brief Returns count or sum in case this is the result of a reduce function. |
|
45
|
|
|
* @return integer The result of the reduce function. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getReducedValue() { |
|
48
|
|
|
return empty($this->result['rows']) ? 0 : $this->result['rows'][0]['value']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @brief Returns the result as a real array of rows to be used with `array_column()` or other functions operating on |
|
54
|
|
|
* arrays. |
|
55
|
|
|
* @return array An array of rows. |
|
56
|
|
|
*/ |
|
57
|
|
|
public function asArray() { |
|
58
|
|
|
return $this->result['rows']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @brief Returns `true` in case there aren't rows, `false` otherwise. |
|
64
|
|
|
* @details Since the PHP core developers are noobs, `empty()` cannot be used on any class that implements ArrayAccess. |
|
65
|
|
|
* @attention This method must be used in place of `empty()`. |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isEmpty() { |
|
69
|
|
|
return empty($this->result['rows']) ? TRUE : FALSE; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @brief Returns an external iterator. |
|
75
|
|
|
* @return [ArrayIterator](http://php.net/manual/en/class.arrayiterator.php). |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
public function getIterator() { |
|
78
|
|
|
return new \ArrayIterator($this->result['rows']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @brief Returns the number of documents found. |
|
84
|
|
|
* @return integer Number of documents. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function count() { |
|
87
|
|
|
return count($this->result['rows']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @brief Whether or not an offset exists. |
|
93
|
|
|
* @details This method is executed when using `isset()` or `empty()` on objects implementing ArrayAccess. |
|
94
|
|
|
* @param integer $offset An offset to check for. |
|
95
|
|
|
* @return bool Returns `true` on success or `false` on failure. |
|
96
|
|
|
*/ |
|
97
|
|
|
public function offsetExists($offset) { |
|
98
|
|
|
return isset($this->result['rows'][$offset]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @brief Returns the value at specified offset. |
|
104
|
|
|
* @details This method is executed when checking if offset is `empty()`. |
|
105
|
|
|
* @param integer $offset The offset to retrieve. |
|
106
|
|
|
* @return mixed Can return all value types. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function offsetGet($offset) { |
|
109
|
|
|
return $this->result['rows'][$offset]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
//! @cond HIDDEN_SYMBOLS |
|
114
|
|
|
|
|
115
|
|
|
public function offsetSet($offset, $value) { |
|
116
|
|
|
throw new \BadMethodCallException("Result is immutable and cannot be changed."); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
public function offsetUnset($offset) { |
|
121
|
|
|
throw new \BadMethodCallException("Result is immutable and cannot be changed."); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
//! @endcond |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
} |