1 | <?php |
||
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 = []) |
|
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) |
|
95 | |||
96 | 4 | public function getTotalItemCount() |
|
105 | |||
106 | } |
||
107 |