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\Events\Event; |
16
|
|
|
use Maslosoft\Mangan\Interfaces\DataProviderInterface; |
17
|
|
|
use Maslosoft\Mangan\Interfaces\FinderInterface; |
18
|
|
|
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface; |
19
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\ConfigureTrait; |
20
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\CriteriaTrait; |
21
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\DataTrait; |
22
|
|
|
use Maslosoft\Mangan\Traits\DataProvider\PaginationTrait; |
23
|
|
|
use Maslosoft\Mangan\Traits\ModelAwareTrait; |
24
|
|
|
use Maslosoft\Mangan\Traits\SortAwareTrait; |
25
|
|
|
use Maslosoft\Manganel\Interfaces\IndexAwareInterface; |
26
|
|
|
use Maslosoft\Manganel\Interfaces\ScoreAwareInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SearchProvider |
30
|
|
|
* |
31
|
|
|
* @method SearchCriteria getCriteria() |
32
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
33
|
|
|
*/ |
34
|
|
|
class SearchProvider implements DataProviderInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
use ConfigureTrait, |
38
|
|
|
CriteriaTrait, |
39
|
|
|
DataTrait, |
40
|
|
|
ModelAwareTrait, |
41
|
|
|
PaginationTrait, |
42
|
|
|
SortAwareTrait; |
43
|
|
|
|
44
|
|
|
const CriteriaClass = SearchCriteria::class; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Total items count cache |
48
|
|
|
* @var int |
49
|
|
|
*/ |
50
|
|
|
private $totalItemCount = null; |
51
|
|
|
|
52
|
7 |
|
public function __construct($modelClass = null, $config = []) |
53
|
|
|
{ |
54
|
7 |
|
$this->configure($modelClass, $config); |
55
|
7 |
|
} |
56
|
|
|
|
57
|
7 |
|
protected function fetchData() |
58
|
|
|
{ |
59
|
|
|
|
60
|
7 |
|
$criteria = $this->configureFetch(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* TODO Refactor this into SearchFinder class |
64
|
|
|
*/ |
65
|
7 |
|
$qb = new QueryBuilder(); |
66
|
7 |
|
if ($criteria instanceof SearchCriteria) |
67
|
7 |
|
{ |
68
|
7 |
|
$models = $criteria->getModels(); |
69
|
7 |
|
if (!empty($models)) |
70
|
7 |
|
{ |
71
|
|
|
$qb->add($models); |
72
|
|
|
} |
73
|
7 |
|
} |
74
|
7 |
|
$model = $this->getModel(); |
75
|
7 |
|
if (!Event::handled($model, FinderInterface::EventBeforeFind)) |
|
|
|
|
76
|
7 |
|
{ |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
7 |
|
$modelCriteria = null; |
81
|
|
|
|
82
|
|
|
// This check is required for plain php objects |
83
|
7 |
|
if ($model instanceof WithCriteriaInterface) |
84
|
7 |
|
{ |
85
|
|
|
$modelCriteria = $model->getDbCriteria(); |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
$criteria->mergeWith($modelCriteria); |
89
|
7 |
|
if (!empty($model)) |
90
|
7 |
|
{ |
91
|
7 |
|
$qb->add($model); |
92
|
7 |
|
} |
93
|
7 |
|
$qb->setCriteria($criteria); |
94
|
7 |
|
$rawResults = $qb->search($criteria->getSearch()); |
95
|
7 |
|
$results = []; |
96
|
7 |
|
foreach ($rawResults as $data) |
97
|
|
|
{ |
98
|
7 |
|
$model = SearchArray::toModel($data['_source']); |
99
|
7 |
|
if ($model instanceof IndexAwareInterface) |
100
|
7 |
|
{ |
101
|
1 |
|
$model->setIndex($data['_index']); |
102
|
1 |
|
} |
103
|
7 |
|
if ($model instanceof ScoreAwareInterface) |
104
|
7 |
|
{ |
105
|
1 |
|
$model->setScore($data['_score']); |
106
|
1 |
|
} |
107
|
7 |
|
$results[] = $model; |
108
|
7 |
|
} |
109
|
7 |
|
return $results; |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
public function getItemCount($refresh = false) |
113
|
|
|
{ |
114
|
5 |
|
return count($this->getData($refresh)); |
115
|
|
|
} |
116
|
|
|
|
117
|
7 |
|
public function getTotalItemCount() |
118
|
|
|
{ |
119
|
7 |
|
if ($this->totalItemCount === null) |
120
|
7 |
|
{ |
121
|
7 |
|
$qb = new QueryBuilder($this->getModel()); |
122
|
7 |
|
$this->totalItemCount = $qb->count($this->getCriteria()->getSearch()); |
123
|
7 |
|
} |
124
|
7 |
|
return $this->totalItemCount; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.