|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
-
-
- namespace yii\data;
-
- use yii\helpers\ArrayHelper;
-
-
- class ArrayDataProvider extends BaseDataProvider
- {
-
-
- public $key;
-
-
- public $allModels;
-
-
- public $modelClass;
-
-
-
-
- protected function prepareModels()
- {
- if (($models = $this->allModels) === null) {
- return [];
- }
-
- if (($sort = $this->getSort()) !== false) {
- $models = $this->sortModels($models, $sort);
- }
-
- if (($pagination = $this->getPagination()) !== false) {
- $pagination->totalCount = $this->getTotalCount();
-
- if ($pagination->getPageSize() > 0) {
- $models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
- }
- }
-
- return $models;
- }
-
-
-
- protected function prepareKeys($models)
- {
- if ($this->key !== null) {
- $keys = [];
- foreach ($models as $model) {
- if (is_string($this->key)) {
- $keys[] = $model[$this->key];
- } else {
- $keys[] = call_user_func($this->key, $model);
- }
- }
-
- return $keys;
- } else {
- return array_keys($models);
- }
- }
-
-
-
- protected function prepareTotalCount()
- {
- return count($this->allModels);
- }
-
-
-
- protected function sortModels($models, $sort)
- {
- $orders = $sort->getOrders();
- if (!empty($orders)) {
- ArrayHelper::multisort($models, array_keys($orders), array_values($orders));
- }
-
- return $models;
- }
- }
|