Passed
Push — develop ( d633c2...348aa2 )
by nguereza
04:37
created

Where   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 29
eloc 60
c 2
b 0
f 0
dl 0
loc 311
rs 10

25 Methods

Rating   Name   Duplication   Size   Complexity  
A neq() 0 3 1
A lte() 0 3 1
A isNotNull() 0 3 1
A in() 0 3 1
A lt() 0 3 1
A init() 0 9 2
A notBetween() 0 3 1
A __construct() 0 6 1
A is() 0 3 1
A isNull() 0 3 1
A between() 0 3 1
A __clone() 0 7 2
A nop() 0 5 1
A like() 0 3 1
A notIn() 0 3 1
A eq() 0 3 1
A isNot() 0 3 1
A gte() 0 3 1
A gt() 0 3 1
A addInCondition() 0 5 1
A notLike() 0 3 1
A addLikeCondition() 0 10 1
A addBetweenCondition() 0 11 1
A addCondition() 0 18 3
A addNullCondition() 0 5 1
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file Where.php
33
 *
34
 *  The class for where query statement
35
 *
36
 *  @package    Platine\Database\Query
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database\Query;
48
49
use Closure;
50
51
/**
52
 * Class Where
53
 * @package Platine\Database\Query
54
 */
55
class Where
56
{
57
58
    /**
59
     * @var string|Expression
60
     */
61
    protected $column;
62
63
    /**
64
     * @var string
65
     */
66
    protected string $separator;
67
68
    /**
69
     * The Query statement instance
70
     * @var QueryStatement
71
     */
72
    protected QueryStatement $queryStatement;
73
74
    /**
75
     * The Where statement instance
76
     * @var WhereStatement
77
     */
78
    protected WhereStatement $whereStatement;
79
80
    /**
81
     * Where constructor.
82
     * @param WhereStatement $whereStatement
83
     * @param QueryStatement $queryStatement
84
     */
85
    public function __construct(
86
        WhereStatement $whereStatement,
87
        QueryStatement $queryStatement
88
    ) {
89
        $this->queryStatement = $queryStatement;
90
        $this->whereStatement = $whereStatement;
91
    }
92
93
    /**
94
     * @param string|Expression|Closure $column
95
     * @param string $separator
96
     * @return self
97
     */
98
    public function init($column, string $separator): self
99
    {
100
        if ($column instanceof Closure) {
101
            $column = Expression::fromClosure($column);
102
        }
103
        $this->column = $column;
104
        $this->separator = $separator;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param mixed $value
111
     * @param bool $isColumn
112
     * @return WhereStatement|Select|Delete|Update
113
     */
114
    public function is($value, bool $isColumn = false): WhereStatement
115
    {
116
        return $this->addCondition($value, '=', $isColumn);
117
    }
118
119
    /**
120
     * @param mixed $value
121
     * @param bool $isColumn
122
     * @return WhereStatement|Select|Delete|Update
123
     */
124
    public function eq($value, bool $isColumn = false): WhereStatement
125
    {
126
        return $this->is($value, $isColumn);
127
    }
128
129
    /**
130
     * @param mixed $value
131
     * @param bool $isColumn
132
     * @return WhereStatement|Select|Delete|Update
133
     */
134
    public function isNot($value, bool $isColumn = false): WhereStatement
135
    {
136
        return $this->addCondition($value, '!=', $isColumn);
137
    }
138
139
    /**
140
     * @param mixed $value
141
     * @param bool $isColumn
142
     * @return WhereStatement|Select|Delete|Update
143
     */
144
    public function neq($value, bool $isColumn = false): WhereStatement
145
    {
146
        return $this->isNot($value, $isColumn);
147
    }
148
149
    /**
150
     * @param mixed $value
151
     * @param bool $isColumn
152
     * @return WhereStatement|Select|Delete|Update
153
     */
154
    public function lt($value, bool $isColumn = false): WhereStatement
155
    {
156
        return $this->addCondition($value, '<', $isColumn);
157
    }
158
159
    /**
160
     * @param mixed $value
161
     * @param bool $isColumn
162
     * @return WhereStatement|Select|Delete|Update
163
     */
164
    public function gt($value, bool $isColumn = false): WhereStatement
165
    {
166
        return $this->addCondition($value, '>', $isColumn);
167
    }
168
169
    /**
170
     * @param mixed $value
171
     * @param bool $isColumn
172
     * @return WhereStatement|Select|Delete|Update
173
     */
174
    public function lte($value, bool $isColumn = false): WhereStatement
175
    {
176
        return $this->addCondition($value, '<=', $isColumn);
177
    }
178
179
    /**
180
     * @param mixed $value
181
     * @param bool $isColumn
182
     * @return WhereStatement|Select|Delete|Update
183
     */
184
    public function gte($value, bool $isColumn = false): WhereStatement
185
    {
186
        return $this->addCondition($value, '>=', $isColumn);
187
    }
188
189
    /**
190
     * @param mixed $value1
191
     * @param mixed $value2
192
     * @return WhereStatement|Select|Delete|Update
193
     */
194
    public function between($value1, $value2): WhereStatement
195
    {
196
        return $this->addBetweenCondition($value1, $value2, false);
197
    }
198
199
    /**
200
     * @param mixed $value1
201
     * @param mixed $value2
202
     * @return WhereStatement|Select|Delete|Update
203
     */
204
    public function notBetween($value1, $value2): WhereStatement
205
    {
206
        return $this->addBetweenCondition($value1, $value2, true);
207
    }
208
209
    /**
210
     * @param mixed $value
211
     * @return WhereStatement|Select|Delete|Update
212
     */
213
    public function like($value): WhereStatement
214
    {
215
        return $this->addLikeCondition($value, false);
216
    }
217
218
    /**
219
     * @param mixed $value
220
     * @return WhereStatement|Select|Delete|Update
221
     */
222
    public function notLike($value): WhereStatement
223
    {
224
        return $this->addLikeCondition($value, true);
225
    }
226
227
    /**
228
     * @param array<int, mixed>|Closure $value
229
     * @return WhereStatement
230
     */
231
    public function in($value): WhereStatement
232
    {
233
        return $this->addInCondition($value, false);
234
    }
235
236
    /**
237
     * @param array<int, mixed>|Closure $value
238
     * @return WhereStatement
239
     */
240
    public function notIn($value): WhereStatement
241
    {
242
        return $this->addInCondition($value, true);
243
    }
244
245
    /**
246
     * @return WhereStatement|Select|Delete|Update
247
     */
248
    public function isNull(): WhereStatement
249
    {
250
        return $this->addNullCondition(false);
251
    }
252
253
    /**
254
     * @return WhereStatement|Select|Delete|Update
255
     */
256
    public function isNotNull(): WhereStatement
257
    {
258
        return $this->addNullCondition(true);
259
    }
260
261
    /**
262
     * @return WhereStatement|Select|Delete|Update
263
     */
264
    public function nop(): WhereStatement
265
    {
266
        $this->queryStatement->addWhereNop($this->column, $this->separator);
267
268
        return $this->whereStatement;
269
    }
270
271
    /**
272
     * @inheritDoc
273
     */
274
    public function __clone()
275
    {
276
        if ($this->column instanceof Expression) {
277
            $this->column = clone $this->column;
278
        }
279
        $this->queryStatement = clone $this->queryStatement;
280
        $this->whereStatement = new WhereStatement($this->queryStatement);
281
    }
282
283
    /**
284
     * @param mixed $value
285
     * @param string $operator
286
     * @param bool $isColumn
287
     * @return WhereStatement|Select|Delete|Update
288
     */
289
    protected function addCondition(
290
        $value,
291
        string $operator,
292
        bool $isColumn = false
293
    ): WhereStatement {
294
        if ($isColumn && is_string($value)) {
295
            $value = function (Expression $expr) use ($value) {
296
                return $expr->column($value);
297
            };
298
        }
299
        $this->queryStatement->addWhere(
300
            $this->column,
301
            $value,
302
            $operator,
303
            $this->separator
304
        );
305
306
        return $this->whereStatement;
307
    }
308
309
    /**
310
     * @param mixed $value1
311
     * @param mixed $value2
312
     * @param bool $not
313
     * @return WhereStatement|Select|Delete|update
314
     */
315
    protected function addBetweenCondition($value1, $value2, bool $not): WhereStatement
316
    {
317
        $this->queryStatement->addWhereBetween(
318
            $this->column,
319
            $value1,
320
            $value2,
321
            $this->separator,
322
            $not
323
        );
324
325
        return $this->whereStatement;
326
    }
327
328
    /**
329
     * @param string $pattern
330
     * @param bool $not
331
     * @return WhereStatement|Select|Delete|update
332
     */
333
    protected function addLikeCondition(string $pattern, bool $not): WhereStatement
334
    {
335
        $this->queryStatement->addWhereLike(
336
            $this->column,
337
            $pattern,
338
            $this->separator,
339
            $not
340
        );
341
342
        return $this->whereStatement;
343
    }
344
345
    /**
346
     * @param mixed $value
347
     * @param bool $not
348
     * @return WhereStatement|Select|Delete|update
349
     */
350
    protected function addInCondition($value, bool $not): WhereStatement
351
    {
352
        $this->queryStatement->addWhereIn($this->column, $value, $this->separator, $not);
353
354
        return $this->whereStatement;
355
    }
356
357
    /**
358
     * @param bool $not
359
     * @return WhereStatement|Select|Delete|Update
360
     */
361
    protected function addNullCondition(bool $not): WhereStatement
362
    {
363
        $this->queryStatement->addWhereNull($this->column, $this->separator, $not);
364
365
        return $this->whereStatement;
366
    }
367
}
368