1
|
|
|
<?php namespace Bardex\Elastic; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SearchResult |
5
|
|
|
* @package Bardex\Elastic |
6
|
|
|
* @author Andrey Volynov <[email protected]> |
7
|
|
|
* @author Alexey Sumin <[email protected]> |
8
|
|
|
*/ |
9
|
|
|
class SearchResult implements \ArrayAccess, \Countable, \IteratorAggregate |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $totalFound; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array|array[]|object[] $results |
18
|
|
|
*/ |
19
|
|
|
protected $results = []; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
public function __construct($results, $totalFound) |
23
|
|
|
{ |
24
|
|
|
$results = (array)$results; |
25
|
|
|
$this->results = $results; |
26
|
|
|
$this->totalFound = $totalFound; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Returns first entry from result set, or null, |
31
|
|
|
* if result set is empty. |
32
|
|
|
* |
33
|
|
|
* @return null|array|object|array[]|object[] |
34
|
|
|
*/ |
35
|
|
|
public function getFirst() |
36
|
|
|
{ |
37
|
|
|
if ($this->isEmpty()) { |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
$keys = array_keys($this->results); |
41
|
|
|
return $this->results[$keys[0]]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns the complete result set. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getResults() |
50
|
|
|
{ |
51
|
|
|
return $this->results; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Method returns count of returned query results. |
56
|
|
|
* |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
|
|
public function count() |
60
|
|
|
{ |
61
|
|
|
return count($this->results); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Method returns count of total query results in index. |
66
|
|
|
* |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
|
|
public function getTotalFound() |
70
|
|
|
{ |
71
|
|
|
return $this->totalFound; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @deprecated |
76
|
|
|
* @return int |
77
|
|
|
*/ |
78
|
|
|
public function getTotalCount() |
79
|
|
|
{ |
80
|
|
|
return $this->getTotalFound(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Method determines, if result data set is empty. |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isEmpty() |
90
|
|
|
{ |
91
|
|
|
return $this->count() == 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function offsetExists($offset) |
95
|
|
|
{ |
96
|
|
|
return array_key_exists($offset, $this->results); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function offsetGet($offset) |
100
|
|
|
{ |
101
|
|
|
return $this->results[$offset]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function offsetSet($offset, $value) |
105
|
|
|
{ |
106
|
|
|
$this->results[$offset] = $value; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function offsetUnset($offset) |
112
|
|
|
{ |
113
|
|
|
unset($this->results[$offset]); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getIterator() |
119
|
|
|
{ |
120
|
|
|
return new \ArrayIterator($this->results); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|