1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) 2015 ublaboo <[email protected]> |
5
|
|
|
* @author Pavel Janda <[email protected]> |
6
|
|
|
* @package Ublaboo |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ublaboo\DataGrid\AggregationFunction; |
10
|
|
|
|
11
|
|
|
use Ublaboo\DataGrid\DataModel; |
12
|
|
|
use Ublaboo\DataGrid\DataSource\IDataSource; |
13
|
|
|
use Ublaboo\DataGrid\Exception\DataGridException; |
14
|
|
|
|
15
|
1 |
|
trait TDataGridAggregationFunction |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var IAggregationFunction[] |
19
|
|
|
*/ |
20
|
|
|
private $aggregationFunctions = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var IAggregationFunction|NULL |
24
|
|
|
*/ |
25
|
|
|
private $multipleAggregationFunction; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $key |
30
|
|
|
* @param IAggregationFunction $aggregationFunction |
31
|
|
|
* @return static |
32
|
|
|
* @throws DataGridException |
33
|
|
|
*/ |
34
|
|
|
public function addAggregationFunction($key, IAggregationFunction $aggregationFunction) |
35
|
|
|
{ |
36
|
|
|
if ($this->hasColumnsSummary()) { |
|
|
|
|
37
|
|
|
throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!$this->dataModel instanceof DataModel) { |
|
|
|
|
41
|
|
|
throw new DataGridException('You have to set a data source first.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (isset($this->aggregationFunctions[$key])) { |
45
|
|
|
throw new DataGridException('There is already a AggregationFunction defined on column ' . $key); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if ($this->multipleAggregationFunction instanceof MultipleAggregationFunction) { |
|
|
|
|
49
|
|
|
throw new DataGridException('You can not use both AggregationFunctions and MultipleAggregationFunction'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->aggregationFunctions[$key] = $aggregationFunction; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param IMultipleAggregationFunction $multipleAggregationFunction |
60
|
|
|
* @throws DataGridException |
61
|
|
|
* @return static |
62
|
|
|
*/ |
63
|
|
|
public function setMultipleAggregationFunction(IMultipleAggregationFunction $multipleAggregationFunction) |
64
|
|
|
{ |
65
|
|
|
if ($this->hasColumnsSummary()) { |
|
|
|
|
66
|
|
|
throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!empty($this->aggregationFunctions)) { |
70
|
|
|
throw new DataGridException('You can not use both AggregationFunctions and MultipleAggregationFunction'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->multipleAggregationFunction = $multipleAggregationFunction; |
|
|
|
|
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param IDataSource $dataSource |
81
|
|
|
* @throws DataGridException |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function beforeDataModelFilter(IDataSource $dataSource) |
|
|
|
|
85
|
|
|
{ |
86
|
1 |
|
if (!$this->hasSomeAggregationFunction()) { |
87
|
1 |
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (!$dataSource instanceof IAggregatable) { |
91
|
|
|
throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($this->multipleAggregationFunction) { |
95
|
|
|
if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
96
|
|
|
$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
103
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
104
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param IDataSource $dataSource |
112
|
|
|
* @throws DataGridException |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
View Code Duplication |
public function afterDataModelFilter(IDataSource $dataSource) |
|
|
|
|
116
|
|
|
{ |
117
|
1 |
|
if (!$this->hasSomeAggregationFunction()) { |
118
|
1 |
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!$dataSource instanceof IAggregatable) { |
122
|
|
|
throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($this->multipleAggregationFunction) { |
126
|
|
|
if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
127
|
|
|
$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
134
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
135
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param IDataSource $dataSource |
143
|
|
|
* @throws DataGridException |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function afterDataModelPaginated(IDataSource $dataSource) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
if (!$this->hasSomeAggregationFunction()) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!$dataSource instanceof IAggregatable) { |
153
|
|
|
throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($this->multipleAggregationFunction) { |
157
|
|
|
if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
158
|
|
|
$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
165
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
166
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function hasSomeAggregationFunction() |
176
|
|
|
{ |
177
|
1 |
|
return !empty($this->aggregationFunctions) || $this->multipleAggregationFunction; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return IAggregationFunction[] |
183
|
|
|
*/ |
184
|
|
|
public function getAggregationFunctions() |
185
|
|
|
{ |
186
|
|
|
return $this->aggregationFunctions; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return MultipleAggregationFunction |
192
|
|
|
*/ |
193
|
|
|
public function getMultipleAggregationFunction() |
194
|
|
|
{ |
195
|
|
|
return $this->multipleAggregationFunction; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.