Completed
Push — master ( 1db5f1...1fea5f )
by Beniamin
02:25
created

RelativeQueryBuilder::getSelectClauses()   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 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
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 UnderQuery 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\UnderQuery\Table;
13
14
use Phuria\UnderQuery\QueryBuilder\Clause as Clause;
15
use Phuria\UnderQuery\QueryBuilder\BuilderInterface;
16
use Phuria\UnderQuery\QueryBuilder\Clause\OrderByInterface;
17
use Phuria\UnderQuery\QueryBuilder\Clause\SetInterface;
18
use Phuria\UnderQuery\QueryBuilder\Clause\WhereInterface;
19
use Phuria\UnderQuery\Utils\RecursiveArgs;
20
21
/**
22
 * @author Beniamin Jonatan Šimko <[email protected]>
23
 */
24
class RelativeQueryBuilder implements
25
    Clause\GroupByInterface,
26
    Clause\HavingInterface,
27
    Clause\LimitInterface,
28
    Clause\OrderByInterface,
29
    Clause\SelectInterface,
30
    Clause\SetInterface,
31
    Clause\WhereInterface
32
{
33
    /**
34
     * @var BuilderInterface
35
     */
36
    private $wrappedBuilder;
37
38
    /**
39
     * @var TableInterface
40
     */
41
    private $wrappedTable;
42
43
    /**
44
     * @param BuilderInterface $builder
45
     * @param TableInterface   $table
46
     */
47 1
    public function __construct(BuilderInterface $builder, TableInterface $table)
48
    {
49 1
        $this->wrappedBuilder = $builder;
50 1
        $this->wrappedTable = $table;
51 1
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 1
    public function __destruct()
57
    {
58 1
        unset($this->wrappedBuilder, $this->wrappedTable);
59 1
    }
60
61
    /**
62
     * @param array $args
63
     *
64
     * @return array
65
     */
66 1
    public function replaceSelfReference(array $args)
67
    {
68
        return RecursiveArgs::map($args, function ($arg) {
69 1
            if (false === is_string($arg)) {
70
                return $arg;
71
            }
72
73 1
            return str_replace('@.', $this->wrappedBuilder->objectToString($this->wrappedTable) . '.', $arg);
74 1
        });
75
    }
76
77
    /**
78
     * @param callable $callback
79
     *
80
     * @return mixed
81
     */
82 1
    private function getQueryBuilder(callable $callback)
83
    {
84 1
        return $callback($this->wrappedBuilder);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 1
    public function addSelect($_)
91
    {
92 1
        $args = $this->replaceSelfReference(func_get_args());
93
        $this->getQueryBuilder(function (Clause\SelectInterface $qb) use ($args) {
94 1
            $qb->addSelect($args);
95 1
        });
96
97 1
        return $this;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 1
    public function getSelectClauses()
104
    {
105
        return $this->getQueryBuilder(function (Clause\SelectInterface $qb) {
106 1
            return $qb->getSelectClauses();
107 1
        });
108
    }
109
110
    /**
111
     * @inheritdoc
112
     */
113
    public function getGroupByClauses()
114
    {
115
        return $this->getQueryBuilder(function (Clause\GroupByInterface $qb) {
116
            return $qb->getGroupByClauses();
117
        });
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123
    public function isGroupByWithRollUp()
124
    {
125
        return $this->getQueryBuilder(function (Clause\GroupByInterface $qb) {
126
            return $qb->isGroupByWithRollUp();
127
        });
128
    }
129
130
    /**
131
     * @inheritdoc
132
     */
133
    public function addGroupBy($_)
134
    {
135
        $args = $this->replaceSelfReference(func_get_args());
136
        $this->getQueryBuilder(function (Clause\GroupByInterface $qb) use ($args) {
137
            $qb->addGroupBy($args);
138
        });
139
140
        return $this;
141
    }
142
143
    /**
144
     * @inheritdoc
145
     */
146
    public function setGroupByWithRollUp($groupByWithRollUp)
147
    {
148
        $this->getQueryBuilder(function (Clause\GroupByInterface $qb) use ($groupByWithRollUp) {
149
            $qb->setGroupByWithRollUp($groupByWithRollUp);
150
        });
151
152
        return $this;
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    public function getHavingClauses()
159
    {
160
        return $this->getQueryBuilder(function (Clause\HavingInterface $qb) {
161
            return $qb->getHavingClauses();
162
        });
163
    }
164
165
    /**
166
     * @inheritdoc
167
     */
168
    public function andHaving($_)
169
    {
170
        $args = $this->replaceSelfReference(func_get_args());
171
        $this->getQueryBuilder(function (Clause\HavingInterface $qb) use ($args) {
172
            $qb->andHaving($args);
173
        });
174
175
        return $this;
176
    }
177
178
    /**
179
     * @inheritdoc
180
     */
181
    public function getLimitClause()
182
    {
183
        return $this->getQueryBuilder(function (Clause\LimitInterface $qb) {
184
            return $qb->getLimitClause();
185
        });
186
    }
187
188
    /**
189
     * @inheritdoc
190
     */
191
    public function setLimit($limitClause)
192
    {
193
        $this->getQueryBuilder(function (Clause\LimitInterface $qb) use ($limitClause) {
194
            $qb->setLimit($limitClause);
195
        });
196
197
        return $this;
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203
    public function getOrderByClauses()
204
    {
205
        return $this->getQueryBuilder(function (Clause\OrderByInterface $qb) {
206
            return $qb->getOrderByClauses();
207
        });
208
    }
209
210
    /**
211
     * @inheritDoc
212
     */
213
    public function addOrderBy($_)
214
    {
215
        $args = $this->replaceSelfReference(func_get_args());
216
        $this->getQueryBuilder(function (Clause\OrderByInterface $qb) use ($args) {
217
            $qb->addOrderBy($args);
218
        });
219
220
        return $this;
221
    }
222
223
    /**
224
     * @inheritDoc
225
     */
226
    public function getSetClauses()
227
    {
228
        return $this->getQueryBuilder(function (Clause\SetInterface $qb) {
229
            return $qb->getSetClauses();
230
        });
231
    }
232
233
    /**
234
     * @inheritDoc
235
     */
236
    public function addSet($_)
237
    {
238
        $args = $this->replaceSelfReference(func_get_args());
239
        $this->getQueryBuilder(function (Clause\SetInterface $qb) use ($args) {
240
            $qb->addSet($args);
241
        });
242
243
        return $this;
244
    }
245
246
    /**
247
     * @inheritDoc
248
     */
249
    public function getWhereClauses()
250
    {
251
        return $this->getQueryBuilder(function (Clause\WhereInterface $qb) {
252
            return $qb->getWhereClauses();
253
        });
254
    }
255
256
    /**
257
     * @inheritDoc
258
     */
259
    public function andWhere($_)
260
    {
261
        $args = $this->replaceSelfReference(func_get_args());
262
        $this->getQueryBuilder(function (Clause\WhereInterface $qb) use ($args) {
263
            $qb->andWhere($args);
0 ignored issues
show
Documentation introduced by
$args is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
264
        });
265
266
        return $this;
267
    }
268
}