1 | <?php |
||
13 | class Result implements \IteratorAggregate, \ArrayAccess, \Countable |
||
14 | { |
||
15 | protected $documents; |
||
16 | protected $total; |
||
17 | |||
18 | /** |
||
19 | * Constructor |
||
20 | * |
||
21 | * @param array $documents An array of Documents |
||
22 | * @param integer $total If this result only represents a small slice of |
||
23 | * the total (when using limit), this parameter |
||
24 | * represents the total number of documents. |
||
25 | */ |
||
26 | 27 | public function __construct($documents, $total) |
|
27 | { |
||
28 | 27 | $this->documents = array_values($documents); |
|
29 | 27 | $this->total = $total; |
|
30 | 27 | } |
|
31 | |||
32 | /** |
||
33 | * Returns the number of documents in this result |
||
34 | * |
||
35 | * @return integer The number of documents |
||
36 | */ |
||
37 | 10 | public function count() |
|
38 | { |
||
39 | 10 | return count($this->documents); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns the total number of documents (if using limit in a query). |
||
44 | * Useful for working out pagination. |
||
45 | * |
||
46 | * @return integer The total number of documents |
||
47 | */ |
||
48 | 8 | public function total() |
|
52 | |||
53 | /** |
||
54 | * @inheritdoc |
||
55 | */ |
||
56 | 1 | public function getIterator() |
|
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | 1 | public function offsetSet($offset, $value) |
|
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | public function offsetExists($offset) |
||
76 | |||
77 | /** |
||
78 | * @inheritdoc |
||
79 | */ |
||
80 | 1 | public function offsetUnset($offset) |
|
84 | |||
85 | /** |
||
86 | * @inheritdoc |
||
87 | */ |
||
88 | 6 | public function offsetGet($offset) |
|
92 | |||
93 | /** |
||
94 | * Gets the first document from the result. |
||
95 | * |
||
96 | * @return mixed The first document, or false if the result is empty. |
||
97 | */ |
||
98 | 11 | public function first() |
|
102 | |||
103 | /** |
||
104 | * Gets the last document from the results. |
||
105 | * |
||
106 | * @return mixed The last document, or false if the result is empty. |
||
107 | */ |
||
108 | 1 | public function last() |
|
112 | |||
113 | /** |
||
114 | * Get the value specified by $key of the first object in the result. |
||
115 | * |
||
116 | * @return mixed The value, or false if there are no documents or the key |
||
117 | * doesnt exist. |
||
118 | */ |
||
119 | 3 | public function value($key) |
|
129 | |||
130 | /** |
||
131 | * Returns an array where each value is a single property from each |
||
132 | * document. If the property doesnt exist on the document then it won't |
||
133 | * be in the returned array. |
||
134 | * |
||
135 | * @param string $field The name of the field to pick. |
||
136 | * |
||
137 | * @return array The array of values, one from each document. |
||
138 | */ |
||
139 | 1 | public function pick($field) |
|
151 | |||
152 | /** |
||
153 | * Returns an assoiative array (a hash), where for each document the |
||
154 | * value of one property is the key, and another property is the value. |
||
155 | * |
||
156 | * @param string $keyField The name of the property to use for the key. |
||
157 | * @param string $valueField Name of the property to use for the value. |
||
158 | * |
||
159 | * @return array An associative array. |
||
160 | */ |
||
161 | 1 | public function hash($keyField, $valueField) |
|
171 | } |
||
172 |