|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/manganel |
|
7
|
|
|
* @license AGPL, Commercial |
|
8
|
|
|
* |
|
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
|
10
|
|
|
* @link http://maslosoft.com/manganel/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Maslosoft\Manganel; |
|
14
|
|
|
|
|
15
|
|
|
use Maslosoft\Mangan\Interfaces\DataProviderInterface; |
|
16
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait; |
|
17
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait; |
|
18
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\DataTrait; |
|
19
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait; |
|
20
|
|
|
use Maslosoft\Mangan\Traits\ModelAwareTrait; |
|
21
|
|
|
use Maslosoft\Mangan\Traits\SortAwareTrait; |
|
22
|
|
|
use Maslosoft\Manganel\Interfaces\IndexAwareInterface; |
|
23
|
|
|
use Maslosoft\Manganel\Interfaces\ScoreAwareInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SearchProvider |
|
27
|
|
|
* |
|
28
|
|
|
* @method SearchCriteria getCriteria() |
|
29
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
30
|
|
|
*/ |
|
31
|
|
|
class SearchProvider implements DataProviderInterface |
|
32
|
|
|
{ |
|
33
|
|
|
|
|
34
|
|
|
use ConfigureTrait, |
|
35
|
|
|
CriteriaTrait, |
|
36
|
|
|
DataTrait, |
|
37
|
|
|
ModelAwareTrait, |
|
38
|
|
|
PaginationTrait, |
|
39
|
|
|
SortAwareTrait; |
|
40
|
|
|
|
|
41
|
|
|
const CriteriaClass = SearchCriteria::class; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Total items count cache |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
private $totalItemCount = null; |
|
48
|
|
|
|
|
49
|
4 |
|
public function __construct($modelClass = null, $config = []) |
|
50
|
|
|
{ |
|
51
|
4 |
|
$this->configure($modelClass, $config); |
|
52
|
4 |
|
} |
|
53
|
|
|
|
|
54
|
4 |
|
protected function fetchData() |
|
55
|
|
|
{ |
|
56
|
4 |
|
$criteria = $this->configureFetch(); |
|
57
|
|
|
|
|
58
|
4 |
|
$qb = new QueryBuilder(); |
|
59
|
4 |
|
if ($criteria instanceof SearchCriteria) |
|
60
|
4 |
|
{ |
|
61
|
4 |
|
$models = $criteria->getModels(); |
|
62
|
4 |
|
if (!empty($models)) |
|
63
|
4 |
|
{ |
|
64
|
|
|
$qb->add($models); |
|
65
|
|
|
} |
|
66
|
4 |
|
} |
|
67
|
4 |
|
$model = $this->getModel(); |
|
68
|
4 |
|
if (!empty($model)) |
|
69
|
4 |
|
{ |
|
70
|
4 |
|
$qb->add($model); |
|
71
|
4 |
|
} |
|
72
|
4 |
|
$qb->setCriteria($criteria); |
|
73
|
4 |
|
$rawResults = $qb->search($criteria->getSearch()); |
|
74
|
4 |
|
$results = []; |
|
75
|
4 |
|
foreach ($rawResults as $data) |
|
76
|
|
|
{ |
|
77
|
4 |
|
$model = SearchArray::toModel($data['_source']); |
|
78
|
4 |
|
if ($model instanceof IndexAwareInterface) |
|
79
|
4 |
|
{ |
|
80
|
1 |
|
$model->setIndex($data['_index']); |
|
81
|
1 |
|
} |
|
82
|
4 |
|
if ($model instanceof ScoreAwareInterface) |
|
83
|
4 |
|
{ |
|
84
|
1 |
|
$model->setScore($data['_score']); |
|
85
|
1 |
|
} |
|
86
|
4 |
|
$results[] = $model; |
|
87
|
4 |
|
} |
|
88
|
4 |
|
return $results; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
public function getItemCount($refresh = false) |
|
92
|
|
|
{ |
|
93
|
2 |
|
return count($this->getData($refresh)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
4 |
|
public function getTotalItemCount() |
|
97
|
|
|
{ |
|
98
|
4 |
|
if ($this->totalItemCount === null) |
|
99
|
4 |
|
{ |
|
100
|
4 |
|
$qb = new QueryBuilder($this->getModel()); |
|
101
|
4 |
|
$this->totalItemCount = $qb->count($this->getCriteria()->getSearch()); |
|
102
|
4 |
|
} |
|
103
|
4 |
|
return $this->totalItemCount; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|