FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
database/Expression/WhenThenExpression.php at 5.x · cakephp/database · GitHub
cakephp
/
database
Public
Notifications
You must be signed in to change notification settings
Fork
13
Star
98
Code
Actions
Security and quality
0
Insights
Additional navigation options
Code
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
database
/
Expression
/
WhenThenExpression.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
318 lines (282 loc) · 9.67 KB
Breadcrumbs
database
/
Expression
/
WhenThenExpression.php
Copy path
File metadata and controls
318 lines (282 loc) · 9.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
declare
(strict_types=
1
);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.3.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace
Cake
\
Database
\
Expression
;
use
Cake
\
Database
\
ExpressionInterface
;
use
Cake
\
Database
\
Query
;
use
Cake
\
Database
\
Type
\
ExpressionTypeCasterTrait
;
use
Cake
\
Database
\
TypeMap
;
use
Cake
\
Database
\
ValueBinder
;
use
Closure
;
use
InvalidArgumentException
;
use
LogicException
;
/**
* Represents a SQL when/then clause with a fluid API
*/
class
WhenThenExpression
implements
ExpressionInterface
{
use
CaseExpressionTrait;
use
ExpressionTypeCasterTrait;
/**
* The names of the clauses that are valid for use with the
* `clause()` method.
*
* @var array<string>
*/
protected
array
$
validClauseNames
= [
'
when
'
,
'
then
'
,
];
/**
* The type map to use when using an array of conditions for the
* `WHEN` value.
*
* @var \Cake\Database\TypeMap
*/
protected
TypeMap
$
_typeMap
;
/**
* Then `WHEN` value.
*
* @var \Cake\Database\ExpressionInterface|object|scalar|null
*/
protected
mixed
$
when
=
null
;
/**
* The `WHEN` value type.
*
* @var array|string|null
*/
protected
array
|
string
|
null
$
whenType
=
null
;
/**
* The `THEN` value.
*
* @var \Cake\Database\ExpressionInterface|object|scalar|null
*/
protected
mixed
$
then
=
null
;
/**
* Whether the `THEN` value has been defined, eg whether `then()`
* has been invoked.
*
* @var bool
*/
protected
bool
$
hasThenBeenDefined
=
false
;
/**
* The `THEN` result type.
*
* @var string|null
*/
protected
?
string
$
thenType
=
null
;
/**
* Constructor.
*
* @param \Cake\Database\TypeMap|null $typeMap The type map to use when using an array of conditions for the `WHEN`
* value.
*/
public
function
__construct
(?
TypeMap
$
typeMap
=
null
)
{
$
this
->
_typeMap
=
$
typeMap
??
new
TypeMap
();
}
/**
* Sets the `WHEN` value.
*
* @param object|array|string|float|int|bool $when The `WHEN` value. When using an array of
* conditions, it must be compatible with `\Cake\Database\Query::where()`. Note that this argument is _not_
* completely safe for use with user data, as a user supplied array would allow for raw SQL to slip in! If you
* plan to use user data, either pass a single type for the `$type` argument (which forces the `$when` value to be
* a non-array, and then always binds the data), use a conditions array where the user data is only passed on the
* value side of the array entries, or custom bindings!
* @param array<string, string>|string|null $type The when value type. Either an associative array when using array style
* conditions, or else a string. If no type is provided, the type will be tried to be inferred from the value.
* @return $this
* @throws \InvalidArgumentException In case the `$when` argument is an empty array.
* @throws \InvalidArgumentException In case the `$when` argument is an array, and the `$type` argument is neither
* an array, nor null.
* @throws \InvalidArgumentException In case the `$when` argument is a non-array value, and the `$type` argument is
* neither a string, nor null.
* @see CaseStatementExpression::when() for a more detailed usage explanation.
*/
public
function
when
(
object
|
array
|
string
|
float
|
int
|
bool
$
when
,
array
|
string
|
null
$
type
=
null
)
{
if
(
is_array
(
$
when
)) {
if
(!
$
when
) {
throw
new
InvalidArgumentException
(
'
The `$when` argument must be a non-empty array
'
);
}
if
(
$
type
!==
null
&&
!
is_array
(
$
type
)
) {
throw
new
InvalidArgumentException
(
sprintf
(
'
When using an array for the `$when` argument, the `$type` argument must be an
'
.
'
array too, `%s` given.
'
,
get_debug_type
(
$
type
),
));
}
// avoid dirtying the type map for possible consecutive `when()` calls
$
typeMap
=
clone
$
this
->
_typeMap
;
if
(
is_array
(
$
type
) &&
$
type
!== []
) {
$
typeMap
=
$
typeMap
->
setTypes
(
$
type
);
}
$
when
=
new
QueryExpression
(
$
when
,
$
typeMap
);
}
else
{
if
(
$
type
!==
null
&&
!
is_string
(
$
type
)
) {
throw
new
InvalidArgumentException
(
sprintf
(
'
When using a non-array value for the `$when` argument, the `$type` argument must
'
.
'
be a string, `%s` given.
'
,
get_debug_type
(
$
type
),
));
}
if
(
$
type
===
null
&&
!(
$
when
instanceof
ExpressionInterface)
) {
$
type
=
$
this
->
inferType
(
$
when
);
}
}
$
this
->
when
=
$
when
;
$
this
->
whenType
=
$
type
;
return
$
this
;
}
/**
* Sets the `THEN` result value.
*
* @param \Cake\Database\ExpressionInterface|object|scalar|null $result The result value.
* @param string|null $type The result type. If no type is provided, the type will be inferred from the given
* result value.
* @return $this
*/
public
function
then
(
mixed
$
result
, ?
string
$
type
=
null
)
{
if
(
$
result
!==
null
&&
!
is_scalar
(
$
result
) &&
!(
is_object
(
$
result
) && !(
$
result
instanceof
Closure))
) {
throw
new
InvalidArgumentException
(
sprintf
(
'
The `$result` argument must be either `null`, a scalar value, an object,
'
.
'
or an instance of `\%s`, `%s` given.
'
,
ExpressionInterface::class,
get_debug_type
(
$
result
),
));
}
$
this
->
then
=
$
result
;
$
this
->
thenType
=
$
type
??
$
this
->
inferType
(
$
result
);
$
this
->
hasThenBeenDefined
=
true
;
return
$
this
;
}
/**
* Returns the expression's result value type.
*
* @return string|null
* @see WhenThenExpression::then()
*/
public
function
getResultType
(): ?
string
{
return
$
this
->
thenType
;
}
/**
* Returns the available data for the given clause.
*
* ### Available clauses
*
* The following clause names are available:
*
* * `when`: The `WHEN` value.
* * `then`: The `THEN` result value.
*
* @param string $clause The name of the clause to obtain.
* @return \Cake\Database\ExpressionInterface|object|scalar|null
* @throws \InvalidArgumentException In case the given clause name is invalid.
*/
public
function
clause
(
string
$
clause
):
mixed
{
if
(!
in_array
(
$
clause
,
$
this
->
validClauseNames
,
true
)) {
throw
new
InvalidArgumentException
(
sprintf
(
'
The `$clause` argument must be one of `%s`, the given value `%s` is invalid.
'
,
implode
(
'
`, `
'
,
$
this
->
validClauseNames
),
$
clause
,
),
);
}
return
$
this
->{
$
clause
};
}
/**
* @inheritDoc
*/
public
function
sql
(
ValueBinder
$
binder
):
string
{
if
(
$
this
->
when
===
null
) {
throw
new
LogicException
(
'
Case expression has incomplete when clause. Missing `when()`.
'
);
}
if
(!
$
this
->
hasThenBeenDefined
) {
throw
new
LogicException
(
'
Case expression has incomplete when clause. Missing `then()` after `when()`.
'
);
}
$
when
=
$
this
->
when
;
if
(
is_string
(
$
this
->
whenType
) &&
!(
$
when
instanceof
ExpressionInterface)
) {
$
when
=
$
this
->
_castToExpression
(
$
when
,
$
this
->
whenType
);
}
if
(
$
when
instanceof
Query) {
$
when
=
sprintf
(
'
(%s)
'
,
$
when
->
sql
(
$
binder
));
}
elseif
(
$
when
instanceof
ExpressionInterface) {
$
when
=
$
when
->
sql
(
$
binder
);
}
else
{
$
placeholder
=
$
binder
->
placeholder
(
'
c
'
);
if
(
is_string
(
$
this
->
whenType
)) {
$
whenType
=
$
this
->
whenType
;
}
else
{
$
whenType
=
null
;
}
$
binder
->
bind
(
$
placeholder
,
$
when
,
$
whenType
);
$
when
=
$
placeholder
;
}
$
then
=
$
this
->
compileNullableValue
(
$
binder
,
$
this
->
then
,
$
this
->
thenType
);
return
"
WHEN
{
$
when
}
THEN
{
$
then
}"
;
}
/**
* @inheritDoc
*/
public
function
traverse
(
Closure
$
callback
)
{
if
(
$
this
->
when
instanceof
ExpressionInterface) {
$
callback
(
$
this
->
when
);
$
this
->
when
->
traverse
(
$
callback
);
}
if
(
$
this
->
then
instanceof
ExpressionInterface) {
$
callback
(
$
this
->
then
);
$
this
->
then
->
traverse
(
$
callback
);
}
return
$
this
;
}
/**
* Clones the inner expression objects.
*/
public
function
__clone
()
{
if
(
$
this
->
when
instanceof
ExpressionInterface) {
$
this
->
when
=
clone
$
this
->
when
;
}
if
(
$
this
->
then
instanceof
ExpressionInterface) {
$
this
->
then
=
clone
$
this
->
then
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL