|
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
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var IAggregationFunction[] |
|
20
|
|
|
*/ |
|
21
|
|
|
private $aggregationFunctions = []; |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $key |
|
26
|
|
|
* @param IAggregationFunction $aggregationFunction |
|
27
|
|
|
* @return static |
|
28
|
|
|
*/ |
|
29
|
|
|
public function addAggregationFunction($key, IAggregationFunction $aggregationFunction) |
|
30
|
|
|
{ |
|
31
|
|
|
if (!($this->dataModel instanceof DataModel)) { |
|
|
|
|
|
|
32
|
|
|
throw new DataGridException('You have to set a data source first.'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (isset($this->aggregationFunctions[$key])) { |
|
36
|
|
|
throw new DataGridException( |
|
37
|
|
|
"There is already a AggregationFunction defined on column {$key}" |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->aggregationFunctions[$key] = $aggregationFunction; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param IDataSource $dataSource |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
View Code Duplication |
public function beforeDataModelFilter(IDataSource $dataSource) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
|
54
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) { |
|
55
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param IDataSource $dataSource |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function afterDataModelFilter(IDataSource $dataSource) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
|
68
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) { |
|
69
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param IDataSource $dataSource |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
public function afterDataModelPaginated(IDataSource $dataSource) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($this->aggregationFunctions as $aggregationFunction) { |
|
82
|
|
|
if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) { |
|
83
|
|
|
$dataSource->processAggregation([$aggregationFunction, 'processDataSource']); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function hasSomeAggregationFunction() |
|
93
|
|
|
{ |
|
94
|
|
|
return !empty($this->aggregationFunctions); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return IAggregationFunction[] |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getAggregationFunctions() |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->aggregationFunctions; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: