1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Maslosoft\Manganel; |
10
|
|
|
|
11
|
|
|
use Maslosoft\Mangan\Interfaces\DataProviderInterface; |
12
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait; |
13
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait; |
14
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\DataTrait; |
15
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait; |
16
|
|
|
use Maslosoft\Mangan\Traits\ModelAwareTrait; |
17
|
|
|
use Maslosoft\Mangan\Traits\SortAwareTrait; |
18
|
|
|
use Maslosoft\Manganel\Interfaces\IndexAwareInterface; |
19
|
|
|
use Maslosoft\Manganel\Interfaces\ScoreAwareInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* SearchProvider |
23
|
|
|
* |
24
|
|
|
* @method SearchCriteria getCriteria() |
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
26
|
|
|
*/ |
27
|
|
|
class SearchProvider implements DataProviderInterface |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
use ConfigureTrait, |
31
|
|
|
CriteriaTrait, |
32
|
|
|
DataTrait, |
33
|
|
|
ModelAwareTrait, |
34
|
|
|
PaginationTrait, |
35
|
|
|
SortAwareTrait; |
36
|
|
|
|
37
|
|
|
const CriteriaClass = SearchCriteria::class; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Total items count cache |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $totalItemCount = null; |
44
|
|
|
|
45
|
4 |
|
public function __construct($modelClass, $config = []) |
46
|
|
|
{ |
47
|
4 |
|
$this->configure($modelClass, $config); |
48
|
4 |
|
} |
49
|
|
|
|
50
|
4 |
|
protected function fetchData() |
51
|
|
|
{ |
52
|
4 |
|
$criteria = $this->configureFetch(); |
53
|
|
|
|
54
|
4 |
|
$qb = new QueryBuilder($this->getModel()); |
55
|
4 |
|
$qb->setCriteria($criteria); |
56
|
4 |
|
$rawResults = $qb->search($criteria->getSearch()); |
57
|
4 |
|
$results = []; |
58
|
4 |
|
foreach ($rawResults as $data) |
59
|
|
|
{ |
60
|
4 |
|
$model = SearchArray::toModel($data['_source']); |
61
|
4 |
|
if ($model instanceof IndexAwareInterface) |
62
|
|
|
{ |
63
|
1 |
|
$model->setIndex($data['_index']); |
64
|
|
|
} |
65
|
4 |
|
if ($model instanceof ScoreAwareInterface) |
66
|
|
|
{ |
67
|
1 |
|
$model->setScore($data['_score']); |
68
|
|
|
} |
69
|
4 |
|
$results[] = $model; |
70
|
|
|
} |
71
|
4 |
|
return $results; |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
public function getItemCount($refresh = false) |
75
|
|
|
{ |
76
|
2 |
|
return count($this->getData($refresh)); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
public function getTotalItemCount() |
80
|
|
|
{ |
81
|
4 |
|
if ($this->totalItemCount === null) |
82
|
|
|
{ |
83
|
4 |
|
$qb = new QueryBuilder($this->getModel()); |
84
|
4 |
|
$this->totalItemCount = $qb->count($this->getCriteria()->getSearch()); |
85
|
|
|
} |
86
|
4 |
|
return $this->totalItemCount; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|