Fr3nch13/CakePHP Utilities

WhenThenExpression
in package
implements ExpressionInterface uses CaseExpressionTrait, ExpressionTypeCasterTrait

Represents a SQL when/then clause with a fluid API

Table of Contents

Interfaces

ExpressionInterface
An interface used by Expression objects.

Properties

$_typeMap  : TypeMap
The type map to use when using an array of conditions for the `WHEN` value.
$hasThenBeenDefined  : bool
Whether the `THEN` value has been defined, eg whether `then()` has been invoked.
$then  : ExpressionInterface|object|scalar|null
The `THEN` value.
$thenType  : string|null
The `THEN` result type.
$validClauseNames  : array<string|int, string>
The names of the clauses that are valid for use with the `clause()` method.
$when  : ExpressionInterface|object|scalar|null
Then `WHEN` value.
$whenType  : array<string|int, mixed>|string|null
The `WHEN` value type.

Methods

__clone()  : void
Clones the inner expression objects.
__construct()  : mixed
Constructor.
clause()  : ExpressionInterface|object|scalar|null
Returns the available data for the given clause.
getResultType()  : string|null
Returns the expression's result value type.
sql()  : string
Converts the Node into a SQL string fragment.
then()  : $this
Sets the `THEN` result value.
traverse()  : $this
Iterates over each part of the expression recursively for every level of the expressions tree and executes the $callback callable passing as first parameter the instance of the expression currently being iterated.
when()  : $this
Sets the `WHEN` value.
_castToExpression()  : mixed
Conditionally converts the passed value to an ExpressionInterface object if the type class implements the ExpressionTypeInterface. Otherwise, returns the value unmodified.
_requiresToExpressionCasting()  : array<string|int, mixed>
Returns an array with the types that require values to be casted to expressions, out of the list of type names passed as parameter.

Properties

$hasThenBeenDefined

Whether the `THEN` value has been defined, eg whether `then()` has been invoked.

protected bool $hasThenBeenDefined = false

$validClauseNames

The names of the clauses that are valid for use with the `clause()` method.

protected array<string|int, string> $validClauseNames = ['when', 'then']

$whenType

The `WHEN` value type.

protected array<string|int, mixed>|string|null $whenType = null

Methods

__construct()

Constructor.

public __construct([TypeMap|null $typeMap = null ]) : mixed
Parameters
$typeMap : TypeMap|null = null

The type map to use when using an array of conditions for the WHEN value.

clause()

Returns the available data for the given clause.

public clause(string $clause) : ExpressionInterface|object|scalar|null

Available clauses

The following clause names are available:

  • when: The WHEN value.
  • then: The THEN result value.
Parameters
$clause : string

The name of the clause to obtain.

Tags
throws
InvalidArgumentException

In case the given clause name is invalid.

Return values
ExpressionInterface|object|scalar|null

getResultType()

Returns the expression's result value type.

public getResultType() : string|null
Tags
see
WhenThenExpression::then()
Return values
string|null

sql()

Converts the Node into a SQL string fragment.

public sql(ValueBinder $binder) : string
Parameters
$binder : ValueBinder

Parameter binder

Tags
inheritDoc
Return values
string

then()

Sets the `THEN` result value.

public then(ExpressionInterface|object|scalar|null $result[, string|null $type = null ]) : $this
Parameters
$result : ExpressionInterface|object|scalar|null

The result value.

$type : string|null = null

The result type. If no type is provided, the type will be inferred from the given result value.

Return values
$this

traverse()

Iterates over each part of the expression recursively for every level of the expressions tree and executes the $callback callable passing as first parameter the instance of the expression currently being iterated.

public traverse(Closure $callback) : $this
Parameters
$callback : Closure

The callable to apply to all nodes.

Tags
inheritDoc
Return values
$this

when()

Sets the `WHEN` value.

public when(ExpressionInterface|object|array<string|int, mixed>|scalar $when[, array<string, string>|string|null $type = null ]) : $this
Parameters
$when : ExpressionInterface|object|array<string|int, mixed>|scalar

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!

$type : array<string, string>|string|null = null

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.

Tags
throws
InvalidArgumentException

In case the $when argument is neither a non-empty array, nor a scalar value, an object, or an instance of \Cake\Database\ExpressionInterface.

throws
InvalidArgumentException

In case the $type argument is neither an array, a string, nor null.

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.

Return values
$this

_castToExpression()

Conditionally converts the passed value to an ExpressionInterface object if the type class implements the ExpressionTypeInterface. Otherwise, returns the value unmodified.

protected _castToExpression(mixed $value[, string|null $type = null ]) : mixed
Parameters
$value : mixed

The value to convert to ExpressionInterface

$type : string|null = null

The type name

_requiresToExpressionCasting()

Returns an array with the types that require values to be casted to expressions, out of the list of type names passed as parameter.

protected _requiresToExpressionCasting(array<string|int, mixed> $types) : array<string|int, mixed>
Parameters
$types : array<string|int, mixed>

List of type names

Return values
array<string|int, mixed>

        
On this page

Search results