1 | <?php |
||
29 | class SearchProvider implements DataProviderInterface |
||
30 | { |
||
31 | |||
32 | use ConfigureTrait, |
||
33 | DataTrait, |
||
34 | ModelAwareTrait, |
||
35 | PaginationTrait, |
||
36 | SortAwareTrait, |
||
37 | CriteriaTrait |
||
38 | { |
||
39 | CriteriaTrait::setCriteria as traitSetCriteria; |
||
40 | } |
||
41 | |||
42 | const CriteriaClass = SearchCriteria::class; |
||
43 | |||
44 | /** |
||
45 | * Total items count cache |
||
46 | * @var int |
||
47 | */ |
||
48 | private $totalItemCount = null; |
||
49 | |||
50 | /** |
||
51 | * Finder instance |
||
52 | * @var SearchFinder |
||
53 | */ |
||
54 | private $finder = null; |
||
55 | private $isStopped = false; |
||
56 | |||
57 | 11 | public function __construct($modelClass = null, $config = []) |
|
58 | { |
||
59 | 11 | if (is_array($modelClass)) |
|
60 | { |
||
61 | 2 | $models = $modelClass; |
|
62 | } |
||
63 | else |
||
64 | { |
||
65 | 9 | $models = [$modelClass]; |
|
66 | } |
||
67 | 11 | foreach ($models as $modelClass) |
|
68 | { |
||
69 | 11 | $this->configure($modelClass, $config); |
|
70 | } |
||
71 | 11 | $this->finder = new SearchFinder($models); |
|
72 | 11 | $criteria = $this->getCriteria(); |
|
73 | 11 | assert($criteria instanceof SearchCriteria); |
|
74 | 11 | $criteria->setModels($models); |
|
75 | 11 | } |
|
76 | |||
77 | public function stop($doStop = true) |
||
81 | |||
82 | 11 | protected function fetchData() |
|
83 | { |
||
84 | 11 | if ($this->isStopped) |
|
85 | { |
||
86 | return []; |
||
87 | } |
||
88 | 11 | $criteria = $this->configureFetch(); |
|
89 | |||
90 | 11 | $models = $criteria->getModels(); |
|
91 | |||
92 | 11 | if (!empty($models)) |
|
93 | { |
||
94 | 11 | $this->finder->setModels($models); |
|
95 | } |
||
96 | 11 | return $this->finder->findAll($criteria); |
|
97 | } |
||
98 | |||
99 | 11 | public function setCriteria($criteria) |
|
105 | |||
106 | 9 | public function getItemCount($refresh = false) |
|
110 | |||
111 | 11 | public function getTotalItemCount() |
|
112 | { |
||
134 | |||
135 | } |
||
136 |