|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Data; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Data\Paginator\PaginatorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* DataProvider provides a base class that implements the {@see DataProviderInterface}. |
|
11
|
|
|
* |
|
12
|
|
|
* For more details and usage information on DataProvider, see the |
|
13
|
|
|
* [guide article on data providers](guide:output-data-providers). |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class DataProvider implements DataProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var int Number of data providers on the current page. Used to generate unique IDs. |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $counter = 0; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string an ID that uniquely identifies the data provider among all data providers. |
|
24
|
|
|
* Generated automatically the following way in case it is not set: |
|
25
|
|
|
* |
|
26
|
|
|
* - First data provider ID is empty. |
|
27
|
|
|
* - Second and all subsequent data provider IDs are: "dp-1", "dp-2", etc. |
|
28
|
|
|
*/ |
|
29
|
|
|
private string $id; |
|
30
|
|
|
private $sort = null; |
|
31
|
|
|
private ?PaginatorInterface $pagination = null; |
|
32
|
|
|
private array $keys = []; |
|
33
|
|
|
private array $models = []; |
|
34
|
|
|
private int $totalCount = 0; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
if ($this->id === null) { |
|
39
|
|
|
if (self::$counter > 0) { |
|
40
|
|
|
$this->id = 'dp-' . self::$counter; |
|
41
|
|
|
} |
|
42
|
|
|
self::$counter++; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Prepares the data models that will be made available in the current page. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array the available data models. |
|
50
|
|
|
*/ |
|
51
|
|
|
abstract protected function prepareModels(); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Prepares the keys associated with the currently available data models. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $value the available data models. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array the keys. |
|
59
|
|
|
*/ |
|
60
|
|
|
abstract protected function prepareKeys(array $models = []): array; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns a value indicating the total number of data models in this data provider. |
|
64
|
|
|
* |
|
65
|
|
|
* @return int total number of data models in this data provider. |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract protected function prepareTotalCount(): int; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Prepares the data models and keys. |
|
71
|
|
|
* |
|
72
|
|
|
* This method will prepare the data models and keys that can be retrieved via {@see getModels()} and |
|
73
|
|
|
* {@see getKeys()}. |
|
74
|
|
|
* |
|
75
|
|
|
* This method will be implicitly called by {@see getModels} and {@see getKeys()} if it has not been called |
|
76
|
|
|
* before. |
|
77
|
|
|
* |
|
78
|
|
|
* @param bool $forcePrepare whether to force data preparation even if it has been done before. |
|
79
|
|
|
*/ |
|
80
|
|
|
public function prepare(bool $forcePrepare = false): void |
|
81
|
|
|
{ |
|
82
|
|
|
if ($forcePrepare || $this->models === []) { |
|
83
|
|
|
$this->models = $this->prepareModels(); |
|
84
|
|
|
} |
|
85
|
|
|
if ($forcePrepare || $this->keys === []) { |
|
86
|
|
|
$this->keys = $this->prepareKeys($this->models); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the data models in the current page. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array the list of data models in the current page. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getModels(): array |
|
96
|
|
|
{ |
|
97
|
|
|
$this->prepare(); |
|
98
|
|
|
|
|
99
|
|
|
return $this->models; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Sets the data models in the current page. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $value the models in the current page. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function models(array $value): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->models = $value; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns the key values associated with the data models. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array the list of key values corresponding to {@see models}. Each data model in |
|
116
|
|
|
* {@see models} is uniquely identified by the corresponding key value in this array. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getKeys(): array |
|
119
|
|
|
{ |
|
120
|
|
|
$this->prepare(); |
|
121
|
|
|
|
|
122
|
|
|
return $this->keys; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets the key values associated with the data models. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $value the list of key values corresponding to {@see models}. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function keys(array $value): void |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$this->keys = $keys; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Returns the number of data models in the current page. |
|
137
|
|
|
* |
|
138
|
|
|
* @return int the number of data models in the current page. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getCount(): int |
|
141
|
|
|
{ |
|
142
|
|
|
return count($this->getModels()); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Returns the total number of data models. |
|
147
|
|
|
* |
|
148
|
|
|
* When {@see pagination} is false, this returns the same value as {@see count}. Otherwise, it will call |
|
149
|
|
|
* {@see prepareTotalCount()} to get the count. |
|
150
|
|
|
* |
|
151
|
|
|
* @return int total number of possible data models. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getTotalCount(): int |
|
154
|
|
|
{ |
|
155
|
|
|
if ($this->getPagination() === false) { |
|
|
|
|
|
|
156
|
|
|
return $this->getCount(); |
|
157
|
|
|
} elseif ($this->totalCount === 0) { |
|
158
|
|
|
$this->totalCount = $this->prepareTotalCount(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->totalCount; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sets the total number of data models. |
|
166
|
|
|
* |
|
167
|
|
|
* @param int $value the total number of data models. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function totalCount(int $value): void |
|
170
|
|
|
{ |
|
171
|
|
|
$this->totalCount = $value; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Returns the pagination object used by this data provider. |
|
176
|
|
|
* |
|
177
|
|
|
* @return PaginatorInterface the pagination object. If this is false, it means the pagination is disabled. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getPagination(): ?PaginatorInterface |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->pagination; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Sets the pagination for this data provider. |
|
186
|
|
|
* |
|
187
|
|
|
* @param PaginatorInterface $value the pagination to be used by this data provider. |
|
188
|
|
|
*/ |
|
189
|
|
|
public function pagination(PaginatorInterface $value): void |
|
190
|
|
|
{ |
|
191
|
|
|
$this->pagination = $value; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Returns the sorting object used by this data provider. |
|
196
|
|
|
* |
|
197
|
|
|
* @return array|bool the sorting object. If this is false, it means the sorting is disabled. |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getSort() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->sort; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Sets the sort definition for this data provider. |
|
206
|
|
|
* |
|
207
|
|
|
* @param array $value the sort definition to be used by this data provider. |
|
208
|
|
|
*/ |
|
209
|
|
|
public function withSort($value): void |
|
210
|
|
|
{ |
|
211
|
|
|
$this->sort = $value; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Refreshes the data provider. |
|
216
|
|
|
* |
|
217
|
|
|
* After calling this method, if {@see getModels()}, {@see getKeys()} or {@see getTotalCount()} is called again, |
|
218
|
|
|
* they will re-execute the query and return the latest data available. |
|
219
|
|
|
*/ |
|
220
|
|
|
public function refresh(): void |
|
221
|
|
|
{ |
|
222
|
|
|
$this->totalCount = null; |
|
223
|
|
|
$this->models = null; |
|
224
|
|
|
$this->keys = null; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.