1 | <?php |
||
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 = []) |
|
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) |
|
116 | |||
117 | 7 | public function getTotalItemCount() |
|
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.