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\Exception\DataGridException; |
12
|
|
|
use Ublaboo\DataGrid\DataModel; |
13
|
|
|
use Ublaboo\DataGrid\DataSource\IDataSource; |
14
|
|
|
|
15
|
|
|
trait TDataGridAggregationFunction |
16
|
1 |
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var IAggregationFunction[] |
20
|
|
|
*/ |
21
|
|
|
private $aggregationFunctions = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var IAggregationFunction|NULL |
25
|
|
|
*/ |
26
|
|
|
private $multipleAggregationFunction; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $key |
31
|
|
|
* @param IAggregationFunction $aggregationFunction |
32
|
|
|
* @return static |
33
|
|
|
* @throws DataGridException |
34
|
|
|
*/ |
35
|
|
|
public function addAggregationFunction($key, IAggregationFunction $aggregationFunction) |
36
|
|
|
{ |
37
|
|
|
if ($this->hasColumnsSummary()) { |
|
|
|
|
38
|
|
|
throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!($this->dataModel instanceof DataModel)) { |
|
|
|
|
42
|
|
|
throw new DataGridException('You have to set a data source first.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (isset($this->aggregationFunctions[$key])) { |
46
|
|
|
throw new DataGridException( |
47
|
|
|
"There is already a AggregationFunction defined on column {$key}" |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->multipleAggregationFunction instanceof MultipleAggregationFunction) { |
|
|
|
|
52
|
|
|
throw new DataGridException( |
53
|
|
|
"You can not use both AggregationFunctions and MultipleAggregationFunction" |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$this->aggregationFunctions[$key] = $aggregationFunction; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param IMultipleAggregationFunction $multipleAggregationFunction |
65
|
|
|
*/ |
66
|
|
|
public function setMultipleAggregationFunction(IMultipleAggregationFunction $multipleAggregationFunction) |
67
|
|
|
{ |
68
|
|
|
if ($this->hasColumnsSummary()) { |
|
|
|
|
69
|
|
|
throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (!empty($this->aggregationFunctions)) { |
73
|
|
|
throw new DataGridException( |
74
|
|
|
"You can not use both AggregationFunctions and MultipleAggregationFunction" |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->multipleAggregationFunction = $multipleAggregationFunction; |
|
|
|
|
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param IDataSource $dataSource |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function beforeDataModelFilter(IDataSource $dataSource) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
if ($this->multipleAggregationFunction) { |
91
|
|
|
if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
92
|
|
|
$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
99
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
100
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
|
|
|
|
101
|
|
|
} |
102
|
1 |
|
} |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param IDataSource $dataSource |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function afterDataModelFilter(IDataSource $dataSource) |
|
|
|
|
111
|
|
|
{ |
112
|
1 |
|
if ($this->multipleAggregationFunction) { |
113
|
|
|
if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
114
|
|
|
$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
121
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
122
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
|
|
|
|
123
|
|
|
} |
124
|
1 |
|
} |
125
|
1 |
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param IDataSource $dataSource |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
public function afterDataModelPaginated(IDataSource $dataSource) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
if ($this->multipleAggregationFunction) { |
135
|
|
|
if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
136
|
|
|
$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
143
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
144
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function hasSomeAggregationFunction() |
154
|
|
|
{ |
155
|
|
|
return !empty($this->aggregationFunctions) || $this->multipleAggregationFunction; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return IAggregationFunction[] |
161
|
|
|
*/ |
162
|
|
|
public function getAggregationFunctions() |
163
|
|
|
{ |
164
|
|
|
return $this->aggregationFunctions; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return MultipleAggregationFunction |
170
|
|
|
*/ |
171
|
|
|
public function getMultipleAggregationFunction() |
172
|
|
|
{ |
173
|
|
|
return $this->multipleAggregationFunction; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
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.