Class WhenThenExpression
Represents a SQL when/then clause with a fluid API
Property Summary
-
$_typeMap protected
Cake\Database\TypeMapThe type map to use when using an array of conditions for the
WHENvalue. -
$hasThenBeenDefined protected
boolWhether the
THENvalue has been defined, eg whetherthen()has been invoked. -
$then protected
Cake\Database\ExpressionInterface|object|scalar|nullThe
THENvalue. -
$thenType protected
string|nullThe
THENresult 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|nullThen
WHENvalue. -
$whenType protected
array|string|nullThe
WHENvalue 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
THENresult 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
WHENvalue.
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
WHENvalue.
_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
arrayclause() ¶ 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: TheWHENvalue.then: TheTHENresult value.
Parameters
-
string$clause The name of the clause to obtain.
Returns
Cake\Database\ExpressionInterface|object|scalar|nullThrows
InvalidArgumentExceptionIn 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
stringgetResultType() ¶ public
getResultType(): string|null
Returns the expression's result value type.
Returns
string|nullSee 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|nullThe abstract type, or null if it could not be inferred.
sql() ¶ public
sql(Cake\Database\ValueBinder $binder): string
Converts the Node into a SQL string fragment.
Parameters
-
Cake\Database\ValueBinder$binder
Returns
stringthen() ¶ 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
$thistraverse() ¶ 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
$thiswhen() ¶ 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
WHENvalue. 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$typeargument (which forces the$whenvalue 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
$thisThrows
InvalidArgumentExceptionIn case the `$when` argument is an empty array.
InvalidArgumentExceptionIn case the `$when` argument is an array, and the `$type` argument is neither an array, nor null.
InvalidArgumentExceptionIn 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>