Completed
Push — master ( f3cdf4...360009 )
by Beniamin
04:07
created

QueryClauses::addGroupBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\QueryBuilder;
13
14
use Phuria\QueryBuilder\Expression\ExpressionCollection;
15
use Phuria\QueryBuilder\Expression\ExpressionInterface;
16
use Phuria\QueryBuilder\Expression\QueryClauseExpression as QueryExpr;
17
use Phuria\QueryBuilder\Expression\SelectClauseExpression;
18
19
/**
20
 * @author Beniamin Jonatan Šimko <[email protected]>
21
 */
22
class QueryClauses
23
{
24
    const QUERY_SELECT = 1;
25
    const QUERY_UPDATE = 2;
26
27
    /**
28
     * @var array $selectClauses
29
     */
30
    private $selectClauses = [];
31
32
    /**
33
     * @var array $whereClauses
34
     */
35
    private $whereClauses = [];
36
37
    /**
38
     * @var array $orderByClauses
39
     */
40
    private $orderByClauses = [];
41
42
    /**
43
     * @var array $setClauses
44
     */
45
    private $setClauses = [];
46
47
    /**
48
     * @var array $groupByClauses
49
     */
50
    private $groupByClauses = [];
51
52
    /**
53
     * @var array $havingClauses
54
     */
55
    private $havingClauses = [];
56
57
    /**
58
     * @var array $hints
59
     */
60
    private $hints = [];
61
62
    /**
63
     * @return int
64
     */
65 22
    public function guessQueryType()
66
    {
67 22
        if ($this->selectClauses) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->selectClauses of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
68 21
            return static::QUERY_SELECT;
69
        }
70
71 1
        return static::QUERY_UPDATE;
72
    }
73
74
    /**
75
     * @return $this
76
     */
77 21
    public function addSelect()
78
    {
79 21
        $this->selectClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
80
81 21
        return $this;
82
    }
83
84
    /**
85
     * @return $this
86
     */
87 3
    public function andWhere()
88
    {
89 3
        $this->whereClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
90
91 3
        return $this;
92
    }
93
94
    /**
95
     * @return $this
96
     */
97 1
    public function andHaving()
98
    {
99 1
        $this->havingClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
100
101 1
        return $this;
102
    }
103
104
    /**
105
     * @return $this
106
     */
107 1
    public function addOrderBy()
108
    {
109 1
        $this->orderByClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * @return $this
116
     */
117 1
    public function addSet()
118
    {
119 1
        $this->setClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
120
121 1
        return $this;
122
    }
123
124
    /**
125
     * @return $this
126
     */
127 2
    public function addGroupBy()
128
    {
129 2
        $this->groupByClauses[] = ExprNormalizer::normalizeExpression(func_get_args());
130
131 2
        return $this;
132
    }
133
134
    /**
135
     * @return $this
136
     */
137 1
    public function addHint()
138
    {
139 1
        $this->hints[] = ExprNormalizer::normalizeExpression(func_get_args());
140
141 1
        return $this;
142
    }
143
144
    /**
145
     * @return ExpressionInterface
146
     */
147 21
    public function getSelectExpression()
148
    {
149 21
        return new SelectClauseExpression(
150 21
            new ExpressionCollection($this->hints, ' '),
151 21
            new ExpressionCollection($this->selectClauses, ', ')
152 21
        );
153
    }
154
155
    /**
156
     * @return ExpressionInterface
157
     */
158 21
    public function getWhereExpression()
159
    {
160 21
        return new QueryExpr(
161 21
            QueryExpr::CLAUSE_WHERE,
162 21
            new ExpressionCollection($this->whereClauses, ' AND ')
163 21
        );
164
    }
165
166
    /**
167
     * @return ExpressionInterface
168
     */
169 21
    public function getOrderByExpression()
170
    {
171 21
        return new QueryExpr(
172 21
            QueryExpr::CLAUSE_ORDER_BY,
173 21
            new ExpressionCollection($this->orderByClauses, ', ')
174 21
        );
175
    }
176
177
    /**
178
     * @return ExpressionInterface
179
     */
180 1
    public function getSetExpression()
181
    {
182 1
        return new QueryExpr(
183 1
            QueryExpr::CLAUSE_SET,
184 1
            new ExpressionCollection($this->setClauses, ', ')
185 1
        );
186
    }
187
188
    /**
189
     * @return ExpressionInterface
190
     */
191 21
    public function getGroupByExpression()
192
    {
193 21
        return new QueryExpr(
194 21
            QueryExpr::CLAUSE_GROUP_BY,
195 21
            new ExpressionCollection($this->groupByClauses, ', ')
196 21
        );
197
    }
198
199
    /**
200
     * @return ExpressionInterface
201
     */
202 21
    public function getHavingExpression()
203
    {
204 21
        return new QueryExpr(
205 21
            QueryExpr::CLAUSE_HAVING,
206 21
            new ExpressionCollection($this->havingClauses, ' AND ')
207 21
        );
208
    }
209
}