1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Query\Traits; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use Cycle\Database\Exception\BuilderException; |
16
|
|
|
use Cycle\Database\Injection\FragmentInterface as Fragment; |
17
|
|
|
use Cycle\Database\Injection\ParameterInterface as Parameter; |
18
|
|
|
|
19
|
|
|
trait WhereTrait |
20
|
|
|
{ |
21
|
|
|
protected array $whereTokens = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Simple WHERE condition with various set of arguments. |
25
|
|
|
* |
26
|
|
|
* @param mixed ...$args [(column, value), (column, operator, value)] |
27
|
|
|
* |
28
|
|
|
* @throws BuilderException |
29
|
|
|
* |
30
|
|
|
* @return $this|self |
31
|
|
|
*/ |
32
|
|
|
public function where(mixed ...$args): self |
33
|
1070 |
|
{ |
34
|
|
|
$this->registerToken( |
35
|
1070 |
|
'AND', |
36
|
1070 |
|
$args, |
37
|
|
|
$this->whereTokens, |
38
|
1070 |
|
$this->whereWrapper() |
39
|
1070 |
|
); |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
1030 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Simple AND WHERE condition with various set of arguments. |
46
|
|
|
* |
47
|
|
|
* @param mixed ...$args [(column, value), (column, operator, value)] |
48
|
|
|
* |
49
|
|
|
* @throws BuilderException |
50
|
|
|
* |
51
|
|
|
* @return $this|self |
52
|
|
|
*/ |
53
|
|
|
public function andWhere(mixed ...$args): self |
54
|
72 |
|
{ |
55
|
|
|
$this->registerToken( |
56
|
72 |
|
'AND', |
57
|
72 |
|
$args, |
58
|
|
|
$this->whereTokens, |
59
|
72 |
|
$this->whereWrapper() |
60
|
72 |
|
); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
72 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Simple OR WHERE condition with various set of arguments. |
67
|
|
|
* |
68
|
|
|
* @param mixed ...$args [(column, value), (column, operator, value)] |
69
|
|
|
* |
70
|
|
|
* @throws BuilderException |
71
|
|
|
* |
72
|
|
|
* @return $this|self |
73
|
|
|
*/ |
74
|
|
|
public function orWhere(mixed ...$args): self |
75
|
174 |
|
{ |
76
|
|
|
$this->registerToken( |
77
|
174 |
|
'OR', |
78
|
174 |
|
$args, |
79
|
|
|
$this->whereTokens, |
80
|
174 |
|
$this->whereWrapper() |
81
|
174 |
|
); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
166 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Simple WHERE NOT condition with various set of arguments. |
88
|
|
|
* |
89
|
|
|
* @param mixed ...$args [(column, value), (column, operator, value)] |
90
|
|
|
* |
91
|
|
|
* @throws BuilderException |
92
|
|
|
* |
93
|
|
|
* @return $this|self |
94
|
|
|
*/ |
95
|
|
|
public function whereNot(mixed ...$args): self |
96
|
|
|
{ |
97
|
|
|
$this->registerToken( |
98
|
|
|
'AND NOT', |
99
|
|
|
$args, |
100
|
|
|
$this->whereTokens, |
101
|
|
|
$this->whereWrapper() |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Simple AND WHERE NOT condition with various set of arguments. |
109
|
1070 |
|
* |
110
|
|
|
* @param mixed ...$args [(column, value), (column, operator, value)] |
111
|
1070 |
|
* |
112
|
934 |
|
* @throws BuilderException |
113
|
|
|
* |
114
|
918 |
|
* @return $this|self |
115
|
790 |
|
*/ |
116
|
918 |
|
public function andWhereNot(mixed ...$args): self |
117
|
1070 |
|
{ |
118
|
|
|
$this->registerToken( |
119
|
|
|
'AND NOT', |
120
|
|
|
$args, |
121
|
|
|
$this->whereTokens, |
122
|
|
|
$this->whereWrapper() |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Simple OR WHERE NOT condition with various set of arguments. |
130
|
|
|
* |
131
|
|
|
* @param mixed ...$args [(column, value), (column, operator, value)] |
132
|
|
|
* |
133
|
|
|
* @throws BuilderException |
134
|
|
|
* |
135
|
|
|
* @return $this|self |
136
|
|
|
*/ |
137
|
|
|
public function orWhereNot(mixed ...$args): self |
138
|
|
|
{ |
139
|
|
|
$this->registerToken( |
140
|
|
|
'OR NOT', |
141
|
|
|
$args, |
142
|
|
|
$this->whereTokens, |
143
|
|
|
$this->whereWrapper() |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Convert various amount of where function arguments into valid where token. |
151
|
|
|
* |
152
|
|
|
* @psalm-param non-empty-string $boolean Boolean joiner (AND | OR). |
153
|
|
|
* |
154
|
|
|
* @param array $params Set of parameters collected from where functions. |
155
|
|
|
* @param array $tokens Array to aggregate compiled tokens. Reference. |
156
|
|
|
* @param callable $wrapper Callback or closure used to wrap/collect every potential parameter. |
157
|
|
|
* |
158
|
|
|
* @throws BuilderException |
159
|
|
|
*/ |
160
|
|
|
abstract protected function registerToken( |
161
|
|
|
string $boolean, |
162
|
|
|
array $params, |
163
|
|
|
array &$tokens, |
164
|
|
|
callable $wrapper |
165
|
|
|
): void; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Applied to every potential parameter while where tokens generation. Used to prepare and |
169
|
|
|
* collect where parameters. |
170
|
|
|
*/ |
171
|
|
|
protected function whereWrapper(): Closure |
172
|
|
|
{ |
173
|
|
|
return static fn ($parameter) => $parameter instanceof Parameter || $parameter instanceof Fragment |
174
|
|
|
? $parameter |
175
|
|
|
: new \Cycle\Database\Injection\Parameter($parameter); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|