1 | <?php |
||
33 | abstract class BaseDataProvider extends Component implements DataProviderInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var string an ID that uniquely identifies the data provider among all data providers. |
||
37 | * You should set this property if the same page contains two or more different data providers. |
||
38 | * Otherwise, the [[pagination]] and [[sort]] may not work properly. |
||
39 | */ |
||
40 | public $id; |
||
41 | |||
42 | private $_sort; |
||
43 | private $_pagination; |
||
44 | private $_keys; |
||
45 | private $_models; |
||
46 | private $_totalCount; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Prepares the data models that will be made available in the current page. |
||
51 | * @return array the available data models |
||
52 | */ |
||
53 | abstract protected function prepareModels(); |
||
54 | |||
55 | /** |
||
56 | * Prepares the keys associated with the currently available data models. |
||
57 | * @param array $models the available data models |
||
58 | * @return array the keys |
||
59 | */ |
||
60 | abstract protected function prepareKeys($models); |
||
61 | |||
62 | /** |
||
63 | * Returns a value indicating the total number of data models in this data provider. |
||
64 | * @return int total number of data models in this data provider. |
||
65 | */ |
||
66 | abstract protected function prepareTotalCount(); |
||
67 | |||
68 | /** |
||
69 | * Prepares the data models and keys. |
||
70 | * |
||
71 | * This method will prepare the data models and keys that can be retrieved via |
||
72 | * [[getModels()]] and [[getKeys()]]. |
||
73 | * |
||
74 | * This method will be implicitly called by [[getModels()]] and [[getKeys()]] if it has not been called before. |
||
75 | * |
||
76 | * @param bool $forcePrepare whether to force data preparation even if it has been done before. |
||
77 | */ |
||
78 | 48 | public function prepare($forcePrepare = false) |
|
79 | { |
||
80 | 48 | if ($forcePrepare || $this->_models === null) { |
|
81 | 46 | $this->_models = $this->prepareModels(); |
|
82 | 46 | } |
|
83 | 48 | if ($forcePrepare || $this->_keys === null) { |
|
84 | 48 | $this->_keys = $this->prepareKeys($this->_models); |
|
85 | 48 | } |
|
86 | 48 | } |
|
87 | |||
88 | /** |
||
89 | * Returns the data models in the current page. |
||
90 | * @return array the list of data models in the current page. |
||
91 | */ |
||
92 | 47 | public function getModels() |
|
93 | { |
||
94 | 47 | $this->prepare(); |
|
95 | |||
96 | 47 | return $this->_models; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Sets the data models in the current page. |
||
101 | * @param array $models the models in the current page |
||
102 | */ |
||
103 | 21 | public function setModels($models) |
|
104 | 21 | { |
|
105 | 2 | $this->_models = $models; |
|
106 | 2 | } |
|
107 | |||
108 | /** |
||
109 | * Returns the key values associated with the data models. |
||
110 | * @return array the list of key values corresponding to [[models]]. Each data model in [[models]] |
||
111 | * is uniquely identified by the corresponding key value in this array. |
||
112 | */ |
||
113 | 23 | public function getKeys() |
|
114 | { |
||
115 | 23 | $this->prepare(); |
|
116 | |||
117 | 23 | return $this->_keys; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Sets the key values associated with the data models. |
||
122 | * @param array $keys the list of key values corresponding to [[models]]. |
||
123 | */ |
||
124 | public function setKeys($keys) |
||
125 | { |
||
126 | $this->_keys = $keys; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Returns the number of data models in the current page. |
||
131 | * @return int the number of data models in the current page. |
||
132 | */ |
||
133 | 13 | public function getCount() |
|
134 | { |
||
135 | 13 | return count($this->getModels()); |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * Returns the total number of data models. |
||
140 | * When [[pagination]] is false, this returns the same value as [[count]]. |
||
141 | * Otherwise, it will call [[prepareTotalCount()]] to get the count. |
||
142 | * @return int total number of possible data models. |
||
143 | */ |
||
144 | 46 | public function getTotalCount() |
|
145 | { |
||
146 | 46 | if ($this->getPagination() === false) { |
|
147 | return $this->getCount(); |
||
148 | 46 | } elseif ($this->_totalCount === null) { |
|
149 | 44 | $this->_totalCount = $this->prepareTotalCount(); |
|
150 | 44 | } |
|
151 | |||
152 | 46 | return $this->_totalCount; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Sets the total number of data models. |
||
157 | * @param int $value the total number of data models. |
||
158 | */ |
||
159 | 8 | public function setTotalCount($value) |
|
160 | { |
||
161 | 8 | $this->_totalCount = $value; |
|
162 | 8 | } |
|
163 | |||
164 | /** |
||
165 | * Returns the pagination object used by this data provider. |
||
166 | * Note that you should call [[prepare()]] or [[getModels()]] first to get correct values |
||
167 | * of [[Pagination::totalCount]] and [[Pagination::pageCount]]. |
||
168 | * @return Pagination|false the pagination object. If this is false, it means the pagination is disabled. |
||
169 | */ |
||
170 | 46 | public function getPagination() |
|
171 | { |
||
172 | 46 | if ($this->_pagination === null) { |
|
173 | 38 | $this->setPagination([]); |
|
174 | 38 | } |
|
175 | |||
176 | 46 | return $this->_pagination; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Sets the pagination for this data provider. |
||
181 | * @param array|Pagination|bool $value the pagination to be used by this data provider. |
||
182 | * This can be one of the following: |
||
183 | * |
||
184 | * - a configuration array for creating the pagination object. The "class" element defaults |
||
185 | * to 'yii\data\Pagination' |
||
186 | * - an instance of [[Pagination]] or its subclass |
||
187 | * - false, if pagination needs to be disabled. |
||
188 | * |
||
189 | * @throws InvalidParamException |
||
190 | */ |
||
191 | 40 | public function setPagination($value) |
|
192 | { |
||
193 | 40 | if (is_array($value)) { |
|
194 | 40 | $config = ['class' => Pagination::className()]; |
|
195 | 40 | if ($this->id !== null) { |
|
196 | $config['pageParam'] = $this->id . '-page'; |
||
197 | $config['pageSizeParam'] = $this->id . '-per-page'; |
||
198 | } |
||
199 | 40 | $this->_pagination = Yii::createObject(array_merge($config, $value)); |
|
200 | 40 | } elseif ($value instanceof Pagination || $value === false) { |
|
201 | $this->_pagination = $value; |
||
202 | } else { |
||
203 | throw new InvalidParamException('Only Pagination instance, configuration array or false is allowed.'); |
||
204 | } |
||
205 | 40 | } |
|
206 | |||
207 | /** |
||
208 | * Returns the sorting object used by this data provider. |
||
209 | * @return Sort|bool the sorting object. If this is false, it means the sorting is disabled. |
||
210 | */ |
||
211 | 48 | public function getSort() |
|
219 | |||
220 | /** |
||
221 | * Sets the sort definition for this data provider. |
||
222 | * @param array|Sort|bool $value the sort definition to be used by this data provider. |
||
223 | * This can be one of the following: |
||
224 | * |
||
225 | * - a configuration array for creating the sort definition object. The "class" element defaults |
||
226 | * to 'yii\data\Sort' |
||
227 | * - an instance of [[Sort]] or its subclass |
||
228 | * - false, if sorting needs to be disabled. |
||
229 | * |
||
230 | * @throws InvalidParamException |
||
231 | */ |
||
232 | 48 | public function setSort($value) |
|
246 | |||
247 | /** |
||
248 | * Refreshes the data provider. |
||
249 | * After calling this method, if [[getModels()]], [[getKeys()]] or [[getTotalCount()]] is called again, |
||
250 | * they will re-execute the query and return the latest data available. |
||
251 | */ |
||
252 | 6 | public function refresh() |
|
258 | } |
||
259 |