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
|
|
|
public function __construct($modelClass = null, $config = []) |
53
|
|
|
{ |
54
|
|
|
$this->configure($modelClass, $config); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function fetchData() |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
$criteria = $this->configureFetch(); |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* TODO Refactor this into SearchFinder class |
64
|
|
|
*/ |
65
|
|
|
$qb = new QueryBuilder(); |
66
|
|
|
if ($criteria instanceof SearchCriteria) |
67
|
|
|
{ |
68
|
|
|
$models = $criteria->getModels(); |
69
|
|
|
if (!empty($models)) |
70
|
|
|
{ |
71
|
|
|
$qb->add($models); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
$model = $this->getModel(); |
75
|
|
|
if (!empty($model) && !Event::handled($model, FinderInterface::EventBeforeFind)) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return []; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$modelCriteria = null; |
81
|
|
|
|
82
|
|
|
// This check is required for plain php objects |
83
|
|
|
if ($model instanceof WithCriteriaInterface) |
84
|
|
|
{ |
85
|
|
|
$modelCriteria = $model->getDbCriteria(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$criteria->mergeWith($modelCriteria); |
89
|
|
|
if (!empty($model)) |
90
|
|
|
{ |
91
|
|
|
$qb->add($model); |
92
|
|
|
} |
93
|
|
|
$qb->setCriteria($criteria); |
94
|
|
|
$rawResults = $qb->search($criteria->getSearch()); |
95
|
|
|
$results = []; |
96
|
|
|
foreach ($rawResults as $data) |
97
|
|
|
{ |
98
|
|
|
$model = SearchArray::toModel($data['_source']); |
99
|
|
|
if ($model instanceof IndexAwareInterface) |
100
|
|
|
{ |
101
|
|
|
$model->setIndex($data['_index']); |
102
|
|
|
} |
103
|
|
|
if ($model instanceof ScoreAwareInterface) |
104
|
|
|
{ |
105
|
|
|
$model->setScore($data['_score']); |
106
|
|
|
} |
107
|
|
|
$results[] = $model; |
108
|
|
|
} |
109
|
|
|
return $results; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getItemCount($refresh = false) |
113
|
|
|
{ |
114
|
|
|
return count($this->getData($refresh)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function getTotalItemCount() |
118
|
|
|
{ |
119
|
|
|
if ($this->totalItemCount === null) |
120
|
|
|
{ |
121
|
|
|
$qb = new QueryBuilder($this->getModel()); |
122
|
|
|
/** |
123
|
|
|
* TODO Must count with criteria too! |
124
|
|
|
* And multi model |
125
|
|
|
*/ |
126
|
|
|
$criteria = new SearchCriteria($this->getCriteria()); |
127
|
|
|
$criteria->setLimit(false); |
128
|
|
|
$criteria->setOffset(false); |
129
|
|
|
$qb->setCriteria($criteria); |
130
|
|
|
$this->totalItemCount = $qb->count($this->getCriteria()->getSearch()); |
131
|
|
|
} |
132
|
|
|
return $this->totalItemCount; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: