Query   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 75
c 1
b 0
f 0
dl 0
loc 275
rs 10
wmc 30

15 Methods

Rating   Name   Duplication   Size   Complexity  
A onlyDeleted() 0 5 1
A groupBy() 0 9 2
A orHaving() 0 5 1
A __clone() 0 4 1
A distinct() 0 5 1
A with() 0 10 2
A withDeleted() 0 5 1
A having() 0 5 1
A __construct() 0 4 1
A offset() 0 5 1
B getWithAttributes() 0 52 11
A getHavingStatement() 0 3 1
A select() 0 11 3
A orderBy() 0 11 2
A limit() 0 5 1
1
<?php
2
3
/**
4
 * Platine ORM
5
 *
6
 * Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine ORM
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 Query.php
33
 *
34
 *  The Query class
35
 *
36
 *  @package    Platine\Orm\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\Orm\Query;
48
49
use Closure;
50
use Platine\Database\Query\BaseStatement;
51
use Platine\Database\Query\ColumnExpression;
52
use Platine\Database\Query\Expression;
53
use Platine\Database\Query\HavingStatement;
54
use Platine\Database\Query\QueryStatement;
55
56
/**
57
 * @class Query
58
 * @package Platine\Orm\Query
59
 */
60
class Query extends BaseStatement
61
{
62
    /**
63
     *
64
     * @var HavingStatement
65
     */
66
    protected HavingStatement $havingStatement;
67
68
    /**
69
     * The list of relation data to load with the query
70
     * @var array<int, string>|array<string, Closure>
71
     */
72
    protected array $with = [];
73
74
    /**
75
     * Whether need load relation data immediately
76
     * @var bool
77
     */
78
    protected bool $immediate = false;
79
80
    /**
81
     * Whether return the data with the deleted
82
     * @var bool
83
     */
84
    protected bool $withSoftDeleted = false;
85
86
    /**
87
     * Whether to return only deleted data
88
     * @var bool
89
     */
90
    protected bool $onlySoftDeleted = false;
91
92
93
    /**
94
     * Create new instance
95
     * @param QueryStatement|null $queryStatement
96
     */
97
    public function __construct(QueryStatement $queryStatement = null)
98
    {
99
        parent::__construct($queryStatement);
100
        $this->havingStatement = new HavingStatement($this->queryStatement);
101
    }
102
103
    /**
104
     * Clone the object
105
     */
106
    public function __clone(): void
107
    {
108
        parent::__clone();
109
        $this->havingStatement = new HavingStatement($this->queryStatement);
110
    }
111
112
    /**
113
     *
114
     * @param bool $value
115
     * @return $this
116
     */
117
    public function withDeleted(bool $value = true): self
118
    {
119
        $this->withSoftDeleted = $value;
120
121
        return $this;
122
    }
123
124
    /**
125
     *
126
     * @param bool $value
127
     * @return $this
128
     */
129
    public function onlyDeleted(bool $value = true): self
130
    {
131
        $this->onlySoftDeleted = $this->withSoftDeleted = $value;
132
133
        return $this;
134
    }
135
136
    /**
137
     *
138
     * @param string|array<int, string>|array<string, Closure> $value
139
     * @param bool $immediate
140
     * @return $this
141
     */
142
    public function with(string|array $value, bool $immediate = false): self
143
    {
144
        if (!is_array($value)) {
0 ignored issues
show
introduced by
The condition is_array($value) is always true.
Loading history...
145
            $value = [$value];
146
        }
147
148
        $this->with = $value;
149
        $this->immediate = $immediate;
150
151
        return $this;
152
    }
153
154
    /**
155
     *
156
     * @param bool $distinct
157
     * @return $this
158
     */
159
    public function distinct(bool $distinct = true): self
160
    {
161
        $this->getQueryStatement()->setDistinct($distinct);
162
163
        return $this;
164
    }
165
166
    /**
167
     *
168
     * @param string|Expression|Closure|string[]|Expression[]|Closure[] $columns
169
     * @return $this
170
     */
171
    public function groupBy(string|Expression|Closure|array $columns): self
172
    {
173
        if (!is_array($columns)) {
0 ignored issues
show
introduced by
The condition is_array($columns) is always true.
Loading history...
174
            $columns = [$columns];
175
        }
176
177
        $this->getQueryStatement()->addGroupBy($columns);
178
179
        return $this;
180
    }
181
182
    /**
183
     *
184
     * @param string|Expression|Closure $column
185
     * @param Closure|null $value
186
     * @return $this
187
     */
188
    public function having(string|Expression|Closure $column, Closure $value = null): self
189
    {
190
        $this->getHavingStatement()->having($column, $value);
191
192
        return $this;
193
    }
194
195
    /**
196
     *
197
     * @param string|Expression|Closure $column
198
     * @param Closure|null $value
199
     * @return $this
200
     */
201
    public function orHaving(string|Expression|Closure $column, Closure $value = null): self
202
    {
203
        $this->getHavingStatement()->orHaving($column, $value);
204
205
        return $this;
206
    }
207
208
    /**
209
     *
210
     * @param string|Closure|Expression|string[]|Expression[]|Closure[] $columns
211
     * @param string $order
212
     * @return $this
213
     */
214
    public function orderBy(
215
        string|Closure|Expression|array $columns,
216
        string $order = 'ASC'
217
    ): self {
218
        if (!is_array($columns)) {
0 ignored issues
show
introduced by
The condition is_array($columns) is always true.
Loading history...
219
            $columns = [$columns];
220
        }
221
222
        $this->getQueryStatement()->addOrder($columns, $order);
223
224
        return $this;
225
    }
226
227
    /**
228
     *
229
     * @param int $value
230
     * @return $this
231
     */
232
    public function limit(int $value): self
233
    {
234
        $this->getQueryStatement()->setLimit($value);
235
236
        return $this;
237
    }
238
239
    /**
240
     *
241
     * @param int $value
242
     * @return $this
243
     */
244
    public function offset(int $value): self
245
    {
246
        $this->getQueryStatement()->setOffset($value);
247
248
        return $this;
249
    }
250
251
    /**
252
     *
253
     * @return HavingStatement
254
     */
255
    protected function getHavingStatement(): HavingStatement
256
    {
257
        return $this->havingStatement;
258
    }
259
260
    /**
261
     *
262
     * @param string|string[]|Expression|Expression[]|Closure|Closure[] $columns
263
     * @return void
264
     */
265
    protected function select(string|Expression|Closure|array $columns = []): void
266
    {
267
        $exp = new ColumnExpression($this->getQueryStatement());
268
        if ($columns instanceof Closure) {
0 ignored issues
show
introduced by
$columns is never a sub-type of Closure.
Loading history...
269
            $columns($exp);
270
        } else {
271
            if (!is_array($columns)) {
0 ignored issues
show
introduced by
The condition is_array($columns) is always true.
Loading history...
272
                $columns = [$columns];
273
            }
274
275
            $exp->columns($columns);
276
        }
277
    }
278
279
    /**
280
     * Return the relation data attributes
281
     * @return array<string, array<string, mixed>>
282
     */
283
    protected function getWithAttributes(): array
284
    {
285
        $with = [];
286
        $extra = [];
287
288
        foreach ($this->with as $key => /** @var string|Closure $value */ $value) {
289
            /** @var string $fullName */
290
            $fullName = $value;
291
            $callback = null;
292
293
            if ($value instanceof Closure) {
294
                /** @var string $fullName */
295
                $fullName = $key;
296
                $callback = $value;
297
            }
298
299
            $fullName = explode('.', $fullName);
300
            $name = array_shift($fullName);
301
            $fullName = implode('.', $fullName);
302
303
            if ($fullName === '') {
304
                if (!isset($with[$name]) || $callback !== null) {
305
                    $with[$name] = $callback;
306
307
                    if (!isset($extra[$name])) {
308
                        $extra[$name] = [];
309
                    }
310
                }
311
            } else {
312
                if (!isset($extra[$name])) {
313
                    $with[$name] = null;
314
                    $extra[$name] = [];
315
                }
316
317
                /** @var array<string, mixed> $tmp */
318
                $tmp = &$extra[$name];
319
320
                if (isset($tmp[$fullName]) || in_array($fullName, $tmp)) {
321
                    continue;
322
                }
323
324
                if ($callback === null) {
325
                    $tmp[] = $fullName;
326
                } else {
327
                    $tmp[$fullName] = $callback;
328
                }
329
            }
330
        }
331
332
        return [
333
            'with' => $with,
334
            'extra' => $extra
335
        ];
336
    }
337
}
338