Completed
Push — master ( f0e6de...68f317 )
by Hong
03:39
created

GroupByTrait::groupByDesc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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\GroupByInterface;
19
20
/**
21
 * GroupByTrait
22
 *
23
 * @package Phossa2\Query
24
 * @author  Hong Zhang <[email protected]>
25
 * @see     GroupByInterface
26
 * @version 2.0.0
27
 * @since   2.0.0 added
28
 */
29
trait GroupByTrait
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function groupBy($col)
35
    {
36
        // support multiple group by
37
        if (func_num_args() > 1) {
38
            $col = func_get_args();
39
        }
40
        return $this->realGroupBy($col);
41
    }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function groupByDesc($col)
47
    {
48
        return $this->realGroupBy($col, 'DESC');
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function groupByTpl(/*# string */ $template, $col)
55
    {
56
        return $this->realGroupBy(new Template($template, $col), '', true);
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function groupByRaw(/*# string */ $groupby)
63
    {
64
        return $this->realGroupBy($groupby, '', true);
65
    }
66
67
    /**
68
     * real group by
69
     * @param  string|string[]|Template $col column[s]
70
     * @param  string $suffix ''|ASC|DESC
71
     * @param  bool $rawMode
72
     * @return $this
73
     * @access protected
74
     */
75 View Code Duplication
    protected function realGroupBy(
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
76
        $col,
77
        /*# sting */ $suffix = '',
78
        /*# bool */ $rawMode = false)
79
    {
80
        if (is_array($col)) {
81
            $this->multipleGroupBy($col, $suffix);
82
        } else {
83
            $clause = &$this->getClause('GROUP BY');
84
            $part = [$col, $this->isRaw($col, $rawMode)];
85
            if (!empty($suffix)) {
86
                $part[] = $suffix;
87
            }
88
            $clause[] = $part;
89
        }
90
        return $this;
91
    }
92
93
    /**
94
     * Multitple groupbys
95
     *
96
     * @param  array $cols
97
     * @param  string $suffix
98
     * @access protected
99
     */
100
    protected function multipleGroupBy(array $cols, /*# string */ $suffix)
101
    {
102
        foreach ($cols as $col) {
103
            $this->realGroupBy($col, $suffix);
104
        }
105
    }
106
107
    /**
108
     * Build GROUP BY
109
     *
110
     * @param  string $prefix
111
     * @param  array $settings
112
     * @return string
113
     * @access protected
114
     */
115
    protected function buildGroupby(
116
        /*# string */ $prefix,
117
        array $settings
118
    )/*# : string */ {
119
        return $this->buildClause('GROUP BY', $prefix, $settings);
120
    }
121
122
    abstract protected function isRaw($str, /*# bool */ $rawMode)/*# : bool */;
123
    abstract protected function &getClause(/*# string */ $clauseName)/*# : array */;
124
    abstract protected function buildClause(
125
        /*# string */ $clauseName,
126
        /*# string */ $clausePrefix,
127
        array $settings,
128
        array $clauseParts = []
129
    )/*# string */;
130
}
131