Where::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 10
cc 3
nc 2
nop 5
crap 3
1
<?php
2
3
namespace Rougin\Windstorm\Doctrine;
4
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Rougin\Windstorm\Doctrine\Builder\Expression;
7
use Rougin\Windstorm\WhereInterface;
8
9
/**
10
 * Where Query
11
 *
12
 * @package Windstorm
13
 * @author  Rougin Gutib <[email protected]>
14
 */
15
class Where implements WhereInterface
16
{
17
    /**
18
     * @var \Doctrine\DBAL\Query\QueryBuilder
19
     */
20
    protected $builder;
21
22
    /**
23
     * @var \Rougin\Windstorm\Doctrine\Builder\Expression
24
     */
25
    protected $expression;
26
27
    /**
28
     * @var string
29
     */
30
    protected $key;
31
32
    /**
33
     * @var string
34
     */
35
    protected $method = 'where';
36
37
    /**
38
     * @var \Rougin\Windstorm\Doctrine\Query
39
     */
40
    protected $query;
41
42
    /**
43
     * @var string
44
     */
45
    protected $type;
46
47
    /**
48
     * Initializes the query instance.
49
     *
50
     * @param \Rougin\Windstorm\Doctrine\Query  $query
51
     * @param \Doctrine\DBAL\Query\QueryBuilder $builder
52
     * @param string                            $key
53
     * @param string|null                       $initial
54
     * @param string                            $type
55
     */
56 123
    public function __construct(Query $query, QueryBuilder $builder, $key, $initial, $type = '')
57
    {
58 123
        $this->builder = $builder;
59
60 123
        $this->expression = new Expression;
61
62 123
        $this->query = $query;
63
64 123
        if ($initial && strpos($key, '.') === false)
65 82
        {
66 12
            $key = $initial . '.' . $key;
67 8
        }
68
69 123
        $this->key = $key;
70
71 123
        $this->type = $type;
72 123
    }
73
74
    /**
75
     * Generates an equality comparison.
76
     *
77
     * @param  mixed $value
78
     * @return \Rougin\Windstorm\QueryInterface
79
     */
80 84
    public function equals($value)
81
    {
82 84
        list($key, $type) = $this->parameter($value);
83
84 84
        $expr = $this->expression->eq($this->key, $key);
85
86 84
        $this->builder->$type($expr);
87
88 84
        return $this->query->builder($this->builder);
89
    }
90
91
    /**
92
     * Generates an non-equality comparison.
93
     *
94
     * @param  mixed $value
95
     * @return \Rougin\Windstorm\QueryInterface
96
     */
97 3
    public function notEqualTo($value)
98
    {
99 3
        list($key, $type) = $this->parameter($value);
100
101 3
        $expr = $this->expression->neq($this->key, $key);
102
103 3
        $this->builder->$type($expr);
104
105 3
        return $this->query->builder($this->builder);
106
    }
107
108
    /**
109
     * Generates a greater-than comparison.
110
     *
111
     * @param  mixed $value
112
     * @return \Rougin\Windstorm\QueryInterface
113
     */
114 3
    public function greaterThan($value)
115
    {
116 3
        list($key, $type) = $this->parameter($value);
117
118 3
        $expr = $this->expression->gt($this->key, $key);
119
120 3
        $this->builder->$type($expr);
121
122 3
        return $this->query->builder($this->builder);
123
    }
124
125
    /**
126
     * Generates a greater-than or an equality comparison.
127
     *
128
     * @param  mixed $value
129
     * @return \Rougin\Windstorm\QueryInterface
130
     */
131 3
    public function greaterThanOrEqualTo($value)
132
    {
133 3
        list($key, $type) = $this->parameter($value);
134
135 3
        $expr = $this->expression->gte($this->key, $key);
136
137 3
        $this->builder->$type($expr);
138
139 3
        return $this->query->builder($this->builder);
140
    }
141
142
    /**
143
     * Generates an IN () query comparison.
144
     *
145
     * @param  array $values
146
     * @return \Rougin\Windstorm\QueryInterface
147
     */
148 3
    public function in(array $values)
149
    {
150 3
        list($keys, $type) = $this->parameters($values);
151
152 3
        $expr = $this->expression->in($this->key, $keys);
153
154 3
        $this->builder->$type($expr);
155
156 3
        return $this->query->builder($this->builder);
157
    }
158
159
    /**
160
     * Generates an NOT IN () query comparison.
161
     *
162
     * @param  array $values
163
     * @return \Rougin\Windstorm\QueryInterface
164
     */
165 3
    public function notIn(array $values)
166
    {
167 3
        list($keys, $type) = $this->parameters($values);
168
169 3
        $expr = $this->expression->notIn($this->key, $keys);
170
171 3
        $this->builder->$type($expr);
172
173 3
        return $this->query->builder($this->builder);
174
    }
175
176
    /**
177
     * Generates a false comparison.
178
     *
179
     * @return \Rougin\Windstorm\QueryInterface
180
     */
181 3
    public function isFalse()
182
    {
183 3
        $type = strtolower($this->type) . $this->method;
184
185 3
        $expr = $this->expression->eq($this->key, 0);
186
187 3
        $this->builder->$type($expr);
188
189 3
        return $this->query->builder($this->builder);
190
    }
191
192
    /**
193
     * Generates an IS NOT NULL query comparison.
194
     *
195
     * @return \Rougin\Windstorm\QueryInterface
196
     */
197 3
    public function isNotNull()
198
    {
199 3
        $type = strtolower($this->type) . $this->method;
200
201 3
        $expr = $this->expression->isNotNull($this->key);
202
203 3
        $this->builder->$type($expr);
204
205 3
        return $this->query->builder($this->builder);
206
    }
207
208
    /**
209
     * Generates an IS NULL query comparison.
210
     *
211
     * @return \Rougin\Windstorm\QueryInterface
212
     */
213 3
    public function isNull()
214
    {
215 3
        $expr = $this->expression->isNull($this->key);
216
217 3
        $type = strtolower($this->type) . $this->method;
218
219 3
        $this->builder->$type($expr);
220
221 3
        return $this->query->builder($this->builder);
222
    }
223
224
    /**
225
     * Generates a true comparison.
226
     *
227
     * @return \Rougin\Windstorm\QueryInterface
228
     */
229 27
    public function isTrue()
230
    {
231 27
        $expr = $this->expression->eq($this->key, 1);
232
233 27
        $type = strtolower($this->type) . $this->method;
234
235 27
        $this->builder->$type($expr);
236
237 27
        return $this->query->builder($this->builder);
238
    }
239
240
    /**
241
     * Generates a less-than comparison.
242
     *
243
     * @param  mixed $value
244
     * @return \Rougin\Windstorm\QueryInterface
245
     */
246 3
    public function lessThan($value)
247
    {
248 3
        list($key, $type) = $this->parameter($value);
249
250 3
        $expr = $this->expression->lt($this->key, $key);
251
252 3
        $this->builder->$type($expr);
253
254 3
        return $this->query->builder($this->builder);
255
    }
256
257
    /**
258
     * Generates a less-than or an equality comparison.
259
     *
260
     * @param  mixed $value
261
     * @return \Rougin\Windstorm\QueryInterface
262
     */
263 3
    public function lessThanOrEqualTo($value)
264
    {
265 3
        list($key, $type) = $this->parameter($value);
266
267 3
        $expr = $this->expression->lte($this->key, $key);
268
269 3
        $this->builder->$type($expr);
270
271 3
        return $this->query->builder($this->builder);
272
    }
273
274
    /**
275
     * Generates a LIKE query comparison.
276
     *
277
     * @param  string $value
278
     * @return \Rougin\Windstorm\QueryInterface
279
     */
280 3
    public function like($value)
281
    {
282 3
        list($key, $type) = $this->parameter($value);
283
284 3
        $expr = $this->expression->like($this->key, $key);
285
286 3
        $this->builder->$type($expr);
287
288 3
        return $this->query->builder($this->builder);
289
    }
290
291
    /**
292
     * Generates a NOT LIKE query comparison.
293
     *
294
     * @param  string $value
295
     * @return \Rougin\Windstorm\QueryInterface
296
     */
297 3
    public function notLike($value)
298
    {
299 3
        list($key, $type) = $this->parameter($value);
300
301 3
        $expr = $this->expression->notLike($this->key, $key);
302
303 3
        $this->builder->$type($expr);
304
305 3
        return $this->query->builder($this->builder);
306
    }
307
308
    /**
309
     * Sets a parameter placeholder in the builder.
310
     *
311
     * @param  mixed       $value
312
     * @param  string|null $suffix
313
     * @return array
314
     */
315 111
    protected function parameter($value, $suffix = null)
316
    {
317 111
        $key = $this->key[0] === ':' ? $this->key : ':' . $this->key;
318
319 111
        $key = $suffix !== null ? $key . '_' . $suffix : $key;
320
321 111
        $key = strtolower(str_replace('.', '_', (string) $key));
322
323 111
        $this->builder->setParameter($key, $value, gettype($value));
324
325 111
        return array($key, strtolower($this->type) . $this->method);
326
    }
327
328
    /**
329
     * Sets an array of parameter placeholders in the builder.
330
     *
331
     * @param  array $values
332
     * @return array
333
     */
334 6
    protected function parameters(array $values)
335
    {
336 6
        list($keys, $type) = array(array(), '');
337
338 6
        $values = array_values((array) $values);
339
340 6
        foreach ($values as $index => $value)
341
        {
342 6
            list($key, $type) = $this->parameter($value, $index);
343
344 6
            array_push($keys, $key);
345 4
        }
346
347 6
        return array((array) $keys, (string) $type);
348
    }
349
}
350