Issues (59)

src/Builders/Group.php (1 issue)

1
<?php
2
/**
3
 * Columns groups
4
 * User: moyo
5
 * Date: 2018/5/13
6
 * Time: 11:18 AM
7
 */
8
9
namespace Carno\Database\SQL\Builders;
10
11
use Carno\Database\SQL\Builder;
12
13
trait Group
14
{
15
    /**
16
     * @var array
17
     */
18
    private $bGroups = [];
19
20
    /**
21
     * @return string
22
     */
23
    protected function gGroups() : string
24
    {
25
        return $this->bGroups ? sprintf(' GROUP BY %s', implode(',', $this->bGroups)) : '';
26
    }
27
28
    /**
29
     * @param mixed ...$groups
30
     * @return Builder
31
     */
32
    public function group(...$groups) : Builder
33
    {
34
        foreach ($groups as $group) {
35
            if (is_array($group)) {
36
                switch (count($group)) {
37
                    case 1:
38
                        // group([expr])
39
                        $this->bGroups[] = $group[0];
40
                        break;
41
                    case 2:
42
                        // group([column, order])
43
                        $this->bGroups[] = sprintf('`%s` %s', $group[0], strtoupper($group[1]));
44
                        break;
45
                }
46
            } else {
47
                // group(column1, column2)
48
                $this->bGroups[] = sprintf('`%s`', $group);
49
            }
50
        }
51
52
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Carno\Database\SQL\Builders\Group which includes types incompatible with the type-hinted return Carno\Database\SQL\Builder.
Loading history...
53
    }
54
}
55