GroupByTrait::buildGroupByQueryPart()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits;
4
5
use Janisbiz\LightOrm\Dms\MySQL\Enum\ConditionEnum;
6
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException;
7
8
trait GroupByTrait
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $groupBy = [];
14
15
    /**
16
     * @param array|string $groupBy
17
     *
18
     * @return $this
19
     * @throws QueryBuilderException
20
     */
21
    public function groupBy($groupBy)
22
    {
23
        if (empty($groupBy)) {
24
            throw new QueryBuilderException('You must pass $groupBy to groupBy method!');
25
        }
26
27
        if (!\is_array($groupBy)) {
28
            $groupBy = [$groupBy];
29
        }
30
31
        $this->groupBy = \array_merge($this->groupBy, $groupBy);
32
33
        return $this;
34
    }
35
36
    /**
37
     * @return null|string
38
     */
39
    protected function buildGroupByQueryPart(): ?string
40
    {
41
        return empty($this->groupBy)
42
            ? null
43
            : \sprintf('%s %s', ConditionEnum::GROUP_BY, \implode(', ', $this->groupBy))
44
        ;
45
    }
46
}
47