1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
namespace EngineWorks\Pivot; |
4
|
|
|
|
5
|
|
|
use EngineWorks\DBAL\DBAL; |
6
|
|
|
|
7
|
|
|
class Aggregate |
8
|
|
|
{ |
9
|
|
|
const COUNT = 'COUNT'; |
10
|
|
|
const SUM = 'SUM'; |
11
|
|
|
const AVG = 'AVG'; |
12
|
|
|
const MIN = 'MIN'; |
13
|
|
|
const MAX = 'MAX'; |
14
|
|
|
|
15
|
|
|
const ORDERNONE = ''; |
16
|
|
|
const ORDERASC = 'ASC'; |
17
|
|
|
const ORDERDESC = 'DESC'; |
18
|
|
|
|
19
|
|
|
private $fieldname; |
20
|
|
|
private $asname; |
21
|
|
|
private $caption; |
22
|
|
|
private $group; |
23
|
|
|
private $decimals; |
24
|
|
|
private $order; |
25
|
|
|
|
26
|
16 |
|
public function __construct( |
27
|
|
|
string $fieldname, |
28
|
|
|
string $asname, |
29
|
|
|
string $caption, |
30
|
|
|
string $group, |
31
|
|
|
int $decimals, |
32
|
|
|
string $order |
33
|
|
|
) { |
34
|
16 |
|
if (! $this->groupExists($group)) { |
35
|
1 |
|
throw new \InvalidArgumentException('Invalid value for group'); |
36
|
|
|
} |
37
|
15 |
|
if ($decimals < 0 || $decimals > 8) { |
38
|
1 |
|
throw new \InvalidArgumentException('Invalid value for decimals'); |
39
|
|
|
} |
40
|
14 |
|
if (! in_array($order, [static::ORDERNONE, static::ORDERASC, static::ORDERDESC])) { |
41
|
1 |
|
throw new \InvalidArgumentException('Invalid order value'); |
42
|
|
|
} |
43
|
13 |
|
$this->fieldname = $fieldname; |
44
|
13 |
|
$this->asname = $asname; |
45
|
13 |
|
$this->caption = $caption; |
46
|
13 |
|
$this->group = $group; |
47
|
13 |
|
$this->decimals = $decimals; |
48
|
13 |
|
$this->order = $order; |
49
|
13 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
public function getFieldname(): string |
55
|
|
|
{ |
56
|
1 |
|
return $this->fieldname; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
13 |
|
public function getAsname(): string |
63
|
|
|
{ |
64
|
13 |
|
return $this->asname; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function getCaption(): string |
71
|
|
|
{ |
72
|
1 |
|
return $this->caption; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
1 |
|
public function getGroup(): string |
79
|
|
|
{ |
80
|
1 |
|
return $this->group; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
10 |
|
public function getDecimals(): int |
87
|
|
|
{ |
88
|
10 |
|
return $this->decimals; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
10 |
|
public function getOrder(): string |
95
|
|
|
{ |
96
|
10 |
|
return $this->order; |
97
|
|
|
} |
98
|
|
|
|
99
|
12 |
|
public function asArray() : array |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
12 |
|
'fieldname' => $this->fieldname, |
103
|
12 |
|
'caption' => $this->caption, |
104
|
12 |
|
'group' => $this->group, |
105
|
12 |
|
'asname' => $this->asname, |
106
|
12 |
|
'decimals' => $this->decimals, |
107
|
12 |
|
'order' => $this->order, |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param DBAL $db |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
9 |
|
public function getSQL(DBAL $db) : string |
116
|
|
|
{ |
117
|
9 |
|
return $this->group |
118
|
9 |
|
. '(' . $db->sqlFieldEscape($this->fieldname) . ')' |
119
|
9 |
|
. ' AS ' . $db->sqlFieldEscape($this->asname); |
120
|
|
|
} |
121
|
|
|
|
122
|
18 |
|
public static function groupTypes() : array |
123
|
|
|
{ |
124
|
|
|
return [ |
125
|
18 |
|
static::COUNT, |
126
|
18 |
|
static::SUM, |
127
|
18 |
|
static::AVG, |
128
|
18 |
|
static::MIN, |
129
|
18 |
|
static::MAX, |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
17 |
|
public static function groupExists(string $group) : bool |
134
|
|
|
{ |
135
|
17 |
|
return in_array($group, static::groupTypes()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|