1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link https://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license https://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace yii\data; |
10
|
|
|
|
11
|
|
|
use yii\helpers\ArrayHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ArrayDataProvider implements a data provider based on a data array. |
15
|
|
|
* |
16
|
|
|
* The [[allModels]] property contains all data models that may be sorted and/or paginated. |
17
|
|
|
* ArrayDataProvider will provide the data after sorting and/or pagination. |
18
|
|
|
* You may configure the [[sort]] and [[pagination]] properties to |
19
|
|
|
* customize the sorting and pagination behaviors. |
20
|
|
|
* |
21
|
|
|
* Elements in the [[allModels]] array may be either objects (e.g. model objects) |
22
|
|
|
* or associative arrays (e.g. query results of DAO). |
23
|
|
|
* Make sure to set the [[key]] property to the name of the field that uniquely |
24
|
|
|
* identifies a data record or false if you do not have such a field. |
25
|
|
|
* |
26
|
|
|
* Compared to [[ActiveDataProvider]], ArrayDataProvider could be less efficient |
27
|
|
|
* because it needs to have [[allModels]] ready. |
28
|
|
|
* |
29
|
|
|
* ArrayDataProvider may be used in the following way: |
30
|
|
|
* |
31
|
|
|
* ```php |
32
|
|
|
* $query = new Query; |
33
|
|
|
* $provider = new ArrayDataProvider([ |
34
|
|
|
* 'allModels' => $query->from('post')->all(), |
35
|
|
|
* 'sort' => [ |
36
|
|
|
* 'attributes' => ['id', 'username', 'email'], |
37
|
|
|
* ], |
38
|
|
|
* 'pagination' => [ |
39
|
|
|
* 'pageSize' => 10, |
40
|
|
|
* ], |
41
|
|
|
* ]); |
42
|
|
|
* // get the posts in the current page |
43
|
|
|
* $posts = $provider->getModels(); |
44
|
|
|
* ``` |
45
|
|
|
* |
46
|
|
|
* Note: if you want to use the sorting feature, you must configure the [[sort]] property |
47
|
|
|
* so that the provider knows which columns can be sorted. |
48
|
|
|
* |
49
|
|
|
* For more details and usage information on ArrayDataProvider, see the [guide article on data providers](guide:output-data-providers). |
50
|
|
|
* |
51
|
|
|
* @author Qiang Xue <[email protected]> |
52
|
|
|
* @since 2.0 |
53
|
|
|
*/ |
54
|
|
|
class ArrayDataProvider extends BaseDataProvider |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @var string|callable|null the column that is used as the key of the data models. |
58
|
|
|
* This can be either a column name, or a callable that returns the key value of a given data model. |
59
|
|
|
* If this is not set, the index of the [[models]] array will be used. |
60
|
|
|
* @see getKeys() |
61
|
|
|
*/ |
62
|
|
|
public $key; |
63
|
|
|
/** |
64
|
|
|
* @var array the data that is not paginated or sorted. When pagination is enabled, |
65
|
|
|
* this property usually contains more elements than [[models]]. |
66
|
|
|
* The array elements must use zero-based integer keys. |
67
|
|
|
*/ |
68
|
|
|
public $allModels; |
69
|
|
|
/** |
70
|
|
|
* @var string the name of the [[\yii\base\Model|Model]] class that will be represented. |
71
|
|
|
* This property is used to get columns' names. |
72
|
|
|
* @since 2.0.9 |
73
|
|
|
*/ |
74
|
|
|
public $modelClass; |
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
45 |
|
protected function prepareModels() |
81
|
|
|
{ |
82
|
45 |
|
if (($models = $this->allModels) === null) { |
|
|
|
|
83
|
|
|
return []; |
84
|
|
|
} |
85
|
|
|
|
86
|
45 |
|
if (($sort = $this->getSort()) !== false) { |
87
|
44 |
|
$models = $this->sortModels($models, $sort); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
45 |
|
$pagination = $this->getPagination(); |
91
|
45 |
|
if ($pagination !== false && $pagination->getPageSize() > 0) { |
92
|
44 |
|
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true); |
93
|
|
|
} |
94
|
45 |
|
return $models; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
45 |
|
protected function prepareKeys($models) |
101
|
|
|
{ |
102
|
45 |
|
if ($this->key !== null) { |
103
|
|
|
$keys = []; |
104
|
|
|
foreach ($models as $model) { |
105
|
|
|
if (is_string($this->key)) { |
106
|
|
|
$keys[] = $model[$this->key]; |
107
|
|
|
} else { |
108
|
|
|
$keys[] = call_user_func($this->key, $model); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $keys; |
113
|
|
|
} |
114
|
|
|
|
115
|
45 |
|
return array_keys($models); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
29 |
|
protected function prepareTotalCount() |
122
|
|
|
{ |
123
|
29 |
|
return is_array($this->allModels) ? count($this->allModels) : 0; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sorts the data models according to the given sort definition. |
128
|
|
|
* @param array $models the models to be sorted |
129
|
|
|
* @param Sort $sort the sort definition |
130
|
|
|
* @return array the sorted data models |
131
|
|
|
*/ |
132
|
44 |
|
protected function sortModels($models, $sort) |
133
|
|
|
{ |
134
|
44 |
|
$orders = $sort->getOrders(); |
135
|
44 |
|
if (!empty($orders)) { |
136
|
4 |
|
ArrayHelper::multisort($models, array_keys($orders), array_values($orders), $sort->sortFlags); |
137
|
|
|
} |
138
|
|
|
|
139
|
44 |
|
return $models; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|