Class WhenThenExpression
Represents a SQL when/then clause with a fluid API
Property Summary
-
$_typeMap protected
Cake\Database\TypeMap
The type map to use when using an array of conditions for the
WHEN
value. -
$hasThenBeenDefined protected
bool
Whether the
THEN
value has been defined, eg whetherthen()
has been invoked. -
$then protected
Cake\Database\ExpressionInterface|object|scalar|null
The
THEN
value. -
$thenType protected
string|null
The
THEN
result type. -
$validClauseNames protected
list<string>
The names of the clauses that are valid for use with the
clause()
method. -
$when protected
Cake\Database\ExpressionInterface|object|scalar|null
Then
WHEN
value. -
$whenType protected
array|string|null
The
WHEN
value type.
Method Summary
-
__clone() public
Clones the inner expression objects.
-
__construct() public
Constructor.
-
_castToExpression() protected
Conditionally converts the passed value to an ExpressionInterface object if the type class implements the ExpressionTypeInterface. Otherwise, returns the value unmodified.
-
_requiresToExpressionCasting() protected
Returns an array with the types that require values to be casted to expressions, out of the list of type names passed as parameter.
-
clause() public
Returns the available data for the given clause.
-
compileNullableValue() protected
Compiles a nullable value to SQL.
-
getResultType() public
Returns the expression's result value type.
-
inferType() protected
Infers the abstract type for the given value.
-
sql() public
Converts the Node into a SQL string fragment.
-
then() public
Sets the
THEN
result value. -
traverse() public
Iterates over each part of the expression recursively for every level of the expressions tree and executes the callback, passing as first parameter the instance of the expression currently being iterated.
-
when() public
Sets the
WHEN
value.
Method Detail
__construct() ¶ public
__construct(Cake\Database\TypeMap|null $typeMap = null)
Constructor.
Parameters
-
Cake\Database\TypeMap|null
$typeMap optional The type map to use when using an array of conditions for the
WHEN
value.
_castToExpression() ¶ protected
_castToExpression(mixed $value, string|null $type = null): mixed
Conditionally converts the passed value to an ExpressionInterface object if the type class implements the ExpressionTypeInterface. Otherwise, returns the value unmodified.
Parameters
-
mixed
$value The value to convert to ExpressionInterface
-
string|null
$type optional The type name
Returns
mixed
_requiresToExpressionCasting() ¶ protected
_requiresToExpressionCasting(array $types): array
Returns an array with the types that require values to be casted to expressions, out of the list of type names passed as parameter.
Parameters
-
array
$types List of type names
Returns
array
clause() ¶ public
clause(string $clause): Cake\Database\ExpressionInterface|object|scalar|null
Returns the available data for the given clause.
Available clauses
The following clause names are available:
when
: TheWHEN
value.then
: TheTHEN
result value.
Parameters
-
string
$clause The name of the clause to obtain.
Returns
Cake\Database\ExpressionInterface|object|scalar|null
Throws
InvalidArgumentException
In case the given clause name is invalid.
compileNullableValue() ¶ protected
compileNullableValue(Cake\Database\ValueBinder $binder, Cake\Database\ExpressionInterface|object|scalar|null $value, string|null $type = null): string
Compiles a nullable value to SQL.
Parameters
-
Cake\Database\ValueBinder
$binder The value binder to use.
-
Cake\Database\ExpressionInterface|object|scalar|null
$value The value to compile.
-
string|null
$type optional The value type.
Returns
string
getResultType() ¶ public
getResultType(): string|null
Returns the expression's result value type.
Returns
string|null
See Also
inferType() ¶ protected
inferType(mixed $value): string|null
Infers the abstract type for the given value.
Parameters
-
mixed
$value The value for which to infer the type.
Returns
string|null
sql() ¶ public
sql(Cake\Database\ValueBinder $binder): string
Converts the Node into a SQL string fragment.
Parameters
-
Cake\Database\ValueBinder
$binder
Returns
string
then() ¶ public
then(Cake\Database\ExpressionInterface|object|scalar|null $result, string|null $type = null): $this
Sets the THEN
result value.
Parameters
-
Cake\Database\ExpressionInterface|object|scalar|null
$result The result value.
-
string|null
$type optional The result type. If no type is provided, the type will be inferred from the given result value.
Returns
$this
traverse() ¶ public
traverse(Closure $callback): $this
Iterates over each part of the expression recursively for every level of the expressions tree and executes the callback, passing as first parameter the instance of the expression currently being iterated.
Parameters
-
Closure
$callback
Returns
$this
when() ¶ public
when(object|array|string|float|int|bool $when, array<string, string>|string|null $type = null): $this
Sets the WHEN
value.
Parameters
-
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!-
array<string, string>|string|null
$type optional 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.
Returns
$this
Throws
InvalidArgumentException
In case the `$when` argument is an empty array.
InvalidArgumentException
In case the `$when` argument is an array, and the `$type` argument is neither an array, nor null.
InvalidArgumentException
In case the `$when` argument is a non-array value, and the `$type` argument is neither a string, nor null.
See Also
Property Detail
$_typeMap ¶ protected
The type map to use when using an array of conditions for the
WHEN
value.
Type
Cake\Database\TypeMap
$hasThenBeenDefined ¶ protected
Whether the THEN
value has been defined, eg whether then()
has been invoked.
Type
bool
$validClauseNames ¶ protected
The names of the clauses that are valid for use with the
clause()
method.
Type
list<string>