Fr3nch13/CakePHP Utilities

CaseExpression
in package
implements ExpressionInterface uses ExpressionTypeCasterTrait

This class represents a SQL Case statement

Use QueryExpression::case() or CaseStatementExpression instead

Table of Contents

Interfaces

ExpressionInterface
An interface used by Expression objects.

Properties

$_conditions  : array<string|int, mixed>
A list of strings or other expression objects that represent the conditions of the case statement. For example one key of the array might look like "sum > :value"
$_elseValue  : ExpressionInterface|array<string|int, mixed>|string|null
The `ELSE` value for the case statement. If null then no `ELSE` will be included.
$_values  : array<string|int, mixed>
Values that are associated with the conditions in the $_conditions array.

Methods

__construct()  : mixed
Constructs the case expression
add()  : $this
Adds one or more conditions and their respective true values to the case object.
elseValue()  : void
Sets the default value
sql()  : string
Converts the Node into a SQL string fragment.
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.
_addExpressions()  : void
Iterates over the passed in conditions and ensures that there is a matching true value for each.
_castToExpression()  : mixed
Conditionally converts the passed value to an ExpressionInterface object if the type class implements the ExpressionTypeInterface. Otherwise, returns the value unmodified.
_compile()  : string
Compiles the relevant parts into sql
_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

$_conditions

A list of strings or other expression objects that represent the conditions of the case statement. For example one key of the array might look like "sum > :value"

protected array<string|int, mixed> $_conditions = []

$_elseValue

The `ELSE` value for the case statement. If null then no `ELSE` will be included.

protected ExpressionInterface|array<string|int, mixed>|string|null $_elseValue

$_values

Values that are associated with the conditions in the $_conditions array.

protected array<string|int, mixed> $_values = []

Each value represents the 'true' value for the condition with the corresponding key.

Methods

__construct()

Constructs the case expression

public __construct([ExpressionInterface|array<string|int, mixed> $conditions = [] ][, ExpressionInterface|array<string|int, mixed> $values = [] ][, array<string|int, string> $types = [] ]) : mixed
Parameters
$conditions : ExpressionInterface|array<string|int, mixed> = []

The conditions to test. Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.

$values : ExpressionInterface|array<string|int, mixed> = []

Associative array of values to be associated with the conditions passed in $conditions. If there are more $values than $conditions, the last $value is used as the ELSE value.

$types : array<string|int, string> = []

Associative array of types to be associated with the values passed in $values

add()

Adds one or more conditions and their respective true values to the case object.

public add([ExpressionInterface|array<string|int, mixed> $conditions = [] ][, ExpressionInterface|array<string|int, mixed> $values = [] ][, array<string|int, string> $types = [] ]) : $this

Conditions must be a one dimensional array or a QueryExpression. The trueValues must be a similar structure, but may contain a string value.

Parameters
$conditions : ExpressionInterface|array<string|int, mixed> = []

Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.

$values : ExpressionInterface|array<string|int, mixed> = []

Associative array of values of each condition

$types : array<string|int, string> = []

Associative array of types to be associated with the values

Return values
$this

elseValue()

Sets the default value

public elseValue([ExpressionInterface|array<string|int, mixed>|string|null $value = null ][, string|null $type = null ]) : void
Parameters
$value : ExpressionInterface|array<string|int, mixed>|string|null = null

Value to set

$type : string|null = null

Type of value

sql()

Converts the Node into a SQL string fragment.

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

Placeholder generator object

Return values
string

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

_addExpressions()

Iterates over the passed in conditions and ensures that there is a matching true value for each.

protected _addExpressions(array<string|int, mixed> $conditions, array<string|int, mixed> $values, array<string|int, string> $types) : void

If no matching true value, then it is defaulted to '1'.

Parameters
$conditions : array<string|int, mixed>

Array of ExpressionInterface instances.

$values : array<string|int, mixed>

Associative array of values of each condition

$types : array<string|int, string>

Associative array of types to be associated with the values

_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

_compile()

Compiles the relevant parts into sql

protected _compile(ExpressionInterface|array<string|int, mixed>|string $part, ValueBinder $binder) : string
Parameters
$part : ExpressionInterface|array<string|int, mixed>|string

The part to compile

$binder : ValueBinder

Sql generator

Return values
string

_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