1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
5
|
|
|
* @author Martin Procházka <[email protected]> |
6
|
|
|
* @package Ublaboo |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\DataSource; |
10
|
|
|
|
11
|
|
|
use Doctrine\Common\Collections\Collection; |
12
|
|
|
use Doctrine\Common\Collections\Criteria; |
13
|
|
|
use Ublaboo\DataGrid\Column\ColumnAggregationFunction; |
14
|
|
|
use Ublaboo\DataGrid\Filter; |
15
|
|
|
use Ublaboo\DataGrid\Utils\Sorting; |
16
|
|
|
|
17
|
|
|
final class DoctrineCollectionDataSource extends FilterableDataSource implements IDataSource |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Collection |
22
|
|
|
*/ |
23
|
|
|
private $data_source; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $primary_key; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Criteria |
32
|
|
|
*/ |
33
|
|
|
private $criteria; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $aggregations = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Collection $collection |
42
|
|
|
* @param string $primary_key |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Collection $collection, $primary_key) |
45
|
|
|
{ |
46
|
|
|
$this->criteria = Criteria::create(); |
47
|
|
|
$this->data_source = $collection; |
48
|
|
|
$this->primary_key = $primary_key; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Collection |
54
|
|
|
*/ |
55
|
|
|
private function getFilteredCollection() |
56
|
|
|
{ |
57
|
|
|
return $this->data_source->matching($this->criteria); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/******************************************************************************** |
62
|
|
|
* IDataSource implementation * |
63
|
|
|
********************************************************************************/ |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get count of data |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function getCount() |
71
|
|
|
{ |
72
|
|
|
return $this->getFilteredCollection()->count(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the data |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getData() |
81
|
|
|
{ |
82
|
|
|
return $this->getFilteredCollection()->toArray(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Filter data - get one row |
88
|
|
|
* @param array $condition |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
|
|
public function filterOne(array $condition) |
92
|
|
|
{ |
93
|
|
|
foreach ($condition as $column => $value) { |
94
|
|
|
$expr = Criteria::expr()->eq($column, $value); |
95
|
|
|
$this->criteria->andWhere($expr); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Filter by date |
104
|
|
|
* @param Filter\FilterDate $filter |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
public function applyFilterDate(Filter\FilterDate $filter) |
108
|
|
|
{ |
109
|
|
|
foreach ($filter->getCondition() as $column => $value) { |
110
|
|
|
$date = \DateTime::createFromFormat($filter->getPhpFormat(), $value) |
111
|
|
|
->format('Y-m-d H:i:s'); |
112
|
|
|
|
113
|
|
|
$from = Criteria::expr()->gte($filter->getColumn(), $date->format('Y-m-d 00:00:00')); |
|
|
|
|
114
|
|
|
$to = Criteria::expr()->lte($filter->getColumn(), $date->format('Y-m-d 23:59:59')); |
|
|
|
|
115
|
|
|
|
116
|
|
|
$this->criteria->andWhere($from)->andWhere($to); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Filter by date range |
123
|
|
|
* @param Filter\FilterDateRange $filter |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function applyFilterDateRange(Filter\FilterDateRange $filter) |
127
|
|
|
{ |
128
|
|
|
$values = $conditions[$filter->getColumn()]; |
|
|
|
|
129
|
|
|
|
130
|
|
View Code Duplication |
if ($value_from = $values['from']) { |
|
|
|
|
131
|
|
|
$date_from = \DateTime::createFromFormat($filter->getPhpFormat(), $value_from) |
132
|
|
|
->setTime(0, 0, 0) |
133
|
|
|
->format('Y-m-d H:i:s'); |
134
|
|
|
|
135
|
|
|
$expr = Criteria::expr()->gte($filter->getColumn(), $date_from); |
|
|
|
|
136
|
|
|
$this->criteria->andWhere($expr); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
if ($value_to = $values['to']) { |
|
|
|
|
140
|
|
|
$date_to = \DateTime::createFromFormat($filter->getPhpFormat(), $value_to) |
141
|
|
|
->setTime(23, 59, 59) |
142
|
|
|
->format('Y-m-d H:i:s'); |
143
|
|
|
|
144
|
|
|
$expr = Criteria::expr()->lte($filter->getColumn(), $date_to); |
|
|
|
|
145
|
|
|
$this->criteria->andWhere($expr); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Filter by range |
152
|
|
|
* @param Filter\FilterRange $filter |
153
|
|
|
* @return void |
154
|
|
|
*/ |
155
|
|
|
public function applyFilterRange(Filter\FilterRange $filter) |
156
|
|
|
{ |
157
|
|
|
$values = $conditions[$filter->getColumn()]; |
|
|
|
|
158
|
|
|
|
159
|
|
|
if ($value_from = $values['from']) { |
160
|
|
|
$expr = Criteria::expr()->gte($filter->getColumn(), $value_from); |
|
|
|
|
161
|
|
|
$this->criteria->andWhere($expr); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($value_to = $values['to']) { |
165
|
|
|
$expr = Criteria::expr()->lte($filter->getColumn(), $value_to); |
|
|
|
|
166
|
|
|
$this->criteria->andWhere($expr); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Filter by keyword |
173
|
|
|
* @param Filter\FilterText $filter |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function applyFilterText(Filter\FilterText $filter) |
177
|
|
|
{ |
178
|
|
|
$exprs = []; |
179
|
|
|
|
180
|
|
|
foreach ($filter->getCondition() as $column => $value) { |
181
|
|
|
if($filter->isExactSearch()) { |
182
|
|
|
$exprs[] = Criteria::expr()->eq($column, $value); |
183
|
|
|
continue; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
View Code Duplication |
if ($filter->hasSplitWordsSearch() === FALSE) { |
|
|
|
|
187
|
|
|
$words = [$value]; |
188
|
|
|
} else { |
189
|
|
|
$words = explode(' ', $value); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
foreach ($words as $word) { |
193
|
|
|
$exprs[] = Criteria::expr()->contains($column, $word); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$expr = call_user_func_array([Criteria::expr(), 'orX'], $exprs); |
198
|
|
|
$this->criteria->andWhere($expr); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Filter by multi select value |
204
|
|
|
* @param Filter\FilterMultiSelect $filter |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter) |
208
|
|
|
{ |
209
|
|
|
$values = $filter->getCondition()[$filter->getColumn()]; |
210
|
|
|
|
211
|
|
|
$expr = Criteria::expr()->in($filter->getColumn(), $values); |
|
|
|
|
212
|
|
|
$this->criteria->andWhere($expr); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Filter by select value |
218
|
|
|
* @param Filter\FilterSelect $filter |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
|
|
public function applyFilterSelect(Filter\FilterSelect $filter) |
222
|
|
|
{ |
223
|
|
|
foreach ($filter->getCondition() as $column => $value) { |
224
|
|
|
$expr = Criteria::expr()->eq($column, $value); |
225
|
|
|
$this->criteria->andWhere($expr); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Apply limit and offset on data |
232
|
|
|
* @param int $offset |
233
|
|
|
* @param int $limit |
234
|
|
|
* @return static |
235
|
|
|
*/ |
236
|
|
|
public function limit($offset, $limit) |
237
|
|
|
{ |
238
|
|
|
$this->criteria->setFirstResult($offset)->setMaxResults($limit); |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Sort data |
246
|
|
|
* @param Sorting $sorting |
247
|
|
|
* @return static |
248
|
|
|
*/ |
249
|
|
|
public function sort(Sorting $sorting) |
250
|
|
|
{ |
251
|
|
|
if (is_callable($sorting->getSortCallback())) { |
252
|
|
|
call_user_func( |
253
|
|
|
$sorting->getSortCallback(), |
254
|
|
|
$this->criteria, |
255
|
|
|
$sorting->getSort() |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
if ($sort = $sorting->getSort()) { |
262
|
|
|
$this->criteria->orderBy($sort); |
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$this->criteria->orderBy([$this->primary_key => 'ASC']); |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param string $aggregation_type |
273
|
|
|
* @param string $column |
274
|
|
|
* @return mixed |
275
|
|
|
*/ |
276
|
|
|
public function addAggregationColumn($aggregation_type, $column) |
277
|
|
|
{ |
278
|
|
|
$this->aggregations[$column] = $aggregation_type; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* get aggregation row |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
View Code Duplication |
public function getAggregationData() |
|
|
|
|
286
|
|
|
{ |
287
|
|
|
$result = []; |
288
|
|
|
foreach ($this->data_source as $row) { |
289
|
|
|
foreach ($this->aggregations as $column => $aggregation_type) { |
290
|
|
|
switch ($aggregation_type) { |
291
|
|
|
case ColumnAggregationFunction::AGGREGATION_TYPE_SUM: |
292
|
|
|
case ColumnAggregationFunction::AGGREGATION_TYPE_AVG: |
293
|
|
|
if (!isset($result[$column])) { |
294
|
|
|
$result[$column] = 0; |
295
|
|
|
} |
296
|
|
|
$result[$column] += $row[$column]; |
297
|
|
|
break; |
298
|
|
|
case ColumnAggregationFunction::AGGREGATION_TYPE_MIN: |
299
|
|
|
if (!isset($result[$column]) || $row[$column] < $result[$column]) { |
300
|
|
|
$result[$column] = $row[$column]; |
301
|
|
|
} |
302
|
|
|
break; |
303
|
|
|
case ColumnAggregationFunction::AGGREGATION_TYPE_MAX: |
304
|
|
|
if (!isset($result[$column]) || $row[$column] > $result[$column]) { |
305
|
|
|
$result[$column] = $row[$column]; |
306
|
|
|
} |
307
|
|
|
break; |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
foreach ($this->aggregations as $column => $aggregation_type) { |
313
|
|
|
if ($aggregation_type == ColumnAggregationFunction::AGGREGATION_TYPE_AVG) { |
314
|
|
|
$result[$column] = $result[$column] / $this->data_source->count(); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
return $result; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: