Completed
Push — master ( 2ff065...23f2bf )
by Federico
15s
created

ResultSet   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 32
lcom 2
cbo 3
dl 0
loc 343
rs 9.6
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getResults() 0 4 1
A getDocuments() 0 9 2
A hasSuggests() 0 6 1
A hasAggregations() 0 6 1
A getAggregation() 0 9 3
A hasTimedOut() 0 6 1
A getResponse() 0 4 1
A getQuery() 0 4 1
A count() 0 4 1
A countSuggests() 0 4 1
A current() 0 8 2
A next() 0 6 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 8 2
A offsetSet() 0 12 3
A offsetUnset() 0 4 1
A getSuggests() 0 6 1
A getAggregations() 0 6 1
A getTotalHits() 0 6 1
A getMaxScore() 0 6 1
A getTotalTime() 0 6 1
1
<?php
2
namespace Elastica;
3
4
use Elastica\Exception\InvalidException;
5
6
/**
7
 * Elastica result set.
8
 *
9
 * List of all hits that are returned for a search on elasticsearch
10
 * Result set implements iterator
11
 *
12
 * @author Nicolas Ruflin <[email protected]>
13
 */
14
class ResultSet implements \Iterator, \Countable, \ArrayAccess
15
{
16
    /**
17
     * Current position.
18
     *
19
     * @var int Current position
20
     */
21
    private $_position = 0;
22
23
    /**
24
     * Query.
25
     *
26
     * @var Query Query object
27
     */
28
    private $_query;
29
30
    /**
31
     * Response.
32
     *
33
     * @var Response Response object
34
     */
35
    private $_response;
36
37
    /**
38
     * Results.
39
     *
40
     * @var Result[] Results
41
     */
42
    private $_results = [];
43
44
    /**
45
     * Constructs ResultSet object.
46
     *
47
     * @param Response $response Response object
48
     * @param Query    $query    Query object
49
     * @param Result[] $results
50
     */
51
    public function __construct(Response $response, Query $query, $results)
52
    {
53
        $this->_query = $query;
54
        $this->_response = $response;
55
        $this->_results = $results;
56
    }
57
58
    /**
59
     * Returns all results.
60
     *
61
     * @return Result[] Results
62
     */
63
    public function getResults()
64
    {
65
        return $this->_results;
66
    }
67
68
    /**
69
     * Returns all Documents.
70
     *
71
     * @return array Documents \Elastica\Document
72
     */
73
    public function getDocuments()
74
    {
75
        $documents = [];
76
        foreach ($this->_results as $doc) {
77
            $documents[] = $doc->getDocument();
78
        }
79
80
        return $documents;
81
    }
82
83
    /**
84
     * Returns true if the response contains suggestion results; false otherwise.
85
     *
86
     * @return bool
87
     */
88
    public function hasSuggests()
89
    {
90
        $data = $this->_response->getData();
91
92
        return isset($data['suggest']);
93
    }
94
95
    /**
96
     * Return all suggests.
97
     *
98
     * @return array suggest results
99
     */
100
    public function getSuggests()
101
    {
102
        $data = $this->_response->getData();
103
104
        return $data['suggest'] ?? [];
105
    }
106
107
    /**
108
     * Returns whether aggregations exist.
109
     *
110
     * @return bool Aggregation existence
111
     */
112
    public function hasAggregations()
113
    {
114
        $data = $this->_response->getData();
115
116
        return isset($data['aggregations']);
117
    }
118
119
    /**
120
     * Returns all aggregation results.
121
     *
122
     * @return array
123
     */
124
    public function getAggregations()
125
    {
126
        $data = $this->_response->getData();
127
128
        return $data['aggregations'] ?? [];
129
    }
130
131
    /**
132
     * Retrieve a specific aggregation from this result set.
133
     *
134
     * @param string $name the name of the desired aggregation
135
     *
136
     * @throws Exception\InvalidException if an aggregation by the given name cannot be found
137
     *
138
     * @return array
139
     */
140
    public function getAggregation($name)
141
    {
142
        $data = $this->_response->getData();
143
144
        if (isset($data['aggregations']) && isset($data['aggregations'][$name])) {
145
            return $data['aggregations'][$name];
146
        }
147
        throw new InvalidException("This result set does not contain an aggregation named {$name}.");
148
    }
149
150
    /**
151
     * Returns the total number of found hits.
152
     *
153
     * @return int Total hits
154
     */
155
    public function getTotalHits()
156
    {
157
        $data = $this->_response->getData();
158
159
        return (int) ($data['hits']['total'] ?? 0);
160
    }
161
162
    /**
163
     * Returns the max score of the results found.
164
     *
165
     * @return float Max Score
166
     */
167
    public function getMaxScore()
168
    {
169
        $data = $this->_response->getData();
170
171
        return (float) ($data['hits']['max_score'] ?? 0);
172
    }
173
174
    /**
175
     * Returns the total number of ms for this search to complete.
176
     *
177
     * @return int Total time
178
     */
179
    public function getTotalTime()
180
    {
181
        $data = $this->_response->getData();
182
183
        return $data['took'] ?? 0;
184
    }
185
186
    /**
187
     * Returns true if the query has timed out.
188
     *
189
     * @return bool Timed out
190
     */
191
    public function hasTimedOut()
192
    {
193
        $data = $this->_response->getData();
194
195
        return !empty($data['timed_out']);
196
    }
197
198
    /**
199
     * Returns response object.
200
     *
201
     * @return Response Response object
202
     */
203
    public function getResponse()
204
    {
205
        return $this->_response;
206
    }
207
208
    /**
209
     * @return Query
210
     */
211
    public function getQuery()
212
    {
213
        return $this->_query;
214
    }
215
216
    /**
217
     * Returns size of current set.
218
     *
219
     * @return int Size of set
220
     */
221
    public function count()
222
    {
223
        return count($this->_results);
224
    }
225
226
    /**
227
     * Returns size of current suggests.
228
     *
229
     * @return int Size of suggests
230
     */
231
    public function countSuggests()
232
    {
233
        return sizeof($this->getSuggests());
234
    }
235
236
    /**
237
     * Returns the current object of the set.
238
     *
239
     * @return \Elastica\Result|bool Set object or false if not valid (no more entries)
240
     */
241
    public function current()
242
    {
243
        if ($this->valid()) {
244
            return $this->_results[$this->key()];
245
        } else {
246
            return false;
247
        }
248
    }
249
250
    /**
251
     * Sets pointer (current) to the next item of the set.
252
     */
253
    public function next()
254
    {
255
        ++$this->_position;
256
257
        return $this->current();
258
    }
259
260
    /**
261
     * Returns the position of the current entry.
262
     *
263
     * @return int Current position
264
     */
265
    public function key()
266
    {
267
        return $this->_position;
268
    }
269
270
    /**
271
     * Check if an object exists at the current position.
272
     *
273
     * @return bool True if object exists
274
     */
275
    public function valid()
276
    {
277
        return isset($this->_results[$this->key()]);
278
    }
279
280
    /**
281
     * Resets position to 0, restarts iterator.
282
     */
283
    public function rewind()
284
    {
285
        $this->_position = 0;
286
    }
287
288
    /**
289
     * Whether a offset exists.
290
     *
291
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
292
     *
293
     * @param int $offset
294
     *
295
     * @return bool true on success or false on failure.
296
     */
297
    public function offsetExists($offset)
298
    {
299
        return isset($this->_results[$offset]);
300
    }
301
302
    /**
303
     * Offset to retrieve.
304
     *
305
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
306
     *
307
     * @param int $offset
308
     *
309
     * @throws Exception\InvalidException If offset doesn't exist
310
     *
311
     * @return Result|null
312
     */
313
    public function offsetGet($offset)
314
    {
315
        if ($this->offsetExists($offset)) {
316
            return $this->_results[$offset];
317
        } else {
318
            throw new InvalidException('Offset does not exist.');
319
        }
320
    }
321
322
    /**
323
     * Offset to set.
324
     *
325
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
326
     *
327
     * @param int    $offset
328
     * @param Result $value
329
     *
330
     * @throws Exception\InvalidException
331
     */
332
    public function offsetSet($offset, $value)
333
    {
334
        if (!($value instanceof Result)) {
335
            throw new InvalidException('ResultSet is a collection of Result only.');
336
        }
337
338
        if (!isset($this->_results[$offset])) {
339
            throw new InvalidException('Offset does not exist.');
340
        }
341
342
        $this->_results[$offset] = $value;
343
    }
344
345
    /**
346
     * Offset to unset.
347
     *
348
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
349
     *
350
     * @param int $offset
351
     */
352
    public function offsetUnset($offset)
353
    {
354
        unset($this->_results[$offset]);
355
    }
356
}
357