1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Query |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Query\Traits\Clause; |
16
|
|
|
|
17
|
|
|
use Phossa2\Query\Misc\Template; |
18
|
|
|
use Phossa2\Query\Interfaces\Clause\GroupInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* GroupTrait |
22
|
|
|
* |
23
|
|
|
* @package Phossa2\Query |
24
|
|
|
* @author Hong Zhang <[email protected]> |
25
|
|
|
* @see GroupInterface |
26
|
|
|
* @version 2.0.0 |
27
|
|
|
* @since 2.0.0 added |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
trait GroupTrait |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
use AbstractTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
|
|
public function group($col) |
37
|
|
|
{ |
38
|
|
|
// support multiple group by |
39
|
|
|
if (func_num_args() > 1) { |
40
|
|
|
$col = func_get_args(); |
41
|
|
|
} |
42
|
|
|
return $this->realGroup($col); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function groupDesc($col) |
49
|
|
|
{ |
50
|
|
|
// support multiple group by |
51
|
|
|
if (func_num_args() > 1) { |
52
|
|
|
$col = func_get_args(); |
53
|
|
|
} |
54
|
|
|
return $this->realGroup($col, 'DESC'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function groupTpl(/*# string */ $template, $col, array $params = []) |
61
|
|
|
{ |
62
|
|
|
$template = $this->positionedParam($template, $params); |
63
|
|
|
return $this->realGroup(new Template($template, $col), '', true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
*/ |
69
|
|
|
public function groupRaw(/*# string */ $rawString, array $params = []) |
70
|
|
|
{ |
71
|
|
|
$rawString = $this->positionedParam($rawString, $params); |
72
|
|
|
return $this->realGroup($rawString, '', true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* real group by |
77
|
|
|
* @param string|string[]|Template $col column[s] |
78
|
|
|
* @param string $suffix ''|ASC|DESC |
79
|
|
|
* @param bool $rawMode |
80
|
|
|
* @return $this |
81
|
|
|
* @access protected |
82
|
|
|
*/ |
83
|
|
|
protected function realGroup( |
84
|
|
|
$col, |
85
|
|
|
/*# sting */ $suffix = '', |
86
|
|
|
/*# bool */ $rawMode = false |
87
|
|
|
) { |
88
|
|
|
|
89
|
|
|
if (is_array($col)) { |
90
|
|
|
$this->multipleGroup($col, $suffix); |
91
|
|
|
} else { |
92
|
|
|
$clause = &$this->getClause('GROUP BY'); |
93
|
|
|
$part = [$col, $this->isRaw($col, $rawMode)]; |
94
|
|
|
if (!empty($suffix)) { |
95
|
|
|
$part[] = $suffix; |
96
|
|
|
} |
97
|
|
|
$clause[] = $part; |
98
|
|
|
} |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Multitple groupbys |
104
|
|
|
* |
105
|
|
|
* @param array $cols |
106
|
|
|
* @param string $suffix |
107
|
|
|
* @access protected |
108
|
|
|
*/ |
109
|
|
|
protected function multipleGroup(array $cols, /*# string */ $suffix) |
110
|
|
|
{ |
111
|
|
|
foreach ($cols as $col) { |
112
|
|
|
$this->realGroup($col, $suffix); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Build GROUP BY |
118
|
|
|
* |
119
|
|
|
* @param string $prefix |
120
|
|
|
* @param array $settings |
121
|
|
|
* @return string |
122
|
|
|
* @access protected |
123
|
|
|
*/ |
124
|
|
|
protected function buildGroup( |
125
|
|
|
/*# string */ $prefix, |
126
|
|
|
array $settings |
127
|
|
|
)/*# : string */ { |
128
|
|
|
return $this->buildClause('GROUP BY', $prefix, $settings); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.