1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maslosoft\Manganel\Adapters\Finder; |
4
|
|
|
|
5
|
|
|
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface; |
6
|
|
|
use Maslosoft\Manganel\QueryBuilder; |
7
|
|
|
use Maslosoft\Manganel\SearchCriteria; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ElasticSearchCursor |
11
|
|
|
* |
12
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
13
|
|
|
*/ |
14
|
|
|
class ElasticSearchCursor implements FinderCursorInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Whether query was sent to search server |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
private $isExecuted = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* |
25
|
|
|
* @var QueryBuilder |
26
|
|
|
*/ |
27
|
|
|
private $qb = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $data = []; |
34
|
|
|
|
35
|
3 |
|
public function __construct(QueryBuilder $qb) |
36
|
|
|
{ |
37
|
3 |
|
$this->qb = $qb; |
38
|
3 |
|
$criteria = $qb->getCriteria(); |
39
|
3 |
|
if (empty($criteria)) |
40
|
|
|
{ |
41
|
|
|
$this->qb->setCriteria(new SearchCriteria()); |
42
|
|
|
} |
43
|
3 |
|
} |
44
|
|
|
|
45
|
3 |
|
public function limit($num) |
46
|
|
|
{ |
47
|
3 |
|
$this->qb->getCriteria()->limit($num); |
48
|
3 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function skip($num) |
52
|
|
|
{ |
53
|
|
|
$this->qb->getCriteria()->offset($num); |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function sort(array $fields) |
58
|
|
|
{ |
59
|
|
|
$this->qb->getCriteria()->setSort($fields); |
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function fields(array $fields) |
64
|
|
|
{ |
65
|
|
|
$this->qb->getCriteria()->select($fields); |
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// <editor-fold defaultstate="collapsed" desc="Countable impl"> |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
3 |
|
public function count() |
76
|
|
|
{ |
77
|
3 |
|
$this->execute(); |
78
|
3 |
|
return count($this->data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// </editor-fold> |
82
|
|
|
// <editor-fold defaultstate="collapsed" desc="Iterator impl"> |
83
|
|
|
public function current() |
84
|
|
|
{ |
85
|
|
|
$this->execute(); |
86
|
|
|
return current($this->data); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function key() |
90
|
|
|
{ |
91
|
|
|
$this->execute(); |
92
|
|
|
return key($this->data); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function next() |
96
|
|
|
{ |
97
|
|
|
$this->execute(); |
98
|
|
|
next($this->data); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function rewind() |
102
|
|
|
{ |
103
|
|
|
$this->execute(); |
104
|
|
|
return reset($this->data); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function valid() |
108
|
|
|
{ |
109
|
|
|
$this->execute(); |
110
|
|
|
$key = key($this->data); |
111
|
|
|
return ($key !== null && $key !== false); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
private function execute() |
115
|
|
|
{ |
116
|
3 |
|
if (!$this->isExecuted) |
117
|
|
|
{ |
118
|
3 |
|
$this->isExecuted = true; |
119
|
3 |
|
$data = $this->qb->search(); |
120
|
3 |
|
foreach ($data as $result) |
121
|
|
|
{ |
122
|
|
|
$this->data[] = $result['_source']; |
123
|
|
|
} |
124
|
|
|
} |
125
|
3 |
|
} |
126
|
|
|
|
127
|
|
|
// </editor-fold> |
128
|
|
|
} |
129
|
|
|
|