class Expression implements Stringable (View source)

Validation expression builder.

Expression Builder implementation for the Mighty Validation Expression Language (mVEL) v1 (v1.*.*).

Constants

BEHAVIORS

Expression behaviors.

OPERATORS

Expression operators.

Properties

protected string $buffer

Expression string buffer.

protected array $tokens

Expression string buffer tokens.

Methods

mixed
__call(string $name, array $arguments)

Provides rules and aliases as class methods.

string
__toString()

Returns the current expression string.

and()

Adds AND operator (ampersand: &) (default). Ands the next rule/group.

string
build()

Returns the current expression string.

close()

Adds CLOSE operator (closing parenthesis: )). Ends the current group.

static string
comment(string $text)

Returns a the passed string as a valid comment.

concat(string $string)

Writes the passed string to the current expression string buffer.

string
createRuleStatement(string $name, array $arguments)

Returns a valid rule statement from the passed name and arguments.

string
getBehaviorChars()

No description

array
getOperatorChars()

No description

group(callable $callback)

Groups a set of rules inside a pair of parentheses ().

macro(string $macro)

Writes a macro to the current expression string.

normal()

Makes the expression normal (executes all checks). The is the default behavior.

not()

Adds NOT operator (tilde: ~). Negates the next rule/group.

open()

Adds OPEN operator (opening parenthesis: (). Starts a new group.

optimistic()

Makes the expression optimistic (stop checking after first success).

or()

Adds OR operator (pipe: |). Ors the next rule/group.

pessimistic()

Makes the expression pessimistic (stop checking after first failure).

rule(string $rule)

Writes a rule to the current expression string.

static string
variable(string $key)

Returns a the passed key as a valid back-reference injectable.

write(string $string)

Writes the passed string to the current expression string buffer.

xor()

Adds XOR operator (caret: ^). Xors the next rule/group.

Details

mixed __call(string $name, array $arguments)

Provides rules and aliases as class methods.

Parameters

string $name
array $arguments

Return Value

mixed

string __toString()

Returns the current expression string.

Return Value

string

Expression and()

Adds AND operator (ampersand: &) (default). Ands the next rule/group.

Return Value

Expression

string build()

Returns the current expression string.

Return Value

string

Exceptions

InvalidValidationExpressionException

Expression close()

Adds CLOSE operator (closing parenthesis: )). Ends the current group.

Return Value

Expression

static string comment(string $text)

Returns a the passed string as a valid comment.

Parameters

string $text

Comment text.

Return Value

string

A valid comment or an empty string if the passed parameter was empty.

NOTE: This method returns the passed string as a valid comment (∕*text*∕) only, and does NOT add it to the buffer, use self::concat() to add it.

Expression concat(string $string)

Writes the passed string to the current expression string buffer.

Note that the necessary operators will NOT be added automatically (use self::write() instead). Note that this method may make the expression fail when build if it is malformed.

Parameters

string $string

Rule, rule statement, rule alias, rule macro, an operator, a variable, a comment, or any arbitrary string.

Return Value

Expression

protected string createRuleStatement(string $name, array $arguments)

Returns a valid rule statement from the passed name and arguments.

Parameters

string $name

Rule name, preferably in camelCase.

array $arguments

Rule arguments.

Return Value

string

The rule statement.

private string getBehaviorChars()

No description

Return Value

string

private array getOperatorChars()

No description

Return Value

array

Expression group(callable $callback)

Groups a set of rules inside a pair of parentheses ().

Parameters

callable $callback

The callback that will be called to build the group. The callback will be bound to the current instance and/or receive it as the first parameter if can't be bound.

Return Value

Expression

Expression macro(string $macro)

Writes a macro to the current expression string.

Parameters

string $macro

Macro name (without []).

Return Value

Expression

Expression normal()

Makes the expression normal (executes all checks). The is the default behavior.

NOTE: Use this to remove an already added behavior.

Return Value

Expression

Expression not()

Adds NOT operator (tilde: ~). Negates the next rule/group.

Return Value

Expression

Expression open()

Adds OPEN operator (opening parenthesis: (). Starts a new group.

Return Value

Expression

Expression optimistic()

Makes the expression optimistic (stop checking after first success).

NOTE: This has effect only if the expression is not already PESSIMISTIC.

NOTE: Be careful when using OPTIMISTIC with AND or XOR.

Return Value

Expression

Expression or()

Adds OR operator (pipe: |). Ors the next rule/group.

Return Value

Expression

Expression pessimistic()

Makes the expression pessimistic (stop checking after first failure).

NOTE: This has effect only if the expression is not already PESSIMISTIC.

NOTE: Be careful when using PESSIMISTIC with OR or XOR.

Return Value

Expression

Expression rule(string $rule)

Writes a rule to the current expression string.

Parameters

string $rule

Rule statement (or rule alias).

Return Value

Expression

static string variable(string $key)

Returns a the passed key as a valid back-reference injectable.

Parameters

string $key

Variable key (without $}).

Return Value

string

A valid back-reference or an empty string if the passed parameter was empty.

NOTE: This method returns the passed key as a valid back-reference (${key}) only, and NOT the actual value of the variable and does NOT add it to the buffer.

Expression write(string $string)

Writes the passed string to the current expression string buffer.

Note that the necessary operators will be added automatically.

Parameters

string $string

Rule, rule statement, rule alias, rule macro or an operator.

Return Value

Expression

Expression xor()

Adds XOR operator (caret: ^). Xors the next rule/group.

Return Value

Expression