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