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\Interfaces\Clause\GroupByInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* GroupByTrait |
21
|
|
|
* |
22
|
|
|
* @package Phossa2\Query |
23
|
|
|
* @author Hong Zhang <[email protected]> |
24
|
|
|
* @see GroupByInterface |
25
|
|
|
* @version 2.0.0 |
26
|
|
|
* @since 2.0.0 added |
27
|
|
|
*/ |
28
|
|
|
trait GroupByTrait |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public function groupBy($col) |
34
|
|
|
{ |
35
|
|
|
return $this->realGroupBy($col); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
|
|
public function groupByTpl(/*# string */ $template, $col) |
42
|
|
|
{ |
43
|
|
|
return $this->realGroupBy($this->clauseTpl($template, $col), true); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function groupByRaw(/*# string */ $groupby) |
50
|
|
|
{ |
51
|
|
|
return $this->realGroupBy($groupby, true); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string|string[] $col column[s] |
56
|
|
|
* @param bool $rawMode |
57
|
|
|
* @return $this |
58
|
|
|
* @access protected |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
protected function realGroupBy($col, /*# bool */ $rawMode = false) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (is_array($col)) { |
63
|
|
|
$this->multipleGroupBy($col); |
64
|
|
|
} else { |
65
|
|
|
$clause = &$this->getClause('GROUP BY'); |
66
|
|
|
$clause[] = [$col, $this->isRaw($col, $rawMode)]; |
67
|
|
|
} |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Multitple groupbys |
73
|
|
|
* |
74
|
|
|
* @param array $cols |
75
|
|
|
* @access protected |
76
|
|
|
*/ |
77
|
|
|
protected function multipleGroupBy(array $cols) |
78
|
|
|
{ |
79
|
|
|
foreach ($cols as $col) { |
80
|
|
|
$this->realGroupBy($col); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Build GROUP BY |
86
|
|
|
* |
87
|
|
|
* @param array $settings |
88
|
|
|
* @return string |
89
|
|
|
* @access protected |
90
|
|
|
*/ |
91
|
|
|
protected function buildGroupby(array $settings)/*# : string */ |
92
|
|
|
{ |
93
|
|
|
$result = []; |
94
|
|
|
$clause = &$this->getClause('GROUP BY'); |
95
|
|
|
foreach ($clause as $grp) { |
96
|
|
|
$result[] = $this->quoteItem($grp[0], $grp[1]); |
97
|
|
|
} |
98
|
|
|
return $this->joinClause('GROUP BY', ',', $result, $settings); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */; |
102
|
|
|
abstract protected function clauseTpl(/*# string */ $template, $col)/*# : string */; |
103
|
|
|
abstract protected function &getClause(/*# string */ $clauseName)/*# : array */; |
104
|
|
|
abstract protected function quoteItem($item, /*# bool */ $rawMode = false)/*# : string */; |
105
|
|
|
abstract protected function joinClause( |
106
|
|
|
/*# : string */ $prefix, |
107
|
|
|
/*# : string */ $seperator, |
108
|
|
|
array $clause, |
109
|
|
|
array $settings |
110
|
|
|
)/*# : string */; |
111
|
|
|
} |
112
|
|
|
|
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.