Having::addCondition()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 2
nop 3
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 Having.php
33
 *
34
 *  The class for Havin 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   https://www.platine-php.com
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 Having
53
 * @package Platine\Database\Query
54
 */
55
class Having
56
{
57
    /**
58
     * @var string|Expression
59
     */
60
    protected string|Expression $aggregate;
61
62
    /**
63
     * @var string
64
     */
65
    protected string $separator;
66
67
    /**
68
     * Having constructor.
69
     * @param QueryStatement $queryStatement
70
     */
71
    public function __construct(protected QueryStatement $queryStatement)
72
    {
73
    }
74
75
    /**
76
     * @param string|Expression|Closure $aggregate
77
     * @param string $separator
78
     * @return self
79
     */
80
    public function init(string|Expression|Closure $aggregate, string $separator): self
81
    {
82
        if ($aggregate instanceof Closure) {
83
            $aggregate = Expression::fromClosure($aggregate);
84
        }
85
        $this->aggregate = $aggregate;
86
        $this->separator = $separator;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param mixed $value
93
     * @param bool $isColumn
94
     * @return void
95
     */
96
    public function is(mixed $value, bool $isColumn = false): void
97
    {
98
        $this->addCondition($value, '=', $isColumn);
99
    }
100
101
    /**
102
     * @param mixed $value
103
     * @param bool $isColumn
104
     * @return void
105
     */
106
    public function eq(mixed $value, bool $isColumn = false): void
107
    {
108
        $this->is($value, $isColumn);
109
    }
110
111
    /**
112
     * @param mixed $value
113
     * @param bool $isColumn
114
     * @return void
115
     */
116
    public function isNot(mixed $value, bool $isColumn = false): void
117
    {
118
        $this->addCondition($value, '!=', $isColumn);
119
    }
120
121
    /**
122
     * @param mixed $value
123
     * @param bool $isColumn
124
     * @return void
125
     */
126
    public function neq(mixed $value, bool $isColumn = false): void
127
    {
128
        $this->isNot($value, $isColumn);
129
    }
130
131
    /**
132
     * @param mixed $value
133
     * @param bool $isColumn
134
     * @return void
135
     */
136
    public function lt(mixed $value, bool $isColumn = false): void
137
    {
138
        $this->addCondition($value, '<', $isColumn);
139
    }
140
141
    /**
142
     * @param mixed $value
143
     * @param bool $isColumn
144
     * @return void
145
     */
146
    public function gt(mixed $value, bool $isColumn = false): void
147
    {
148
        $this->addCondition($value, '>', $isColumn);
149
    }
150
151
    /**
152
     * @param mixed $value
153
     * @param bool $isColumn
154
     * @return void
155
     */
156
    public function lte(mixed $value, bool $isColumn = false): void
157
    {
158
        $this->addCondition($value, '<=', $isColumn);
159
    }
160
161
    /**
162
     * @param mixed $value
163
     * @param bool $isColumn
164
     * @return void
165
     */
166
    public function gte(mixed $value, bool $isColumn = false): void
167
    {
168
        $this->addCondition($value, '>=', $isColumn);
169
    }
170
171
    /**
172
     * @param mixed $value1
173
     * @param mixed $value2
174
     * @return void
175
     */
176
    public function between(mixed $value1, mixed $value2): void
177
    {
178
        $this->queryStatement->addHavingBetween(
179
            $this->aggregate,
180
            $value1,
181
            $value2,
182
            $this->separator,
183
            false
184
        );
185
    }
186
187
    /**
188
     * @param mixed $value1
189
     * @param mixed $value2
190
     * @return void
191
     */
192
    public function notBetween(mixed $value1, mixed $value2): void
193
    {
194
        $this->queryStatement->addHavingBetween(
195
            $this->aggregate,
196
            $value1,
197
            $value2,
198
            $this->separator,
199
            true
200
        );
201
    }
202
203
    /**
204
     * @param array<int, mixed>|Closure $value
205
     * @return void
206
     */
207
    public function in(array|Closure $value): void
208
    {
209
        $this->queryStatement->addHavingIn(
210
            $this->aggregate,
211
            $value,
212
            $this->separator,
213
            false
214
        );
215
    }
216
217
    /**
218
     * @param array<int, mixed>|Closure $value
219
     * @return void
220
     */
221
    public function notIn(array|Closure $value): void
222
    {
223
        $this->queryStatement->addHavingIn(
224
            $this->aggregate,
225
            $value,
226
            $this->separator,
227
            true
228
        );
229
    }
230
231
    /**
232
     * @inheritDoc
233
     */
234
    public function __clone(): void
235
    {
236
        if ($this->aggregate instanceof Expression) {
237
            $this->aggregate = clone $this->aggregate;
238
        }
239
        $this->queryStatement = clone $this->queryStatement;
240
    }
241
242
    /**
243
     * @param mixed $value
244
     * @param string $operator
245
     * @param bool $isColumn
246
     * @return void
247
     */
248
    protected function addCondition(mixed $value, string $operator, bool $isColumn): void
249
    {
250
        if ($isColumn && is_string($value)) {
251
            $expr = new Expression();
252
            $value = $expr->column($value);
253
        }
254
        $this->queryStatement->addHaving(
255
            $this->aggregate,
256
            $value,
257
            $operator,
258
            $this->separator
259
        );
260
    }
261
}
262