|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL-3.0-only, proprietary` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/manganel |
|
7
|
|
|
* @license AGPL-3.0-only, proprietary |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* @link https://maslosoft.com/manganel/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel\Adapters\Finder; |
|
14
|
|
|
|
|
15
|
|
|
use function array_keys; |
|
16
|
|
|
use Maslosoft\Mangan\Interfaces\Adapters\FinderAdapterInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\ModelAwareInterface; |
|
19
|
|
|
use Maslosoft\Manganel\QueryBuilder; |
|
20
|
|
|
use Maslosoft\Manganel\SearchCriteria; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ElasticSearchAdapter |
|
24
|
|
|
* |
|
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
26
|
|
|
*/ |
|
27
|
|
|
class ElasticSearchAdapter implements FinderAdapterInterface |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* |
|
32
|
|
|
* @var QueryBuilder |
|
33
|
|
|
*/ |
|
34
|
|
|
private $qb = null; |
|
35
|
|
|
private $models = []; |
|
36
|
|
|
|
|
37
|
24 |
|
public function __construct($models) |
|
38
|
|
|
{ |
|
39
|
24 |
|
$this->qb = new QueryBuilder(); |
|
40
|
24 |
|
$this->qb->setModels($models); |
|
41
|
24 |
|
$this->models = $models; |
|
42
|
24 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function count(CriteriaInterface $criteria) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->ensure($criteria); |
|
47
|
|
|
return $this->qb->setCriteria($criteria)->count(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
17 |
|
public function findMany(CriteriaInterface $criteria, $fields = array()) |
|
51
|
|
|
{ |
|
52
|
17 |
|
$this->prepare($criteria, $fields); |
|
53
|
17 |
|
return new ElasticSearchCursor($this->qb); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function findOne(CriteriaInterface $criteria, $fields = array()) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->prepare($criteria, $fields); |
|
59
|
|
|
$data = (new ElasticSearchCursor($this->qb))->current(); |
|
60
|
|
|
if (false === $data) |
|
61
|
|
|
{ |
|
62
|
|
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
return $data; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @internal Used for debugging purposes, should not be used for any manipulations! |
|
70
|
|
|
* @return QueryBuilder |
|
71
|
|
|
*/ |
|
72
|
8 |
|
public function getQueryBuilder() |
|
73
|
|
|
{ |
|
74
|
8 |
|
return $this->qb; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
17 |
|
private function prepare(CriteriaInterface $criteria, $fields) |
|
78
|
|
|
{ |
|
79
|
17 |
|
$this->ensure($criteria); |
|
80
|
17 |
|
assert($criteria instanceof SearchCriteria); |
|
81
|
|
|
|
|
82
|
17 |
|
$this->qb->setCriteria($criteria); |
|
83
|
17 |
|
if (!empty($fields)) |
|
84
|
|
|
{ |
|
85
|
3 |
|
$selected = []; |
|
86
|
3 |
|
foreach ($fields as $index => $value) |
|
87
|
|
|
{ |
|
88
|
3 |
|
$selected[$index] = true; |
|
89
|
|
|
} |
|
90
|
3 |
|
$this->qb->getCriteria()->select($selected); |
|
91
|
|
|
} |
|
92
|
17 |
|
} |
|
93
|
|
|
|
|
94
|
17 |
|
private function ensure(&$criteria) |
|
95
|
|
|
{ |
|
96
|
17 |
|
if (!$criteria instanceof SearchCriteria) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$criteria = new SearchCriteria($criteria); |
|
99
|
|
|
} |
|
100
|
17 |
|
$criteria->setModels($this->models); |
|
101
|
17 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|