| Total Complexity | 8 |
| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | trait GroupByBuilder { |
||
| 8 | use AbstractDB; |
||
| 9 | |||
| 10 | /** @var string[] */ |
||
| 11 | private array $groupBy = []; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @param mixed ...$args |
||
| 15 | * @return $this |
||
| 16 | */ |
||
| 17 | public function groupBy(...$args) { |
||
| 18 | foreach($args as $expression) { |
||
| 19 | if(is_array($expression)) { |
||
| 20 | if(!count($expression)) { |
||
| 21 | continue; |
||
| 22 | } |
||
| 23 | $expression = $this->quoteExpr($expression[0], array_slice($expression, 1)); |
||
| 24 | } |
||
| 25 | $this->groupBy[] = $expression; |
||
| 26 | } |
||
| 27 | |||
| 28 | return $this; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $query |
||
| 33 | * @return string |
||
| 34 | */ |
||
| 35 | protected function buildGroups(string $query): string { |
||
| 36 | if(!count($this->groupBy)) { |
||
| 37 | return $query; |
||
| 38 | } |
||
| 39 | $query .= "GROUP BY\n"; |
||
| 40 | $arr = []; |
||
| 41 | foreach($this->groupBy as $expression) { |
||
| 42 | $arr[] = "\t{$expression}"; |
||
| 43 | } |
||
| 44 | |||
| 45 | return $query . implode(",\n", $arr) . "\n"; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $expression |
||
| 50 | * @param array<int, null|int|float|string|array<int, string>|Builder\DBExpr|Builder\Select> $arguments |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | protected function quoteExpr(string $expression, array $arguments): string { |
||
| 55 | } |
||
| 56 | } |
||
| 57 |